Skip to content

Commit 5b199c0

Browse files
committed
Faster solution using a bubblesort approach
1 parent 77c163d commit 5b199c0

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7979
| --- | --- | --- | --: |
8080
| 1 | [Secret Entrance](https://adventofcode.com/2025/day/1) | [Source](src/year2025/day01.rs) | 25 |
8181
| 2 | [Gift Shop](https://adventofcode.com/2025/day/2) | [Source](src/year2025/day02.rs) | 1 |
82-
| 3 | [Lobby](https://adventofcode.com/2025/day/3) | [Source](src/year2025/day03.rs) | 60 |
82+
| 3 | [Lobby](https://adventofcode.com/2025/day/3) | [Source](src/year2025/day03.rs) | 18 |
8383

8484
## 2024
8585

src/year2025/day03.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
//! # Lobby
2-
pub fn parse(input: &str) -> Vec<&[u8]> {
3-
input.lines().map(str::as_bytes).collect()
2+
use std::mem::replace;
3+
4+
pub fn parse(input: &str) -> Vec<&str> {
5+
input.lines().collect()
46
}
57

6-
pub fn part1(input: &[&[u8]]) -> u64 {
7-
solve(input, 2)
8+
pub fn part1(input: &[&str]) -> u64 {
9+
solve::<2>(input)
810
}
911

10-
pub fn part2(input: &[&[u8]]) -> u64 {
11-
solve(input, 12)
12+
pub fn part2(input: &[&str]) -> u64 {
13+
solve::<12>(input)
1214
}
1315

14-
fn solve(banks: &[&[u8]], limit: usize) -> u64 {
15-
banks
16+
fn solve<const N: usize>(input: &[&str]) -> u64 {
17+
let mut batteries = [0; N];
18+
19+
input
1620
.iter()
1721
.map(|&bank| {
18-
let mut max = 0;
19-
let mut start = 0;
20-
21-
(0..limit).rev().fold(0, |joltage, digit| {
22-
let end = bank.len() - digit;
22+
let end = bank.len() - N;
23+
batteries.copy_from_slice(&bank.as_bytes()[end..]);
2324

24-
(max, start) = (start..end).fold((0, 0), |(max, start), i| {
25-
if bank[i] > max { (bank[i], i + 1) } else { (max, start) }
26-
});
25+
for mut next in bank[..end].bytes().rev() {
26+
for battery in &mut batteries {
27+
if next < *battery {
28+
break;
29+
}
30+
next = replace(battery, next);
31+
}
32+
}
2733

28-
10 * joltage + (max - b'0') as u64
29-
})
34+
batteries.iter().fold(0, |joltage, &b| 10 * joltage + (b - b'0') as u64)
3035
})
3136
.sum()
3237
}

0 commit comments

Comments
 (0)