Skip to content

Commit c03d137

Browse files
committed
solve: day 7
1 parent 2f63d49 commit c03d137

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
resolver = "2"
33
members = [
44
"aoc_lib/aoc_derive",
5-
"aoc_lib/utils", "day1", "day2", "day3", "day4", "day5", "day6",
5+
"aoc_lib/utils", "day1", "day2", "day3", "day4", "day5", "day6", "day7",
66
]
77

88
[workspace.dependencies]

aoc_lib

day7/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "day7"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
aoc_derive.path = '../aoc_lib/aoc_derive'
8+
utils.path = '../aoc_lib/utils'
9+
derive_more.workspace = true
10+
itertools.workspace = true
11+
lazy-regex.workspace = true
12+
parse-display.workspace = true
13+
rayon.workspace = true
14+
regex.workspace = true
15+
num.workspace = true
16+
17+
[dev-dependencies]
18+
pretty_assertions.workspace = true

day7/src/main.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::collections::HashMap;
2+
3+
use aoc_derive::aoc_main;
4+
use utils::{OneOrTwo::*, grid::Grid, *};
5+
6+
#[derive(Debug, Clone)]
7+
struct Beams {
8+
x: HashMap<i64, usize>,
9+
y: i64,
10+
}
11+
12+
impl Beams {
13+
fn solve(mut self, manifold: &Grid<char>) -> (usize, usize) {
14+
let mut num_splits = 0;
15+
while self.y + 1 < manifold.num_rows() as i64 {
16+
self.x = self
17+
.x
18+
.into_iter()
19+
.flat_map(|(x, count)| {
20+
if manifold.get((x, self.y + 1)).unwrap() == &'^' {
21+
num_splits += 1;
22+
Two((x + 1, count), (x - 1, count))
23+
} else {
24+
One((x, count))
25+
}
26+
})
27+
.fold(HashMap::new(), |mut map, (x, count)| {
28+
*map.entry(x).or_insert(0) += count;
29+
map
30+
});
31+
self.y += 1;
32+
}
33+
(num_splits, self.x.values().sum())
34+
}
35+
}
36+
37+
#[aoc_main]
38+
fn solve(input: Input) -> impl Into<Solution> {
39+
let manifold = input.char_grid();
40+
let start = manifold.find_position(&'S').unwrap();
41+
42+
Beams { x: HashMap::from([(start.x, 1)]), y: start.y }.solve(&manifold)
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
#[test]
49+
fn test_examples() {
50+
use utils::assert_example;
51+
assert_example!(
52+
r#".......S.......
53+
...............
54+
.......^.......
55+
...............
56+
......^.^......
57+
...............
58+
.....^.^.^.....
59+
...............
60+
....^.^...^....
61+
...............
62+
...^.^...^.^...
63+
...............
64+
..^...^.....^..
65+
...............
66+
.^.^.^.^.^...^.
67+
..............."#,
68+
21,
69+
40
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)