Skip to content

Commit f8f9874

Browse files
committed
speedup 2025-03 slightly
1 parent 0857c28 commit f8f9874

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

aoc2025_wasm/src/impl_2025_03.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,25 @@ fn calculate_one<const DIGITS: usize>(line: &[u64]) -> Result<u64> {
2727
Ok(ans)
2828
}
2929

30-
fn calculate<const DIGITS: usize>(inp: &str) -> Result<u64> {
30+
fn calculate(inp: &str) -> Result<(u64, u64)> {
3131
inp.lines()
32-
.map(|line| {
32+
.filter_map(|line| {
3333
let bytes = line
3434
.bytes()
35-
.map(|c| {
36-
c.checked_sub(b'0')
37-
.context("invalid character")
38-
.map(|c| c as u64)
39-
})
40-
.collect::<Result<Vec<_>>>()?;
35+
.map(|c| c.checked_sub(b'0').map(|c| c as u64))
36+
.collect::<Option<Vec<_>>>()?;
4137

42-
calculate_one::<DIGITS>(&bytes)
38+
Some((
39+
calculate_one::<2>(&bytes).ok()?,
40+
calculate_one::<12>(&bytes).ok()?,
41+
))
4342
})
44-
.sum::<Result<u64>>()
43+
.reduce(|a, b| (a.0 + b.0, a.1 + b.1))
44+
.context("no valid lines?")
4545
}
4646

4747
pub fn run_2025_03(inp: &str) -> Result<String> {
48-
let p1 = calculate::<2>(inp)?;
49-
let p2 = calculate::<12>(inp)?;
48+
let (p1, p2) = calculate(inp)?;
5049
Ok(format!("{p1}\n{p2}"))
5150
}
5251

@@ -59,21 +58,21 @@ mod tests {
5958

6059
#[test]
6160
fn test_example_p1() {
62-
assert_eq!(calculate::<2>(EXAMPLE_DATA).unwrap(), 357);
61+
assert_eq!(calculate(EXAMPLE_DATA).unwrap().0, 357);
6362
}
6463

6564
#[test]
6665
fn test_example_p2() {
67-
assert_eq!(calculate::<12>(EXAMPLE_DATA).unwrap(), 3121910778619);
66+
assert_eq!(calculate(EXAMPLE_DATA).unwrap().1, 3121910778619);
6867
}
6968

7069
#[test]
7170
fn test_real_p1() {
72-
assert_eq!(calculate::<2>(REAL_DATA).unwrap(), 17107);
71+
assert_eq!(calculate(REAL_DATA).unwrap().0, 17107);
7372
}
7473

7574
#[test]
7675
fn test_real_p2() {
77-
assert_eq!(calculate::<12>(REAL_DATA).unwrap(), 169349762274117);
76+
assert_eq!(calculate(REAL_DATA).unwrap().1, 169349762274117);
7877
}
7978
}

0 commit comments

Comments
 (0)