This commit is contained in:
Olivier David Laplante
2025-12-01 21:09:27 -05:00
parent 84e553f4c7
commit eb616ab44b
6 changed files with 41 additions and 16 deletions

View File

@@ -1,15 +1,18 @@
pub fn run(input: &str) { pub fn run(input: &str) {
let step_list = input.lines() let step_list = input.lines().map(|l| {
.map(|l| l.replace("L", "").replace("R", "-").parse::<i32>().unwrap()); l.split_at(1).1.parse::<i32>().unwrap() * if l.split_at(1).0 == "R" { 1 } else { -1 }
let counted = step_list.scan(50, |dial, steps| {
let at_0 = (*dial..(*dial + steps))
.chain((*dial + steps + 1)..=*dial)
.filter(|num| *num % 100 == 0).count();
*dial += steps;
Some((*dial % 100 == 0, at_0))
}); });
println!("Part 1: {}", counted.clone().filter(|(is_0, _)| *is_0).count()); let counted = step_list.fold((50, 0, 0), |track, steps| {
println!("Part 2: {}", counted.fold(0, |acc, num| acc + num.1)); let at_0 = (track.0..(track.0 + steps))
.chain((track.0 + steps + 1)..=track.0)
.filter(|num| *num % 100 == 0)
.count();
(
track.0 + steps,
track.1 + if (track.0 + steps) % 100 == 0 { 1 } else { 0 },
track.2 + at_0,
)
});
println!("Part 1: {}", counted.1);
println!("Part 2: {}", counted.2);
} }

1
src/day2.rs Normal file
View File

@@ -0,0 +1 @@
pub fn run(_input: &str) {}

0
src/inputs/day2.txt Normal file
View File

10
src/inputs/test1.txt Normal file
View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

0
src/inputs/test2.txt Normal file
View File

View File

@@ -1,19 +1,30 @@
use std::time::Instant;
use dialoguer::Select; use dialoguer::Select;
mod utils;
mod day1; mod day1;
mod day2;
mod utils;
const CHOICES: [&str; 2] = [ const CHOICES: [&str; 5] = [
"Exit", "Exit",
"Day 1: Secret Entrance", "Day 1: Secret Entrance",
"Day 1 (testing): Secret Entrance",
"Day 2: ",
"Day 2 (testing): ",
]; ];
fn main() { fn main() {
while let Some(choice) = get_input() { while let Some(choice) = get_input() {
let start = Instant::now();
match choice { match choice {
1 => day1::run(include_str!("inputs/day1.txt")), 1 => day1::run(include_str!("inputs/day1.txt")),
_ => println!("Invalid selection") 2 => day1::run(include_str!("inputs/test1.txt")),
3 => day2::run(include_str!("inputs/day2.txt")),
4 => day2::run(include_str!("inputs/test2.txt")),
_ => println!("Invalid selection"),
} }
println!("Execution time: {:?}", start.elapsed())
} }
} }
@@ -27,6 +38,6 @@ fn get_input() -> Option<u32> {
match selection { match selection {
0 => None, 0 => None,
_ => Some(selection) _ => Some(selection),
} }
} }