code.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type Point struct {
  10. y, x int
  11. direction int
  12. }
  13. type Universe struct {
  14. galaxies []Point
  15. occupiedRows map[int]bool
  16. occupiedColumns map[int]bool
  17. }
  18. func readInput(file *os.File) Universe {
  19. scanner := bufio.NewScanner(file)
  20. var universe Universe
  21. universe.occupiedRows = make(map[int]bool)
  22. universe.occupiedColumns = make(map[int]bool)
  23. index := 0
  24. for scanner.Scan() {
  25. line := scanner.Text()
  26. if line == "" {
  27. break
  28. }
  29. if strings.Contains(line, "#") {
  30. universe.occupiedRows[index] = true
  31. for i := range line {
  32. if line[i] == '#' {
  33. universe.galaxies = append(universe.galaxies, Point{y: index, x: i})
  34. universe.occupiedColumns[i] = true
  35. }
  36. }
  37. }
  38. index++
  39. }
  40. return universe
  41. }
  42. func main() {
  43. if len(os.Args) < 2 {
  44. log.Fatal("You need to specify a file!")
  45. }
  46. filePath := os.Args[1]
  47. file, err := os.Open(filePath)
  48. if err != nil {
  49. log.Fatalf("Failed to open %s!\n", filePath)
  50. }
  51. universe := readInput(file)
  52. fmt.Println(universe)
  53. }