Skip to content

Commit 4b351ed

Browse files
committed
[2025] Day 7 draft
1 parent 40a22cb commit 4b351ed

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
1111
- [Day 04: Printing Department](aoc_2025/src/day_04.rs)
1212
- [Day 05: Cafeteria](aoc_2025/src/day_05.rs)
1313
- [Day 06: Trash Compactor](aoc_2025/src/day_06.rs)
14+
- [Day 07: Laboratories](aoc_2025/src/day_07.rs)
1415
<!-- MARKER -->
1516

1617
## [2024](https://adventofcode.com/2024) [![aoc_2024](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml)

aoc_2025/src/day_07.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
use std::{
2+
collections::{HashMap, HashSet},
3+
convert::identity,
4+
};
5+
6+
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
7+
use common::{Answer, solution};
8+
use nd_vec::vector;
9+
10+
solution!("Laboratories", 7);
11+
12+
fn part_a(input: &str) -> Answer {
13+
let grid = Grid::parse(input, identity);
14+
let start = grid.find('S').unwrap();
15+
16+
let mut beams = HashSet::new();
17+
beams.insert(start);
18+
19+
let mut out = 0;
20+
21+
loop {
22+
let mut next_beams = HashSet::new();
23+
for beam in beams.iter() {
24+
let next = Direction::Down.advance(*beam);
25+
let Some(tile) = grid.get(next) else {
26+
continue;
27+
};
28+
29+
if *tile == '^' {
30+
out += 1;
31+
next_beams.insert(next - vector!(1, 0));
32+
next_beams.insert(next + vector!(1, 0));
33+
} else {
34+
next_beams.insert(next);
35+
}
36+
}
37+
38+
if next_beams.is_empty() {
39+
break;
40+
}
41+
42+
beams = next_beams;
43+
}
44+
45+
out.into()
46+
}
47+
48+
fn part_b(input: &str) -> Answer {
49+
let grid = Grid::parse(input, identity);
50+
let start = grid.find('S').unwrap();
51+
52+
let mut beams = HashMap::new();
53+
beams.insert(start, 1_u64);
54+
55+
// let mut out = 0;
56+
57+
loop {
58+
let mut next_beams = HashMap::new();
59+
for (beam, count) in beams.iter() {
60+
let next = Direction::Down.advance(*beam);
61+
let Some(tile) = grid.get(next) else {
62+
continue;
63+
};
64+
65+
if *tile == '^' {
66+
*next_beams.entry(next - vector!(1, 0)).or_default() += *count;
67+
*next_beams.entry(next + vector!(1, 0)).or_default() += *count;
68+
} else {
69+
*next_beams.entry(next).or_default() += *count;
70+
}
71+
}
72+
73+
println!("count : {}", next_beams.len());
74+
if next_beams.is_empty() {
75+
break;
76+
}
77+
78+
beams = next_beams;
79+
}
80+
81+
beams.iter().map(|x| x.1).sum::<u64>().into()
82+
}
83+
84+
#[cfg(test)]
85+
mod test {
86+
use indoc::indoc;
87+
88+
const CASE: &str = indoc! {"
89+
.......S.......
90+
...............
91+
.......^.......
92+
...............
93+
......^.^......
94+
...............
95+
.....^.^.^.....
96+
...............
97+
....^.^...^....
98+
...............
99+
...^.^...^.^...
100+
...............
101+
..^...^.....^..
102+
...............
103+
.^.^.^.^.^...^.
104+
...............
105+
"};
106+
107+
#[test]
108+
fn part_a() {
109+
assert_eq!(super::part_a(CASE), 21.into());
110+
}
111+
112+
#[test]
113+
fn part_b() {
114+
assert_eq!(super::part_b(CASE), 40.into());
115+
}
116+
}

aoc_2025/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod day_03;
66
mod day_04;
77
mod day_05;
88
mod day_06;
9+
mod day_07;
910
// [import_marker]
1011

1112
pub const SOLUTIONS: &[Solution] = &[
@@ -15,5 +16,6 @@ pub const SOLUTIONS: &[Solution] = &[
1516
day_04::SOLUTION,
1617
day_05::SOLUTION,
1718
day_06::SOLUTION,
19+
day_07::SOLUTION,
1820
// [list_marker]
1921
];

0 commit comments

Comments
 (0)