Implement 2025 - Day 1 - Part 1
This commit is contained in:
63
2025/01.rs
Normal file
63
2025/01.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
---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);
|
||||
}
|
||||
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