Skip to content

Commit fe4d7d4

Browse files
committed
Day 8
1 parent a1ce6f0 commit fe4d7d4

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1818
| [Day 5](./src/bin/05.rs) | `66.0µs` | `188.7µs` |
1919
| [Day 6](./src/bin/06.rs) | `48.5µs` | `4.2ms` |
2020
| [Day 7](./src/bin/07.rs) | `316.2µs` | `319.6µs` |
21-
| [Day 8](./src/bin/08.rs) | `7.3µs` | `15.6µs` |
21+
| [Day 8](./src/bin/08.rs) | `7.4µs` | `15.5µs` |
2222

2323
**Total: 6.00ms**
2424
<!--- benchmarking table --->

src/bin/08.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const MAX_ANTENNA_PER_TYPE: usize = 16;
1212
type AntennaMap<'a> =
1313
HeaplessHashMap<&'a char, HeaplessVec<Point, MAX_ANTENNA_PER_TYPE>, MAX_ANTENNA_TYPES>;
1414

15-
fn solve<const PART1: bool>(input: &str) -> Option<u32> {
15+
fn solve(
16+
input: &str,
17+
update_set: &mut impl FnMut(&Grid<char>, &mut HeaplessHashSet<Point, 2048>, (Point, Point)),
18+
) -> Option<u32> {
1619
let grid = Grid::new_char_grid_from_str(input);
1720

1821
let antennas: AntennaMap = into_group_map_heapless(
@@ -24,33 +27,36 @@ fn solve<const PART1: bool>(input: &str) -> Option<u32> {
2427
let mut pos_set: HeaplessHashSet<Point, 2048> = HeaplessHashSet::new();
2528
for (_, vec) in antennas.iter() {
2629
for (a, b) in vec.iter().tuple_combinations() {
27-
for (a, b) in [(a, b), (b, a)] {
28-
let dir = b.as_vector_direction(a);
29-
let mut p = *a + dir;
30-
if PART1 {
31-
if grid.is_in_bounds(p) {
32-
pos_set.insert(p).unwrap();
33-
}
34-
} else {
35-
pos_set.insert(*a).unwrap();
36-
pos_set.insert(*b).unwrap();
37-
while grid.is_in_bounds(p) {
38-
pos_set.insert(p).unwrap();
39-
p = p + dir;
40-
}
41-
}
30+
for (&a, &b) in [(a, b), (b, a)] {
31+
update_set(&grid, &mut pos_set, (a, b));
4232
}
4333
}
4434
}
4535

4636
Some(pos_set.len() as u32)
4737
}
38+
4839
pub fn part_one(input: &str) -> Option<u32> {
49-
solve::<true>(input)
40+
solve(input, &mut |grid, pos_set, (a, b)| {
41+
let dir = b.as_vector_direction(&a);
42+
let p = a + dir;
43+
if grid.is_in_bounds(p) {
44+
pos_set.insert(p).unwrap();
45+
}
46+
})
5047
}
5148

5249
pub fn part_two(input: &str) -> Option<u32> {
53-
solve::<false>(input)
50+
solve(input, &mut |grid, pos_set, (a, b)| {
51+
let dir = b.as_vector_direction(&a);
52+
let mut p = a + dir;
53+
pos_set.insert(a).unwrap();
54+
pos_set.insert(b).unwrap();
55+
while grid.is_in_bounds(p) {
56+
pos_set.insert(p).unwrap();
57+
p = p + dir;
58+
}
59+
})
5460
}
5561

5662
#[cfg(test)]

0 commit comments

Comments
 (0)