day24.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func readDirections(line string) []string {
  9. var path []string
  10. signs := []rune(line)
  11. size := len(signs)
  12. for i := 0; i < size; i++ {
  13. switch signs[i] {
  14. case 'e', 'w':
  15. path = append(path, string(signs[i]))
  16. case 's', 'n':
  17. path = append(path, string(signs[i:i+2]))
  18. i++
  19. }
  20. }
  21. return path
  22. }
  23. func readFile(file *os.File) [][]string {
  24. var paths [][]string
  25. scanner := bufio.NewScanner(file)
  26. for scanner.Scan() {
  27. line := scanner.Text()
  28. if line == "" {
  29. break
  30. }
  31. paths = append(paths, readDirections(line))
  32. }
  33. if err := scanner.Err(); err != nil {
  34. log.Fatalf("Scanner error: %s", err)
  35. }
  36. return paths
  37. }
  38. type position struct {
  39. x float64
  40. y float64
  41. }
  42. func makeMove(path []string) position {
  43. currentPosition := position{x: 0, y: 0}
  44. for _, item := range path {
  45. switch item {
  46. case "e":
  47. currentPosition.x += 1
  48. case "w":
  49. currentPosition.x -= 1
  50. case "se":
  51. currentPosition.x += 0.5
  52. currentPosition.y -= 0.5
  53. case "sw":
  54. currentPosition.x -= 0.5
  55. currentPosition.y -= 0.5
  56. case "nw":
  57. currentPosition.x -= 0.5
  58. currentPosition.y += 0.5
  59. case "ne":
  60. currentPosition.x += 0.5
  61. currentPosition.y += 0.5
  62. }
  63. }
  64. return currentPosition
  65. }
  66. func makeAllMoves(paths [][]string) map[position]int {
  67. moves := make(map[position]int)
  68. for _, path := range paths {
  69. currentPosition := makeMove(path)
  70. moves[currentPosition] += 1
  71. }
  72. return moves
  73. }
  74. func part1(moves map[position]int) int {
  75. black := 0
  76. for _, value := range moves {
  77. if value%2 != 0 {
  78. black++
  79. }
  80. }
  81. return black
  82. }
  83. func main() {
  84. if len(os.Args) < 2 {
  85. log.Fatal("You need to specify a file!")
  86. }
  87. filePath := os.Args[1]
  88. file, err := os.Open(filePath)
  89. if err != nil {
  90. log.Fatalf("Failed to open %s!\n", filePath)
  91. }
  92. paths := readFile(file)
  93. if err := file.Close(); err != nil {
  94. log.Fatalf("Failed to close file: %s", err)
  95. }
  96. moves := makeAllMoves(paths)
  97. fmt.Println("Part1:", part1(moves))
  98. }