code.go 2.5 KB

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