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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 planets 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) the middle of the parabola occurs after step 1000 (otherwise the code would need a fiddly bit of
  70. // state tracking to ensure it returned a value for part 1).
  71. //
  72. // 3) because the axes are independent, they can be simulated in parallel, and their individual parabolic inflection
  73. // points found. This gives us the loop count for each axis (2x the number of steps to reach the inflection point),
  74. // and we can find the first time all three axis's loops intersect by finding the lowest common multiple of those
  75. // three values.
  76. //
  77. // e.g.
  78. //
  79. // <------------------- position on axis ------------------>
  80. //
  81. // __,..--'"" |
  82. // step 1000 _,..--'"" ^ |
  83. // v _,..-'"" start |
  84. // _,..-'" vel = 0 |
  85. // _,.-'" |
  86. // _.-'" |
  87. // _.-" |
  88. // .-' |
  89. // .' |
  90. // / |
  91. // ; } inflection point steps
  92. // ; } vel = 0, step = n |
  93. // \ |
  94. // `. |
  95. // `-. |
  96. // "-._ |
  97. // "`-._ |
  98. // "`-.,_ |
  99. // "`-..,_ |
  100. // ""`-..,_ |
  101. // ""`--..,_ |
  102. // ""`--..,__ V
  103. // ^
  104. // back to original position
  105. // vel = 0, step = 2n
  106. func main() {
  107. pos, vel := readInput("12/input.txt")
  108. part1wg, part1chan := &sync.WaitGroup{}, make(chan []int64, 3)
  109. part2wg, part2chan := &sync.WaitGroup{}, make(chan int64, 3)
  110. for i, ps := range pos {
  111. part1wg.Add(1)
  112. part2wg.Add(1)
  113. go func(pos, vel []int64) {
  114. for n := int64(0); ; n++ {
  115. step(pos, vel)
  116. if n+1 == 1000 {
  117. part1chan <- append(pos, vel...)
  118. part1wg.Done()
  119. }
  120. if static(vel) {
  121. part2chan <- 2 * (n + 1)
  122. part2wg.Done()
  123. return
  124. }
  125. }
  126. }(ps, vel[i])
  127. }
  128. part1wg.Wait()
  129. println(energy(part1chan, len(pos[0])))
  130. part2wg.Wait()
  131. println(common.LCM(<-part2chan, <-part2chan, <-part2chan))
  132. }