Skip to content

Commit 689806d

Browse files
committed
Year 2024 Day 8
1 parent 280064b commit 689806d

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7979
| 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [Source](src/year2024/day05.rs) | 18 |
8080
| 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [Source](src/year2024/day06.rs) | 386 |
8181
| 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [Source](src/year2024/day07.rs) | 136 |
82+
| 8 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [Source](src/year2024/day08.rs) | 8 |
8283

8384
## 2023
8485

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,5 @@ mod year2024 {
299299
benchmark!(year2024, day05);
300300
benchmark!(year2024, day06);
301301
benchmark!(year2024, day07);
302+
benchmark!(year2024, day08);
302303
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,5 @@ pub mod year2024 {
298298
pub mod day05;
299299
pub mod day06;
300300
pub mod day07;
301+
pub mod day08;
301302
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,5 +368,6 @@ fn year2024() -> Vec<Solution> {
368368
solution!(year2024, day05),
369369
solution!(year2024, day06),
370370
solution!(year2024, day07),
371+
solution!(year2024, day08),
371372
]
372373
}

src/year2024/day08.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! # Resonant Collinearity
2+
use crate::util::grid::*;
3+
use crate::util::hash::*;
4+
use crate::util::point::*;
5+
6+
type Input = (Grid<u8>, FastMap<u8, Vec<Point>>);
7+
8+
pub fn parse(input: &str) -> Input {
9+
let grid = Grid::parse(input);
10+
let mut antennas = FastMap::new();
11+
12+
for y in 0..grid.height {
13+
for x in 0..grid.width {
14+
let point = Point::new(x, y);
15+
let frequency = grid[point];
16+
17+
if frequency != b'.' {
18+
antennas.entry(frequency).or_insert_with(Vec::new).push(point);
19+
}
20+
}
21+
}
22+
23+
(grid, antennas)
24+
}
25+
26+
pub fn part1(input: &Input) -> u32 {
27+
let (grid, antennas) = input;
28+
let mut locations = grid.same_size_with(0);
29+
30+
for frequency in antennas.values() {
31+
for &first in frequency {
32+
for &second in frequency {
33+
if first != second {
34+
let distance = second - first;
35+
let antinode = second + distance;
36+
37+
if grid.contains(antinode) {
38+
locations[antinode] = 1;
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
locations.bytes.iter().sum()
46+
}
47+
48+
pub fn part2(input: &Input) -> u32 {
49+
let (grid, antennas) = input;
50+
let mut locations = grid.same_size_with(0);
51+
52+
for frequency in antennas.values() {
53+
for &first in frequency {
54+
for &second in frequency {
55+
if first != second {
56+
let distance = second - first;
57+
let mut antinode = second;
58+
59+
while grid.contains(antinode) {
60+
locations[antinode] = 1;
61+
antinode += distance;
62+
}
63+
}
64+
}
65+
}
66+
}
67+
68+
locations.bytes.iter().sum()
69+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,5 @@ mod year2024 {
288288
mod day05_test;
289289
mod day06_test;
290290
mod day07_test;
291+
mod day08_test;
291292
}

tests/year2024/day08_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use aoc::year2024::day08::*;
2+
3+
const EXAMPLE: &str = "\
4+
............
5+
........0...
6+
.....0......
7+
.......0....
8+
....0.......
9+
......A.....
10+
............
11+
............
12+
........A...
13+
.........A..
14+
............
15+
............";
16+
17+
#[test]
18+
fn part1_test() {
19+
let input = parse(EXAMPLE);
20+
assert_eq!(part1(&input), 14);
21+
}
22+
23+
#[test]
24+
fn part2_test() {
25+
let input = parse(EXAMPLE);
26+
assert_eq!(part2(&input), 34);
27+
}

0 commit comments

Comments
 (0)