Compare commits
3 Commits
main
...
4347fb2131
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4347fb2131 | ||
|
|
c7454a0202 | ||
|
|
44335ca03c |
81
2025/01.rs
Normal file
81
2025/01.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
---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);
|
||||
}
|
||||
4604
2025/inputs/01_1.txt
Normal file
4604
2025/inputs/01_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
10
2025/inputs/01_example.txt
Normal file
10
2025/inputs/01_example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
Reference in New Issue
Block a user