Skip to content

Commit ea94692

Browse files
committed
fix(07/2025): remove finished beams from vector and solve part one for example
1 parent 4926e86 commit ea94692

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
| [Day 4: Printing Department](src/solutions/year2025/day04.rs) | ⭐⭐ | 3.615 | 9.998 |
1818
| [Day 5: Cafeteria](src/solutions/year2025/day05.rs) | ⭐⭐ | 0.275 | 0.469 |
1919
| [Day 6: Trash Compactor](src/solutions/year2025/day06.rs) | ⭐⭐ | 0.117 | 2.577 |
20-
| [Day 7: Laboratories](src/solutions/year2025/day07.rs) | - | - | - |
20+
| [Day 7: Laboratories](src/solutions/year2025/day07.rs) | | 2.962 | - |
2121

2222
# 2024
2323

src/solutions/year2025/day07.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ impl Solution for Day07 {
2424
let mut splits = 0;
2525

2626
while let Some(current_beam) = current_beams.pop_front() {
27+
if finished_beams
28+
.iter()
29+
.chain(current_beams.iter())
30+
.any(|beam| beam.collides(&current_beam))
31+
{
32+
continue;
33+
}
34+
2735
let down = current_beam.down();
2836

2937
if splitters.contains(&down.current()) {
3038
finished_beams.push(current_beam);
3139
splits += 1;
3240

3341
for split in down.split() {
34-
if finished_beams
35-
.iter()
36-
.chain(current_beams.iter())
37-
.any(|beam| beam.collides(&split))
38-
{
39-
continue;
40-
}
41-
4242
current_beams.push_back(split);
4343
}
4444

@@ -53,8 +53,6 @@ impl Solution for Day07 {
5353
current_beams.push_front(down);
5454
}
5555

56-
print(&grid, &finished_beams);
57-
5856
splits.to_string()
5957
}
6058

@@ -66,9 +64,8 @@ impl Solution for Day07 {
6664
impl Day07 {
6765
fn parse(&self, input: &str) -> Grid<char> {
6866
let without_redundant_lines = input.lines().step_by(2).collect::<Vec<_>>().join("\n");
69-
let input = without_redundant_lines.as_str();
7067

71-
Grid::from(input)
68+
Grid::from(without_redundant_lines.as_str())
7269
}
7370
}
7471

@@ -89,15 +86,12 @@ struct Beam {
8986

9087
impl Beam {
9188
fn collides(&self, other: &Self) -> bool {
92-
let other_start = other.line.start();
93-
if other_start != other.line.end() {
94-
panic!("We only support beam that just started");
95-
}
89+
let other = other.current();
9690

9791
let start = self.line.start();
9892
let end = self.line.end();
9993

100-
start.x == other_start.x && (start.y..=end.y).contains(&other_start.y)
94+
start.x == other.x && (start.y..=end.y).contains(&other.y)
10195
}
10296

10397
fn down(&self) -> Self {
@@ -219,6 +213,22 @@ mod tests {
219213
assert_eq!("3", Day07.part_one(EXAMPLE_FROM_REDDIT));
220214
}
221215

216+
const EXAMPLE_FROM_REDDIT2: &str = r#"..S..
217+
.....
218+
..^..
219+
.....
220+
...^.
221+
.....
222+
.^...
223+
.....
224+
..^..
225+
....."#;
226+
227+
#[test]
228+
fn part_one_example_from_reddit2() {
229+
assert_eq!("4", Day07.part_one(EXAMPLE_FROM_REDDIT2));
230+
}
231+
222232
const MY_EXAMPLE: &str = r#"..S..
223233
.....
224234
..^..

0 commit comments

Comments
 (0)