You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/csmith/aoc-2019/common"
  5. "github.com/csmith/aoc-2019/intcode"
  6. "strconv"
  7. "strings"
  8. )
  9. const (
  10. up = '^'
  11. down = 'v'
  12. left = '<'
  13. right = '>'
  14. borked = 'X'
  15. )
  16. func rotate(from, to rune) rune {
  17. if from == up && to == left || from == left && to == down || from == down && to == right || from == right && to == up {
  18. return 'L'
  19. } else {
  20. return 'R'
  21. }
  22. }
  23. func next(picture [][]rune, x, y int, direction rune) (nextDirection, turn rune) {
  24. if x > 0 && direction != right && picture[y][x-1] == '#' {
  25. nextDirection = left
  26. } else if x < len(picture[0])-1 && direction != left && picture[y][x+1] == '#' {
  27. nextDirection = right
  28. } else if y > 0 && direction != down && picture[y-1][x] == '#' {
  29. nextDirection = up
  30. } else if y < len(picture)-1 && direction != up && picture[y+1][x] == '#' {
  31. nextDirection = down
  32. } else {
  33. return borked, borked
  34. }
  35. return nextDirection, rotate(direction, nextDirection)
  36. }
  37. func follow(picture [][]rune, x, y int, direction rune) (int, int, int) {
  38. deltaX, deltaY := 0, 0
  39. switch direction {
  40. case up:
  41. deltaY = -1
  42. case down:
  43. deltaY = 1
  44. case left:
  45. deltaX = -1
  46. case right:
  47. deltaX = +1
  48. case borked:
  49. return x, y, borked
  50. }
  51. length := 0
  52. for x+deltaX >= 0 && x+deltaX <= len(picture[0])-1 && y+deltaY >= 0 && y+deltaY <= len(picture)-1 && picture[y+deltaY][x+deltaX] == '#' {
  53. x += deltaX
  54. y += deltaY
  55. length++
  56. }
  57. return x, y, length
  58. }
  59. func compactPass(movement string) string {
  60. start := 0
  61. for movement[start] == 'A' || movement[start] == 'B' {
  62. start += 2
  63. }
  64. end := start + 4
  65. for movement[end] != ',' {
  66. end++
  67. }
  68. count := strings.Count(movement, movement[start:end])
  69. for {
  70. newEnd := end + 1
  71. for movement[newEnd] != ',' {
  72. newEnd++
  73. }
  74. if movement[newEnd-1] == 'A' || movement[newEnd-1] == 'B' {
  75. break
  76. }
  77. newCount := strings.Count(movement, movement[start:end])
  78. if newCount == count {
  79. end = newEnd
  80. } else {
  81. break
  82. }
  83. }
  84. return movement[start:end]
  85. }
  86. func compact(movement string) (main, a, b, c string) {
  87. main = movement
  88. a = compactPass(main)
  89. main = strings.ReplaceAll(main, a, "A")
  90. b = compactPass(main)
  91. main = strings.ReplaceAll(main, b, "B")
  92. c = compactPass(main)
  93. main = strings.ReplaceAll(main, c, "C")
  94. return
  95. }
  96. func readPicture(memory []int) [][]rune {
  97. vm := intcode.NewVirtualMachine(memory)
  98. vm.Input = make(chan int, 1)
  99. vm.Output = make(chan int, 1)
  100. go vm.Run()
  101. var picture [][]rune
  102. var currentRow []rune
  103. for {
  104. char, more := <-vm.Output
  105. if !more {
  106. break
  107. }
  108. if char == '\n' && len(currentRow) > 0 {
  109. picture = append(picture, currentRow)
  110. currentRow = make([]rune, 0)
  111. } else {
  112. currentRow = append(currentRow, rune(char))
  113. }
  114. }
  115. return picture
  116. }
  117. func analysePicture(picture [][]rune) (sum int, robot rune, robotX, robotY int) {
  118. robot = borked
  119. for y, line := range picture {
  120. for x, r := range line {
  121. if r == '#' &&
  122. x > 0 && line[x-1] == '#' &&
  123. x < len(line)-1 && line[x+1] == '#' &&
  124. y > 0 && picture[y-1][x] == '#' &&
  125. y < len(picture)-1 && picture[y+1][x] == '#' {
  126. sum += x * y
  127. }
  128. if r == up || r == down || r == left || r == right {
  129. robotX = x
  130. robotY = y
  131. robot = r
  132. }
  133. }
  134. }
  135. return
  136. }
  137. func buildRoute(picture [][]rune, robot rune, robotX, robotY int) string {
  138. var (
  139. length int
  140. turn rune
  141. routeBuilder strings.Builder
  142. )
  143. for {
  144. robot, turn = next(picture, robotX, robotY, robot)
  145. robotX, robotY, length = follow(picture, robotX, robotY, robot)
  146. if robot == borked {
  147. break
  148. }
  149. if routeBuilder.Len() > 0 {
  150. routeBuilder.WriteRune(',')
  151. }
  152. routeBuilder.WriteRune(turn)
  153. routeBuilder.WriteString(strconv.Itoa(length))
  154. }
  155. return routeBuilder.String()
  156. }
  157. func calculateDust(input []int, m, a, b, c string) int {
  158. vm := intcode.NewVirtualMachine(input)
  159. vm.Input = make(chan int, 1)
  160. vm.Output = make(chan int, 1)
  161. vm.Memory[0] = 2
  162. go vm.Run()
  163. go func() {
  164. for _, line := range []string{m, a, b, c, "n"} {
  165. for _, r := range line {
  166. vm.Input <- int(r)
  167. if r == 'L' || r == 'R' {
  168. vm.Input <- ','
  169. }
  170. }
  171. vm.Input <- '\n'
  172. }
  173. }()
  174. return common.Last(vm.Output)
  175. }
  176. func main() {
  177. input := common.ReadCsvAsInts("17/input.txt")
  178. memory := make([]int, len(input))
  179. copy(memory, input)
  180. picture := readPicture(input)
  181. sum, robot, robotX, robotY := analysePicture(picture)
  182. route := buildRoute(picture, robot, robotX, robotY)
  183. m, a, b, c := compact(route)
  184. dust := calculateDust(memory, m, a, b, c)
  185. fmt.Println(sum)
  186. fmt.Println(dust)
  187. }