|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/shaunburdick/advent-of-code-2024/lib/file" |
| 11 | + "github.com/shaunburdick/advent-of-code-2024/lib/grid" |
| 12 | +) |
| 13 | + |
| 14 | +var input string |
| 15 | + |
| 16 | +func init() { |
| 17 | + // do this in init (not main) so test file has same input |
| 18 | + inputFile, err := file.LoadRelativeFile("input.txt") |
| 19 | + if err != nil { |
| 20 | + log.Println(err) |
| 21 | + } |
| 22 | + |
| 23 | + input = strings.TrimRight(inputFile, "\n") |
| 24 | +} |
| 25 | + |
| 26 | +func main() { |
| 27 | + var part int |
| 28 | + flag.IntVar(&part, "part", 1, "part 1 or 2") |
| 29 | + flag.Parse() |
| 30 | + fmt.Println("Running part", part) |
| 31 | + |
| 32 | + if part == 1 { |
| 33 | + ans := part1(input) |
| 34 | + fmt.Println("Output:", ans) |
| 35 | + } else { |
| 36 | + ans := part2(input) |
| 37 | + fmt.Println("Output:", ans) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func part1(input string) int { |
| 42 | + parsed := parseInput(input) |
| 43 | + antinodes := make(map[string]struct{}) |
| 44 | + antennaMap := grid.Grid{Data: parsed} |
| 45 | + nodes := UniqueNodes(antennaMap) |
| 46 | + |
| 47 | + for _, coords := range nodes { |
| 48 | + // if there are more than one instance of the node |
| 49 | + if len(coords) > 1 { |
| 50 | + for i, coordA := range coords { |
| 51 | + // apply to every other coord |
| 52 | + for _, coordB := range coords[i+1:] { |
| 53 | + antiNodeA := grid.Coords{X: 2*coordA.X - coordB.X, Y: 2*coordA.Y - coordB.Y} |
| 54 | + antiNodeB := grid.Coords{X: 2*coordB.X - coordA.X, Y: 2*coordB.Y - coordA.Y} |
| 55 | + |
| 56 | + if antennaMap.InBounds(antiNodeA) { |
| 57 | + antinodes[antiNodeA.String()] = struct{}{} |
| 58 | + } |
| 59 | + |
| 60 | + if antennaMap.InBounds(antiNodeB) { |
| 61 | + antinodes[antiNodeB.String()] = struct{}{} |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return len(antinodes) |
| 69 | +} |
| 70 | + |
| 71 | +func UniqueNodes(g grid.Grid) map[rune][]grid.Coords { |
| 72 | + nodes := make(map[rune][]grid.Coords) |
| 73 | + |
| 74 | + for y, row := range g.Data { |
| 75 | + for x, char := range row { |
| 76 | + if char != '.' { |
| 77 | + if _, found := nodes[char]; !found { |
| 78 | + nodes[char] = []grid.Coords{} |
| 79 | + } |
| 80 | + |
| 81 | + nodes[char] = append(nodes[char], grid.Coords{X: x, Y: y}) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nodes |
| 87 | +} |
| 88 | + |
| 89 | +func part2(input string) int { |
| 90 | + parsed := parseInput(input) |
| 91 | + _ = parsed |
| 92 | + |
| 93 | + return 0 |
| 94 | +} |
| 95 | + |
| 96 | +func parseInput(input string) (ans []string) { |
| 97 | + return strings.Split(input, "\n") |
| 98 | +} |
0 commit comments