code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 getAntinodes(groups map[byte][]Point, matrix [][]byte, multi bool) map[string]Point {
  50. antinodes := make(map[string]Point)
  51. for key, items := range groups {
  52. edge := len(items)
  53. for i := range items {
  54. for j := i + 1; j < edge; j++ {
  55. deltaX := items[j].x - items[i].x
  56. deltaY := items[j].y - items[i].y
  57. if multi {
  58. if items[j].x == items[i].x || items[j].y == items[i].y || abs(deltaX) == abs(deltaY) {
  59. antinodes[items[i].key()] = items[i]
  60. antinodes[items[j].key()] = items[j]
  61. }
  62. }
  63. firstAntinode := Point{x: items[i].x - deltaX, y: items[i].y - deltaY}
  64. for checkAntinode(firstAntinode, matrix, key) {
  65. antinodes[firstAntinode.key()] = firstAntinode
  66. if multi {
  67. firstAntinode = Point{x: firstAntinode.x - deltaX, y: firstAntinode.y - deltaY}
  68. } else {
  69. firstAntinode = Point{x: -1, y: -1}
  70. }
  71. }
  72. secondAntinode := Point{x: items[j].x + deltaX, y: items[j].y + deltaY}
  73. for checkAntinode(secondAntinode, matrix, key) {
  74. antinodes[secondAntinode.key()] = secondAntinode
  75. if multi {
  76. secondAntinode = Point{x: secondAntinode.x + deltaX, y: secondAntinode.y + deltaY}
  77. } else {
  78. secondAntinode = Point{x: -1, y: -1}
  79. }
  80. }
  81. }
  82. }
  83. }
  84. fmt.Println(antinodes)
  85. return antinodes
  86. }
  87. func main() {
  88. if len(os.Args) < 2 {
  89. log.Fatal("You need to specify a file!")
  90. }
  91. filePath := os.Args[1]
  92. file, err := os.Open(filePath)
  93. if err != nil {
  94. log.Fatalf("Failed to open %s!\n", filePath)
  95. }
  96. groups, matrix := readInput(file)
  97. fmt.Println("Part1:", len(getAntinodes(groups, matrix, false)))
  98. fmt.Println("Part2:", len(getAntinodes(groups, matrix, true)))
  99. }