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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 angleBetween(asteroid1, asteroid2 *asteroid) float64 {
  22. if asteroid1.y == asteroid2.y {
  23. if asteroid2.x > asteroid1.x {
  24. return math.Pi / 2
  25. } else {
  26. return 3 * math.Pi / 2
  27. }
  28. } else {
  29. angle := math.Atan(float64(asteroid2.x-asteroid1.x) / float64(asteroid1.y-asteroid2.y))
  30. if asteroid1.y < asteroid2.y {
  31. angle += math.Pi
  32. }
  33. if angle < 0 {
  34. angle += math.Pi * 2
  35. }
  36. return angle
  37. }
  38. }
  39. func checkAngles(asteroid1 *asteroid, others []*asteroid, countVisible bool) map[float64][]*asteroid {
  40. angles := make(map[float64][]*asteroid)
  41. for _, asteroid2 := range others {
  42. if asteroid2 == asteroid1 {
  43. continue
  44. }
  45. angle := angleBetween(asteroid1, asteroid2)
  46. if len(angles[angle]) == 0 && countVisible {
  47. asteroid1.visible++
  48. asteroid2.visible++
  49. }
  50. angles[angle] = append(angles[angle], asteroid2)
  51. }
  52. return angles
  53. }
  54. func main() {
  55. var (
  56. input = common.ReadFileAsStrings("10/input.txt")
  57. asteroids = buildMap(input)
  58. best *asteroid
  59. )
  60. for i, asteroid1 := range asteroids {
  61. checkAngles(asteroid1, asteroids[i+1:], true)
  62. if best == nil || asteroid1.visible > best.visible {
  63. best = asteroid1
  64. }
  65. }
  66. if best == nil {
  67. panic("No asteroids found?")
  68. }
  69. println(best.visible)
  70. targets := checkAngles(best, asteroids, false)
  71. angles := make([]float64, 0, len(targets))
  72. for k := range targets {
  73. angles = append(angles, k)
  74. }
  75. sort.Float64s(angles)
  76. var destroyed *asteroid
  77. var i = 0
  78. for n := 0; n < 200; n++ {
  79. if len(targets[angles[i]]) == 1 {
  80. // There's a single target at this angle, skip the angle in the future
  81. destroyed = targets[angles[i]][0]
  82. angles = append(angles[:i], angles[i+1:]...)
  83. } else {
  84. // Multiple targets exists at this angle, remove the closest and move on to the next angle
  85. var bestDistance = math.MaxFloat64
  86. var bestTarget = 0
  87. for j, target := range targets[angles[i]] {
  88. distance := math.Abs(float64(target.x-best.x)) + math.Abs(float64(target.y-best.y))
  89. if distance < bestDistance {
  90. bestDistance = distance
  91. bestTarget = j
  92. }
  93. }
  94. destroyed = targets[angles[i]][bestTarget]
  95. targets[angles[i]] = append(targets[angles[i]][:bestTarget], targets[angles[i]][bestTarget+1:]...)
  96. i++
  97. }
  98. i = i % len(angles)
  99. }
  100. if destroyed == nil {
  101. panic("Universe doesn't make sense. Reboot and try again?")
  102. }
  103. println(destroyed.x*100 + destroyed.y)
  104. }