Skip to content

Commit b39cada

Browse files
committed
day8
1 parent 629ef12 commit b39cada

File tree

1 file changed

+121
-17
lines changed

1 file changed

+121
-17
lines changed

Diff for: 2024/src/day8.rs

+121-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::{HashMap, HashSet}, fmt::{Debug, Display}, hash::Hash};
1+
use std::{collections::{HashMap, HashSet}, fmt::Debug, hash::Hash};
22

33
use aoc_runner_derive::{aoc, aoc_generator};
44

@@ -89,6 +89,7 @@ Calculate the impact of the signal. How many unique locations within the bounds
8989
enum Cell {
9090
Empty,
9191
Antenna(char),
92+
#[allow(unused)]
9293
Antinode
9394
}
9495

@@ -136,18 +137,12 @@ fn part1(input: &Grid<Cell>) -> u32 {
136137
let dr = r2 as isize - r1 as isize;
137138
let dc = c2 as isize - c1 as isize;
138139

139-
if let Some((r,c, cell)) = input.get_offset(r2, dr, c2, dc) {
140-
// if cell == Cell::Empty
141-
{
142-
antinodes.insert((r,c));
143-
}
140+
if let Some((r,c, _cell)) = input.get_offset(r2, dr, c2, dc) {
141+
antinodes.insert((r,c));
144142
}
145143

146-
if let Some((r,c, cell)) = input.get_offset(r1, -dr, c1, -dc) {
147-
// if cell == Cell::Empty
148-
{
149-
antinodes.insert((r,c));
150-
}
144+
if let Some((r,c, _cell)) = input.get_offset(r1, -dr, c1, -dc) {
145+
antinodes.insert((r,c));
151146
}
152147
}
153148
}
@@ -162,13 +157,109 @@ fn part1(input: &Grid<Cell>) -> u32 {
162157

163158
// dbg!(&result);
164159

165-
166160
antinodes.len() as u32
167161
}
168162

163+
164+
/*
165+
--- Part Two ---
166+
Watching over your shoulder as you work, one of The Historians asks if you took the effects of resonant harmonics into your calculations.
167+
168+
Whoops!
169+
170+
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).
171+
172+
So, these three T-frequency antennas now create many antinodes:
173+
174+
T....#....
175+
...T......
176+
.T....#...
177+
.........#
178+
..#.......
179+
..........
180+
...#......
181+
..........
182+
....#.....
183+
..........
184+
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.
185+
186+
The original example now has 34 antinodes, including the antinodes that appear on every antenna:
187+
188+
##....#....#
189+
.#.#....0...
190+
..#.#0....#.
191+
..##...0....
192+
....0....#..
193+
.#...#A....#
194+
...#..#.....
195+
#....#.#....
196+
..#.....A...
197+
....#....A..
198+
.#........#.
199+
...#......##
200+
Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode?
201+
*/
202+
169203
#[aoc(day8, part2)]
170204
fn part2(input: &Grid<Cell>) -> u32 {
171-
todo!()
205+
let mut antennas = HashMap::new();
206+
for (r, row) in input.0.iter().enumerate() {
207+
for (c, cell) in row.iter().enumerate() {
208+
if let Cell::Antenna(freq) = cell {
209+
antennas.entry(*freq).or_insert_with(HashSet::new).insert((r, c));
210+
}
211+
}
212+
}
213+
214+
let mut antinodes = HashSet::new();
215+
216+
for (_, antennas) in antennas {
217+
let antennas : Vec<_> = antennas.iter().copied().collect();
218+
for i in 0..antennas.len() {
219+
for j in i + 1..antennas.len() {
220+
let (r1, c1) = antennas[i];
221+
let (r2, c2) = antennas[j];
222+
223+
let ddr = r2 as isize - r1 as isize;
224+
let ddc = c2 as isize - c1 as isize;
225+
226+
{
227+
let mut dr = ddr;
228+
let mut dc = ddc;
229+
230+
while let Some((r,c, _cell)) = input.get_offset(r1, dr, c1, dc) {
231+
antinodes.insert((r,c));
232+
233+
dr += ddr;
234+
dc += ddc;
235+
}
236+
}
237+
238+
{
239+
let mut dr = -ddr;
240+
let mut dc = -ddc;
241+
242+
while let Some((r,c, _cell)) = input.get_offset(r2, dr, c2, dc) {
243+
antinodes.insert((r,c));
244+
245+
dr -= ddr;
246+
dc -= ddc;
247+
}
248+
}
249+
}
250+
}
251+
}
252+
253+
// dbg!(&input);
254+
255+
// let mut result = input.clone();
256+
// for an in &antinodes {
257+
// result.0[an.0][an.1] = Cell::Antinode;
258+
// }
259+
260+
// dbg!(&result);
261+
262+
antinodes.len() as u32
172263
}
173264

174265

@@ -194,8 +285,21 @@ mod tests {
194285
"#)), 14);
195286
}
196287

197-
// #[test]
198-
// fn part2_example() {
199-
// assert_eq!(part2(&parse("<EXAMPLE>")), "<RESULT>");
200-
// }
288+
#[test]
289+
fn part2_example() {
290+
assert_eq!(part2(&parse(r#"
291+
............
292+
........0...
293+
.....0......
294+
.......0....
295+
....0.......
296+
......A.....
297+
............
298+
............
299+
........A...
300+
.........A..
301+
............
302+
............
303+
"#)), 34);
304+
}
201305
}

0 commit comments

Comments
 (0)