Skip to content

Commit

Permalink
Day 8 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunburdick committed Dec 11, 2024
1 parent 049fe97 commit ea7eeb2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
42 changes: 42 additions & 0 deletions day-8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
49 changes: 47 additions & 2 deletions day-8/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 15 additions & 4 deletions day-8/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_day8_part1(t *testing.T) {
{
name: "actual",
input: input,
want: 0,
want: 327,
run: file.ExistsRelativeFile("input.txt"),
},
}
Expand All @@ -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"),
},
}
Expand Down

0 comments on commit ea7eeb2

Please sign in to comment.