puzzles/advent_of_code_2025/src/day1/part2.rs

44 lines
No EOL
1.1 KiB
Rust

pub fn solve(input: &str) {
let instructions = input.lines().map(|str| {
let distance = unsafe { str::from_utf8_unchecked( &str.bytes().collect::<Vec<_>>()[1..]).parse::<i32>().unwrap() };
if str.bytes().next().unwrap() == b'L' {
-distance
} else {
distance
}
}).collect::<Vec<_>>();
let mut counter = 0;
let mut dial = 50;
println!("instr min: {}, max: {}", instructions.iter().min().unwrap(), instructions.iter().max().unwrap());
for instruction in instructions {
// let old = dial;
// let new = dial + instruction;
dial += instruction;
match dial {
..=-1 => {
counter += dial / -100;
dial += 100 * (dial / -100); },
100.. => {
counter += dial / 100;
dial %= 100;
},
_ => {}
}
// println!("{old} -> {new} = {dial}");
if dial == 0 {
// println!("incremented!");
counter += 1;
}
}
println!("counter: {}", counter);
}