diff --git a/day-8/README.md b/day-8/README.md index 2786b1e..98533a1 100644 --- a/day-8/README.md +++ b/day-8/README.md @@ -92,3 +92,45 @@ Because the topmost A-frequency antenna overlaps with a 0-frequency antinode, th Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode? ## Part 2 + +Watching over your shoulder as you work, one of The Historians asks if you took the effects of resonant harmonics into your calculations. + +Whoops! + +After updating your model, it turns out that an antinode occurs at any grid position exactly in line with at least two antennas of the same frequency, regardless of distance. This means that some of the new antinodes will occur at the position of each antenna (unless that antenna is the only one of its frequency). + +So, these three T-frequency antennas now create many antinodes: + +``` +T....#.... +...T...... +.T....#... +.........# +..#....... +.......... +...#...... +.......... +....#..... +.......... +``` + +In fact, the three T-frequency antennas are all exactly in line with two antennas, so they are all also antinodes! This brings the total number of antinodes in the above example to 9. + +The original example now has 34 antinodes, including the antinodes that appear on every antenna: + +``` +##....#....# +.#.#....0... +..#.#0....#. +..##...0.... +....0....#.. +.#...#A....# +...#..#..... +#....#.#.... +..#.....A... +....#....A.. +.#........#. +...#......## +``` + +Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode? diff --git a/day-8/main.go b/day-8/main.go index f158b86..950a8df 100644 --- a/day-8/main.go +++ b/day-8/main.go @@ -88,9 +88,54 @@ func UniqueNodes(g grid.Grid) map[rune][]grid.Coords { func part2(input string) int { parsed := parseInput(input) - _ = parsed + antinodes := make(map[string]struct{}) + antennaMap := grid.Grid{Data: parsed} + nodes := UniqueNodes(antennaMap) - return 0 + for _, coords := range nodes { + // if there are more than one instance of the node + if len(coords) > 1 { + for i, coordA := range coords { + // apply to every other coord + for _, coordB := range coords[i+1:] { + // add the antenna themselves + antinodes[coordA.String()] = struct{}{} + antinodes[coordB.String()] = struct{}{} + + diff := []int{coordA.X - coordB.X, coordA.Y - coordB.Y} + mul := 1 + for { + dir1InBounds := true + dir2InBounds := true + dir1 := grid.Coords{X: coordB.X + diff[0]*mul, Y: coordB.Y + diff[1]*mul} + // inverse + dir2 := grid.Coords{X: coordB.X + diff[0]*mul*-1, Y: coordB.Y + diff[1]*mul*-1} + + if antennaMap.InBounds(dir1) { + antinodes[dir1.String()] = struct{}{} + } else { + dir1InBounds = false + } + + if antennaMap.InBounds(dir2) { + antinodes[dir2.String()] = struct{}{} + } else { + dir2InBounds = false + } + + if !dir1InBounds && !dir2InBounds { + break + } else { + mul++ + } + } + + } + } + } + } + + return len(antinodes) } func parseInput(input string) (ans []string) { diff --git a/day-8/main_test.go b/day-8/main_test.go index acf6934..0354534 100644 --- a/day-8/main_test.go +++ b/day-8/main_test.go @@ -37,7 +37,7 @@ func Test_day8_part1(t *testing.T) { { name: "actual", input: input, - want: 0, + want: 327, run: file.ExistsRelativeFile("input.txt"), }, } @@ -58,20 +58,31 @@ func Benchmark_day8_part1(b *testing.B) { } } -var example2 = `` +var example2 = `............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............` func Test_day8_part2(t *testing.T) { tests := []TestDeclaration{ { name: "example", input: example2, - want: 0, + want: 34, run: true, }, { name: "actual", input: input, - want: 0, + want: 1233, run: file.ExistsRelativeFile("input.txt"), }, }