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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package main
  2. import (
  3. "github.com/csmith/aoc-2019/common"
  4. "math"
  5. "sort"
  6. )
  7. type asteroid struct {
  8. x, y, visible int
  9. }
  10. func buildMap(input []string) []*asteroid {
  11. var res []*asteroid
  12. for y, line := range input {
  13. for x, r := range line {
  14. if r == '#' {
  15. res = append(res, &asteroid{x: x, y: y})
  16. }
  17. }
  18. }
  19. return res
  20. }
  21. func checkAngles(asteroid1 *asteroid, others []*asteroid, countVisible bool) map[float64][]*asteroid {
  22. angles := make(map[float64][]*asteroid, len(others))
  23. for _, asteroid2 := range others {
  24. if asteroid2 == asteroid1 {
  25. continue
  26. }
  27. angle := math.Atan2(float64(asteroid2.x-asteroid1.x), float64(asteroid1.y-asteroid2.y))
  28. if angle < 0 {
  29. angle += math.Pi * 2
  30. }
  31. if countVisible && len(angles[angle]) == 0 {
  32. asteroid1.visible++
  33. asteroid2.visible++
  34. }
  35. angles[angle] = append(angles[angle], asteroid2)
  36. }
  37. return angles
  38. }
  39. func selectBest(asteroids []*asteroid) (best *asteroid) {
  40. for i, asteroid1 := range asteroids {
  41. checkAngles(asteroid1, asteroids[i+1:], true)
  42. if best == nil || asteroid1.visible > best.visible {
  43. best = asteroid1
  44. }
  45. }
  46. return
  47. }
  48. func closest(origin *asteroid, asteroids []*asteroid) (int, *asteroid) {
  49. var bestDistance = math.MaxFloat64
  50. var bestIndex = 0
  51. for j, target := range asteroids {
  52. distance := math.Abs(float64(target.x-origin.x)) + math.Abs(float64(target.y-origin.y))
  53. if distance < bestDistance {
  54. bestDistance = distance
  55. bestIndex = j
  56. }
  57. }
  58. return bestIndex, asteroids[bestIndex]
  59. }
  60. func sortedAngles(targets map[float64][]*asteroid) []float64 {
  61. angles := make([]float64, 0, len(targets))
  62. for k := range targets {
  63. angles = append(angles, k)
  64. }
  65. sort.Float64s(angles)
  66. return angles
  67. }
  68. func main() {
  69. var (
  70. input = common.ReadFileAsStrings("10/input.txt")
  71. asteroids = buildMap(input)
  72. best = selectBest(asteroids)
  73. targets = checkAngles(best, asteroids, false)
  74. angles = sortedAngles(targets)
  75. )
  76. var destroyed *asteroid
  77. if len(angles) >= 200 {
  78. // We don't complete a full loop, so the answer is just the nearest asteroid at the 200th angle
  79. _, destroyed = closest(best, targets[angles[199]])
  80. } else {
  81. // We loop once, actually simulate the whole thing.
  82. var i = 0
  83. for n := 0; n < 200; n++ {
  84. if len(targets[angles[i]]) == 1 {
  85. // There's a single target at this angle, skip the angle in the future
  86. destroyed = targets[angles[i]][0]
  87. angles = append(angles[:i], angles[i+1:]...)
  88. } else {
  89. // Multiple targets exists at this angle, remove the closest and move on to the next angle
  90. var nearestIndex int
  91. nearestIndex, destroyed = closest(best, targets[angles[i]])
  92. targets[angles[i]] = append(targets[angles[i]][:nearestIndex], targets[angles[i]][nearestIndex+1:]...)
  93. i++
  94. }
  95. i = i % len(angles)
  96. }
  97. }
  98. if destroyed == nil {
  99. panic("Universe doesn't make sense. Reboot and try again?")
  100. }
  101. println(best.visible)
  102. println(destroyed.x*100 + destroyed.y)
  103. }