64 lines
1.5 KiB
Rust
64 lines
1.5 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 = 0;
|
|
|
|
let mut state: isize = 50;
|
|
for rot in rotations.iter() {
|
|
match rot.direction {
|
|
Direction::Left => {
|
|
state = state.strict_sub_unsigned(rot.amount);
|
|
state = state.strict_rem_euclid(100);
|
|
}
|
|
Direction::Right => {
|
|
state = state.strict_add_unsigned(rot.amount);
|
|
state = state.strict_rem_euclid(100);
|
|
}
|
|
};
|
|
|
|
println!("Applying {:?} => {}", rot, state);
|
|
if state == 0 {
|
|
count += 1;
|
|
}
|
|
}
|
|
|
|
println!("Result: {}", count);
|
|
}
|