Skip to content

Commit f9c26f0

Browse files
committed
Faster approach skipping empty rows and columns
1 parent 9a5bcd1 commit f9c26f0

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8383
| 4 | [Printing Department](https://adventofcode.com/2025/day/4) | [Source](src/year2025/day04.rs) | 177 |
8484
| 5 | [Cafeteria](https://adventofcode.com/2025/day/5) | [Source](src/year2025/day05.rs) | 20 |
8585
| 6 | [Trash Compactor](https://adventofcode.com/2025/day/6) | [Source](src/year2025/day06.rs) | 20 |
86-
| 7 | [Laboratories](https://adventofcode.com/2025/day/7) | [Source](src/year2025/day07.rs) | 15 |
86+
| 7 | [Laboratories](https://adventofcode.com/2025/day/7) | [Source](src/year2025/day07.rs) | 5 |
8787

8888
## 2024
8989

src/year2025/day07.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,26 @@ type Input = (u64, u64);
44
pub fn parse(input: &str) -> Input {
55
let lines: Vec<_> = input.lines().map(str::as_bytes).collect();
66
let width = lines[0].len();
7-
let start = lines[0].iter().position(|&b| b == b'S').unwrap();
7+
let center = width / 2;
88

99
let mut splits = 0;
10-
let mut current = vec![0; width];
11-
let mut next = vec![0; width];
12-
13-
current[start] = 1;
14-
15-
for row in lines {
16-
for (i, &count) in current.iter().enumerate() {
17-
if count > 0 {
18-
if row[i] == b'^' {
19-
splits += 1;
20-
21-
if i > 0 {
22-
next[i - 1] += count;
23-
}
24-
if i < width - 1 {
25-
next[i + 1] += count;
26-
}
27-
} else {
28-
next[i] += count;
29-
}
10+
let mut timelines = vec![0; width];
11+
timelines[center] = 1;
12+
13+
for (y, row) in lines.iter().skip(2).step_by(2).enumerate() {
14+
for x in ((center - y)..(center + y + 1)).step_by(2) {
15+
let count = timelines[x];
16+
17+
if count > 0 && row[x] == b'^' {
18+
splits += 1;
19+
timelines[x] = 0;
20+
timelines[x - 1] += count;
21+
timelines[x + 1] += count;
3022
}
3123
}
32-
33-
(current, next) = (next, current);
34-
next.fill(0);
3524
}
3625

37-
(splits, current.iter().sum())
26+
(splits, timelines.iter().sum())
3827
}
3928

4029
pub fn part1(input: &Input) -> u64 {

0 commit comments

Comments
 (0)