Files
aoc/2025/01.rs
2026-01-08 19:42:42 +01:00

82 lines
1.9 KiB
Rust

---cargo
---
static CONTENT: &'static str = include_str!("./inputs/01_1.txt");
#[derive(Debug)]
enum Direction {
Left,
Right,
}
#[derive(Debug)]
struct Rotation {
direction: Direction,
amount: usize,
}
fn main() {
let rotations: Vec<Rotation> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| {
let (direction, raw_amount) = l.split_at(1);
let direction = match direction {
"L" => Direction::Left,
"R" => Direction::Right,
other => unreachable!("Unknown Direction: {:?}", other),
};
let amount = match raw_amount.parse::<usize>() {
Ok(a) => a,
Err(e) => unreachable!("Invalid Amount ({:?}): {:?}", raw_amount, e),
};
Rotation {
direction,
amount,
}
}).collect();
println!("Rotations: {:#?}", rotations);
let mut count: usize = 0;
let mut state: isize = 50;
for rot in rotations.iter() {
if rot.amount >= 100 {
count += rot.amount / 100;
}
let amount = rot.amount % 100;
if amount == 0 {
continue;
}
match rot.direction {
Direction::Left => {
if state != 0 && amount > state as usize {
count += 1;
}
state = state.strict_sub_unsigned(amount);
state = state.strict_rem_euclid(100);
if state == 0 {
count += 1;
}
}
Direction::Right => {
state = state.strict_add_unsigned(amount);
if state >= 100 {
count += 1;
}
state = state.strict_rem_euclid(100);
}
};
println!("Applying {:?} => {} ({})", rot, state, count);
}
println!("Result: {}", count);
}