Skip to content

Commit ea7eeb2

Browse files
committed
Day 8 Part 2
1 parent 049fe97 commit ea7eeb2

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

day-8/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,45 @@ Because the topmost A-frequency antenna overlaps with a 0-frequency antinode, th
9292
Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
9393

9494
## Part 2
95+
96+
Watching over your shoulder as you work, one of The Historians asks if you took the effects of resonant harmonics into your calculations.
97+
98+
Whoops!
99+
100+
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).
101+
102+
So, these three T-frequency antennas now create many antinodes:
103+
104+
```
105+
T....#....
106+
...T......
107+
.T....#...
108+
.........#
109+
..#.......
110+
..........
111+
...#......
112+
..........
113+
....#.....
114+
..........
115+
```
116+
117+
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.
118+
119+
The original example now has 34 antinodes, including the antinodes that appear on every antenna:
120+
121+
```
122+
##....#....#
123+
.#.#....0...
124+
..#.#0....#.
125+
..##...0....
126+
....0....#..
127+
.#...#A....#
128+
...#..#.....
129+
#....#.#....
130+
..#.....A...
131+
....#....A..
132+
.#........#.
133+
...#......##
134+
```
135+
136+
Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode?

day-8/main.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,54 @@ func UniqueNodes(g grid.Grid) map[rune][]grid.Coords {
8888

8989
func part2(input string) int {
9090
parsed := parseInput(input)
91-
_ = parsed
91+
antinodes := make(map[string]struct{})
92+
antennaMap := grid.Grid{Data: parsed}
93+
nodes := UniqueNodes(antennaMap)
9294

93-
return 0
95+
for _, coords := range nodes {
96+
// if there are more than one instance of the node
97+
if len(coords) > 1 {
98+
for i, coordA := range coords {
99+
// apply to every other coord
100+
for _, coordB := range coords[i+1:] {
101+
// add the antenna themselves
102+
antinodes[coordA.String()] = struct{}{}
103+
antinodes[coordB.String()] = struct{}{}
104+
105+
diff := []int{coordA.X - coordB.X, coordA.Y - coordB.Y}
106+
mul := 1
107+
for {
108+
dir1InBounds := true
109+
dir2InBounds := true
110+
dir1 := grid.Coords{X: coordB.X + diff[0]*mul, Y: coordB.Y + diff[1]*mul}
111+
// inverse
112+
dir2 := grid.Coords{X: coordB.X + diff[0]*mul*-1, Y: coordB.Y + diff[1]*mul*-1}
113+
114+
if antennaMap.InBounds(dir1) {
115+
antinodes[dir1.String()] = struct{}{}
116+
} else {
117+
dir1InBounds = false
118+
}
119+
120+
if antennaMap.InBounds(dir2) {
121+
antinodes[dir2.String()] = struct{}{}
122+
} else {
123+
dir2InBounds = false
124+
}
125+
126+
if !dir1InBounds && !dir2InBounds {
127+
break
128+
} else {
129+
mul++
130+
}
131+
}
132+
133+
}
134+
}
135+
}
136+
}
137+
138+
return len(antinodes)
94139
}
95140

96141
func parseInput(input string) (ans []string) {

day-8/main_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Test_day8_part1(t *testing.T) {
3737
{
3838
name: "actual",
3939
input: input,
40-
want: 0,
40+
want: 327,
4141
run: file.ExistsRelativeFile("input.txt"),
4242
},
4343
}
@@ -58,20 +58,31 @@ func Benchmark_day8_part1(b *testing.B) {
5858
}
5959
}
6060

61-
var example2 = ``
61+
var example2 = `............
62+
........0...
63+
.....0......
64+
.......0....
65+
....0.......
66+
......A.....
67+
............
68+
............
69+
........A...
70+
.........A..
71+
............
72+
............`
6273

6374
func Test_day8_part2(t *testing.T) {
6475
tests := []TestDeclaration{
6576
{
6677
name: "example",
6778
input: example2,
68-
want: 0,
79+
want: 34,
6980
run: true,
7081
},
7182
{
7283
name: "actual",
7384
input: input,
74-
want: 0,
85+
want: 1233,
7586
run: file.ExistsRelativeFile("input.txt"),
7687
},
7788
}

0 commit comments

Comments
 (0)