Skip to content

Commit 01a91ec

Browse files
committed
2025 Day 3 (unclean version)
1 parent 7135d9e commit 01a91ec

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

src/bin/2025_03/main.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::collections::HashMap;
2+
3+
use aoc::common;
4+
5+
fn find(bank: &[u64], i: usize, l: usize, mp: &mut HashMap<(usize, usize), u64>) -> Option<u64> {
6+
if let Some(x) = mp.get(&(i, l)) {
7+
return Some(*x);
8+
}
9+
10+
if i >= bank.len() {
11+
return None;
12+
}
13+
14+
if l == 1 {
15+
let mx = (i..bank.len()).map(|x| bank[x]).max().unwrap();
16+
mp.insert((i, l), mx);
17+
return Some(mx);
18+
}
19+
20+
let x1 = if let Some(x) = find(bank, i + 1, l - 1, mp) {
21+
Some(x + 10u64.pow(l as u32 - 1) * bank[i])
22+
} else {
23+
None
24+
};
25+
26+
let x2 = if let Some(x) = find(bank, i + 1, l, mp) {
27+
Some(x)
28+
} else {
29+
None
30+
};
31+
//dbg!(i, l, x1, x2);
32+
if let Some(x) = x1 {
33+
if let Some(y) = x2 {
34+
mp.insert((i, l), x.max(y));
35+
return Some(x.max(y));
36+
} else {
37+
mp.insert((i, l), x);
38+
return Some(x);
39+
}
40+
} else {
41+
if let Some(y) = x2 {
42+
mp.insert((i, l), y);
43+
return Some(y);
44+
} else {
45+
return None;
46+
}
47+
}
48+
}
49+
50+
fn solve<const PART: usize>(input: &str) -> u64 {
51+
let mut ans = 0;
52+
for line in input.split("\n") {
53+
if line.is_empty() {
54+
continue;
55+
}
56+
let batteries = line
57+
.trim()
58+
.chars()
59+
.map(|c| c.to_digit(10).unwrap_or(0) as u64)
60+
.collect::<Vec<u64>>();
61+
// let mut mx = 0;
62+
// for (i, b1) in batteries.iter().enumerate() {
63+
// for b2 in batteries.iter().skip(i + 1) {
64+
// mx = mx.max(10 * b1 + b2);
65+
// }
66+
// }
67+
let mut mp: HashMap<(usize, usize), u64> = HashMap::new();
68+
if let Some(mx) = find(&batteries, 0, if PART == 1 { 2 } else { 12 }, &mut mp) {
69+
//dbg!(line, mx);
70+
ans = ans + mx;
71+
}
72+
}
73+
ans
74+
}
75+
76+
fn main() {
77+
if let Some(input) = common::get_input() {
78+
common::timed(&input, solve::<1>, true);
79+
common::timed(&input, solve::<2>, true);
80+
}
81+
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_samples() {
89+
let sample_input = "987654321111111\n811111111111119\n234234234234278\n818181911112111";
90+
assert_eq!(solve::<1>(sample_input), 357);
91+
assert_eq!(solve::<2>(sample_input), 3121910778619);
92+
}
93+
}

0 commit comments

Comments
 (0)