Skip to content

Commit e55b7c3

Browse files
committed
2025 day 7 part 2
1 parent 5689a5c commit e55b7c3

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

src/y2025/day7.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::grid::parse_each_char;
2+
use std::collections::HashMap;
23
use std::collections::HashSet;
34

45
#[derive(Debug)]
@@ -44,8 +45,31 @@ pub fn part1(input: &Input) -> usize {
4445
count
4546
}
4647

47-
pub fn part2(input: &Input) -> &str {
48-
"unimplemented"
48+
pub fn part2(input: &Input) -> u64 {
49+
let mut beams: HashMap<usize, u64> = HashMap::new();
50+
beams.insert(input.start, 1);
51+
for splits in input.splitters.iter() {
52+
let mut new_beams: HashMap<usize, u64> = HashMap::with_capacity(beams.len() * 2);
53+
for (beam, paths) in beams.into_iter() {
54+
if splits.contains(&beam) {
55+
new_beams
56+
.entry(beam - 1)
57+
.and_modify(|c| *c += paths)
58+
.or_insert(paths);
59+
new_beams
60+
.entry(beam + 1)
61+
.and_modify(|c| *c += paths)
62+
.or_insert(paths);
63+
} else {
64+
new_beams
65+
.entry(beam)
66+
.and_modify(|c| *c += paths)
67+
.or_insert(paths);
68+
}
69+
}
70+
beams = new_beams;
71+
}
72+
beams.into_values().sum()
4973
}
5074

5175
#[test]
@@ -70,4 +94,5 @@ fn test() {
7094
";
7195
let input = parse_input(test_input);
7296
assert_eq!(21, part1(&input));
97+
assert_eq!(40, part2(&input));
7398
}

0 commit comments

Comments
 (0)