Skip to content

Commit a1ce6f0

Browse files
committed
Day 8
1 parent 1cf8230 commit a1ce6f0

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ 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` |
2122

22-
**Total: 5.98ms**
23+
**Total: 6.00ms**
2324
<!--- benchmarking table --->
2425

2526
---

data/examples/08.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

mygrid/src/point.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ impl Point {
8484
(value & 0xFFFFFFFFFFFFFFFF) as isize,
8585
)
8686
}
87+
88+
#[inline]
89+
pub fn as_direction(&self) -> Direction {
90+
Direction::new(self.line, self.column)
91+
}
92+
93+
#[inline]
94+
pub fn as_vector_direction(&self, other: &Point) -> Direction {
95+
Direction::new(other.line - self.line, other.column - self.column)
96+
}
8797
}
8898

8999
impl Hash for Point {

src/bin/08.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use advent_of_code::into_group_map_heapless;
2+
use heapless::FnvIndexMap as HeaplessHashMap;
3+
use heapless::FnvIndexSet as HeaplessHashSet;
4+
use heapless::Vec as HeaplessVec;
5+
use itertools::Itertools;
6+
use mygrid::grid::Grid;
7+
use mygrid::point::Point;
8+
advent_of_code::solution!(8);
9+
10+
const MAX_ANTENNA_TYPES: usize = 64;
11+
const MAX_ANTENNA_PER_TYPE: usize = 16;
12+
type AntennaMap<'a> =
13+
HeaplessHashMap<&'a char, HeaplessVec<Point, MAX_ANTENNA_PER_TYPE>, MAX_ANTENNA_TYPES>;
14+
15+
fn solve<const PART1: bool>(input: &str) -> Option<u32> {
16+
let grid = Grid::new_char_grid_from_str(input);
17+
18+
let antennas: AntennaMap = into_group_map_heapless(
19+
grid.iter_item_and_position()
20+
.filter(|(_, &c)| c != '.')
21+
.map(|(point, c)| (c, point)),
22+
);
23+
24+
let mut pos_set: HeaplessHashSet<Point, 2048> = HeaplessHashSet::new();
25+
for (_, vec) in antennas.iter() {
26+
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+
}
42+
}
43+
}
44+
}
45+
46+
Some(pos_set.len() as u32)
47+
}
48+
pub fn part_one(input: &str) -> Option<u32> {
49+
solve::<true>(input)
50+
}
51+
52+
pub fn part_two(input: &str) -> Option<u32> {
53+
solve::<false>(input)
54+
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
fn test_part_one() {
62+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
63+
assert_eq!(result, Some(14));
64+
}
65+
66+
#[test]
67+
fn test_part_two() {
68+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
69+
assert_eq!(result, Some(34));
70+
}
71+
}

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
pub mod template;
2+
use std::fmt::Debug;
3+
use std::hash::Hash;
4+
5+
use heapless::FnvIndexMap as HeaplessHashMap;
6+
use heapless::Vec as HeaplessVec;
27

38
// Use this file to add helper functions and additional modules.
9+
10+
pub fn into_group_map_heapless<const M: usize, const N: usize, I, K, V>(
11+
iter: I,
12+
) -> HeaplessHashMap<K, HeaplessVec<V, M>, N>
13+
where
14+
V: Clone + Debug,
15+
I: Iterator<Item = (K, V)>,
16+
K: Hash + Eq + Debug,
17+
{
18+
let mut lookup: HeaplessHashMap<K, HeaplessVec<V, M>, N> = HeaplessHashMap::new();
19+
20+
iter.for_each(|(key, val)| {
21+
if let Some(vec) = lookup.get_mut(&key) {
22+
vec.push(val).unwrap();
23+
} else {
24+
let mut vec = HeaplessVec::new();
25+
vec.push(val).unwrap();
26+
lookup.insert(key, vec).unwrap();
27+
}
28+
});
29+
30+
lookup
31+
}

0 commit comments

Comments
 (0)