code.go 2.4 KB

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