code.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Point struct {
  9. x, y int
  10. }
  11. func (p *Point) key() string {
  12. return fmt.Sprintf("%d_%d", p.y, p.x)
  13. }
  14. func readInput(file *os.File) (map[byte][]Point, [][]byte) {
  15. scanner := bufio.NewScanner(file)
  16. var matrix [][]byte
  17. groups := make(map[byte][]Point)
  18. var y int
  19. for scanner.Scan() {
  20. line := scanner.Text()
  21. if line == "" {
  22. break
  23. }
  24. matrix = append(matrix, []byte(line))
  25. for x := range line {
  26. if line[x] != '.' {
  27. groups[line[x]] = append(groups[line[x]], Point{x: x, y: y})
  28. }
  29. }
  30. y++
  31. }
  32. return groups, matrix
  33. }
  34. func abs(a int) int {
  35. if a < 0 {
  36. return -a
  37. }
  38. return a
  39. }
  40. func checkAntinode(antinode Point, matrix [][]byte, key byte) bool {
  41. if antinode.x < 0 || antinode.y < 0 || antinode.y >= len(matrix) || antinode.x >= len(matrix[0]) {
  42. return false
  43. }
  44. if matrix[antinode.y][antinode.x] == key {
  45. return false
  46. }
  47. return true
  48. }
  49. func checkInLine(antinode Point, antenna Point) bool {
  50. return antinode.x == antenna.x || antinode.y == antenna.y || abs(antinode.x-antenna.x) == abs(antinode.y-antenna.y)
  51. }
  52. func getAntinodes(groups map[byte][]Point, matrix [][]byte, multi bool) map[string]Point {
  53. antinodes := make(map[string]Point)
  54. for key, items := range groups {
  55. edge := len(items)
  56. for i := range items {
  57. for j := i + 1; j < edge; j++ {
  58. deltaX := items[j].x - items[i].x
  59. deltaY := items[j].y - items[i].y
  60. firstAntinode := Point{x: items[i].x - deltaX, y: items[i].y - deltaY}
  61. for checkAntinode(firstAntinode, matrix, key) {
  62. antinodes[firstAntinode.key()] = firstAntinode
  63. if multi {
  64. for _, antenna := range items {
  65. _, ok := antinodes[antenna.key()]
  66. if ok {
  67. continue
  68. }
  69. if checkInLine(firstAntinode, antenna) {
  70. antinodes[antenna.key()] = antenna
  71. }
  72. }
  73. firstAntinode = Point{x: firstAntinode.x - deltaX, y: firstAntinode.y - deltaY}
  74. } else {
  75. firstAntinode = Point{x: -1, y: -1}
  76. }
  77. }
  78. secondAntinode := Point{x: items[j].x + deltaX, y: items[j].y + deltaY}
  79. for checkAntinode(secondAntinode, matrix, key) {
  80. antinodes[secondAntinode.key()] = secondAntinode
  81. if multi {
  82. for _, antenna := range items {
  83. _, ok := antinodes[antenna.key()]
  84. if ok {
  85. continue
  86. }
  87. if checkInLine(secondAntinode, antenna) {
  88. antinodes[antenna.key()] = antenna
  89. }
  90. }
  91. secondAntinode = Point{x: secondAntinode.x + deltaX, y: secondAntinode.y + deltaY}
  92. } else {
  93. secondAntinode = Point{x: -1, y: -1}
  94. }
  95. }
  96. }
  97. }
  98. }
  99. return antinodes
  100. }
  101. func main() {
  102. if len(os.Args) < 2 {
  103. log.Fatal("You need to specify a file!")
  104. }
  105. filePath := os.Args[1]
  106. file, err := os.Open(filePath)
  107. if err != nil {
  108. log.Fatalf("Failed to open %s!\n", filePath)
  109. }
  110. groups, matrix := readInput(file)
  111. fmt.Println("Part1:", len(getAntinodes(groups, matrix, false)))
  112. fmt.Println("Part2:", len(getAntinodes(groups, matrix, true)))
  113. }