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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/csmith/aoc-2019/common"
  5. "sync"
  6. )
  7. func readInput(file string) (pos [3][]int64, vel [3][]int64) {
  8. for _, line := range common.ReadFileAsStrings(file) {
  9. var x, y, z int64
  10. if _, err := fmt.Sscanf(line, "<x=%d, y=%d, z=%d>", &x, &y, &z); err != nil {
  11. panic(fmt.Sprintf("unable to parse line '%v': %v", line, err))
  12. }
  13. pos[0] = append(pos[0], x)
  14. pos[1] = append(pos[1], y)
  15. pos[2] = append(pos[2], z)
  16. vel[0] = append(vel[0], 0)
  17. vel[1] = append(vel[1], 0)
  18. vel[2] = append(vel[2], 0)
  19. }
  20. return
  21. }
  22. func attract(p1, p2 int64, dp1, dp2 *int64) {
  23. if p1 < p2 {
  24. *dp1++
  25. *dp2--
  26. } else if p1 > p2 {
  27. *dp1--
  28. *dp2++
  29. }
  30. }
  31. func step(pos, vel []int64) {
  32. for x := 0; x < len(pos); x++ {
  33. for y := x + 1; y < len(pos); y++ {
  34. attract(pos[x], pos[y], &vel[x], &vel[y])
  35. }
  36. }
  37. for i, v := range vel {
  38. pos[i] += v
  39. }
  40. }
  41. func static(vel []int64) bool {
  42. for _, v := range vel {
  43. if v != 0 {
  44. return false
  45. }
  46. }
  47. return true
  48. }
  49. func energy(channel chan []int64, moons int) int64 {
  50. sums := make([]int64, moons*2)
  51. for i := 0; i < 3; i++ {
  52. row := <-channel
  53. for n, v := range row {
  54. sums[n] += common.Abs(v)
  55. }
  56. }
  57. energy := int64(0)
  58. for i := 0; i < len(sums)/2; i++ {
  59. energy += sums[i] * sums[len(sums)/2+i]
  60. }
  61. return energy
  62. }
  63. // Sweeping assumptions/notes on how this mess works:
  64. //
  65. // 1) the movement of the moons will be parabolic - they'll accelerate towards each other,
  66. // then gradually slow down to a complete stop and reverse path going back to the starting point.
  67. // I'm not sure I can prove that's the case in general, but it does seem to hold true.
  68. //
  69. // 2) all the moons will hit the inflection point of the parabola at the same step. I think this is reasonable
  70. // as each force is mirrored, so bodies accelerating towards one another will end up with the same total velocity
  71. // when they cross
  72. //
  73. // 3) the middle of the parabola occurs after step 1000 (otherwise the code would need a fiddly bit of
  74. // state tracking to ensure it returned a value for part 1).
  75. //
  76. // 4) because the axes are independent, they can be simulated in parallel, and their individual parabolic inflection
  77. // points found. This gives us the loop count for each axis (2x the number of steps to reach the inflection point),
  78. // and we can find the first time all three axis's loops intersect by finding the lowest common multiple of those
  79. // three values.
  80. //
  81. // e.g.
  82. //
  83. // <------------------- position on axis ------------------>
  84. //
  85. // (somewhere) __,..--'"" |
  86. // step 1000 _,..--'"" ^ |
  87. // v _,..-'"" start |
  88. // _,..-'" vel = 0 |
  89. // _,.-'" |
  90. // _.-'" |
  91. // _.-" |
  92. // .-' |
  93. // .' |
  94. // / |
  95. // ; } inflection point steps
  96. // ; } vel = 0, step = n |
  97. // \ |
  98. // `. |
  99. // `-. |
  100. // "-._ |
  101. // "`-._ |
  102. // "`-.,_ |
  103. // "`-..,_ |
  104. // ""`-..,_ |
  105. // ""`--..,_ |
  106. // ""`--..,__ V
  107. // ^
  108. // back to original position
  109. // vel = 0, step = 2n
  110. func main() {
  111. pos, vel := readInput("12/input.txt")
  112. part1wg, part1chan := &sync.WaitGroup{}, make(chan []int64, 3)
  113. part2wg, part2chan := &sync.WaitGroup{}, make(chan int64, 3)
  114. for i, ps := range pos {
  115. part1wg.Add(1)
  116. part2wg.Add(1)
  117. go func(pos, vel []int64) {
  118. for n := int64(0); ; n++ {
  119. step(pos, vel)
  120. if n+1 == 1000 {
  121. part1chan <- append(pos, vel...)
  122. part1wg.Done()
  123. }
  124. if static(vel) {
  125. part2chan <- 2 * (n + 1)
  126. part2wg.Done()
  127. return
  128. }
  129. }
  130. }(ps, vel[i])
  131. }
  132. part1wg.Wait()
  133. println(energy(part1chan, len(pos[0])))
  134. part2wg.Wait()
  135. println(common.LCM(<-part2chan, <-part2chan, <-part2chan))
  136. }