Files
aoc/2025/03.rs
2026-01-08 23:47:10 +01:00

78 lines
2.0 KiB
Rust

---cargo
---
static CONTENT: &'static str = include_str!("./inputs/03_1.txt");
#[derive(Debug)]
struct Bank {
values: [Option<(usize, usize)>; 10],
}
fn main() {
let raw_banks: Vec<Vec<u8>> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| {
l.chars().map(|c| {
match c {
'0' => 0,
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
c => unreachable!("Other {:?}", c),
}
}).collect::<Vec<u8>>()
}).collect();
println!("{:?}", raw_banks);
let mut result = 0usize;
for bank in raw_banks.iter() {
println!("{:?}", bank);
let mut bank_iter = bank.iter();
let mut digits: Vec<_> = (&mut bank_iter).take(12).collect();
println!("Starting Digits {:?}", digits);
for other in bank_iter {
// println!("Looking to add {}", other);
let to_remove = digits.windows(2).enumerate().find_map(|(idx, window)| {
if window[0] >= window[1] {
return None;
}
Some(idx)
});
// dbg!(to_remove);
match to_remove {
Some(to_remove) => {
digits.remove(to_remove);
digits.push(other);
}
None => {
if digits.last().copied().unwrap() < other {
digits.pop();
digits.push(other);
}
}
};
}
println!("Ending Digits {:?}", digits);
let bank_value = digits.iter().copied().rev().enumerate().fold(0usize, |acc, (idx, v)| {
acc + (*v as usize * 10usize.pow(idx as u32))
});
println!("Value {:?}", bank_value);
result += bank_value;
}
println!("Result: {:?}", result);
}