From c568871f4d8e63087a7fa4a3c28fb15e0f30615f Mon Sep 17 00:00:00 2001 From: timeshifter Date: Thu, 2 Jul 2026 13:06:20 +0200 Subject: [PATCH] add day 15 --- day15/Cargo.lock | 7 ++++ day15/Cargo.toml | 6 +++ day15/example.txt | 2 + day15/input.txt | 6 +++ day15/src/main.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 day15/Cargo.lock create mode 100644 day15/Cargo.toml create mode 100644 day15/example.txt create mode 100644 day15/input.txt create mode 100644 day15/src/main.rs diff --git a/day15/Cargo.lock b/day15/Cargo.lock new file mode 100644 index 0000000..a6ac8d2 --- /dev/null +++ b/day15/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day15" +version = "0.1.0" diff --git a/day15/Cargo.toml b/day15/Cargo.toml new file mode 100644 index 0000000..c787d54 --- /dev/null +++ b/day15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day15" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day15/example.txt b/day15/example.txt new file mode 100644 index 0000000..8a94234 --- /dev/null +++ b/day15/example.txt @@ -0,0 +1,2 @@ +Disc #1 has 5 positions; at time=0, it is at position 4. +Disc #2 has 2 positions; at time=0, it is at position 1. diff --git a/day15/input.txt b/day15/input.txt new file mode 100644 index 0000000..1f25755 --- /dev/null +++ b/day15/input.txt @@ -0,0 +1,6 @@ +Disc #1 has 13 positions; at time=0, it is at position 1. +Disc #2 has 19 positions; at time=0, it is at position 10. +Disc #3 has 3 positions; at time=0, it is at position 2. +Disc #4 has 7 positions; at time=0, it is at position 1. +Disc #5 has 5 positions; at time=0, it is at position 3. +Disc #6 has 17 positions; at time=0, it is at position 5. diff --git a/day15/src/main.rs b/day15/src/main.rs new file mode 100644 index 0000000..739c65f --- /dev/null +++ b/day15/src/main.rs @@ -0,0 +1,99 @@ +#[cfg(debug_assertions)] +const FILENAME: &str = "example.txt"; +#[cfg(not(debug_assertions))] +const FILENAME: &str = "input.txt"; + +fn main() { + let mut discs: Vec<_> = std::fs::read_to_string(FILENAME) + .unwrap() + .lines() + .map(Disc::parse) + .collect(); + + part_1(&discs); + + discs.push(Disc { + positions: 11, + current_position: 0, + }); + + part_1(&discs); +} + +fn part_1(discs: &[Disc]) { + let mut initial_state = Simulation::new(discs); + + let mut ball_drop_time = 0; + + loop { + let mut simulation = initial_state.clone(); + + let success = simulation.run(); + if success { + break; + } + ball_drop_time += 1; + initial_state.advance_discs(); + } + println!("{ball_drop_time}") +} + +#[derive(Clone, Debug)] +struct Simulation { + discs: Vec, +} + +impl Simulation { + fn new(discs: &[Disc]) -> Self { + Self { + discs: discs.to_vec(), + } + } + + fn run(&mut self) -> bool { + let mut ball = 0; + loop { + ball += 1; + self.advance_discs(); + + if let Some(disc) = self.discs.get(ball - 1) { + if disc.current_position != 0 { + return false; + } + } else { + return true; + } + } + } + + fn advance_discs(&mut self) { + self.discs.iter_mut().for_each(|disc| disc.advance()); + } +} + +#[derive(Debug, Clone)] +struct Disc { + positions: usize, + current_position: usize, +} + +impl Disc { + fn parse(s: &str) -> Self { + let mut iter = s.split_whitespace(); + let positions = iter.nth(3).unwrap().parse().unwrap(); + + let mut last_iter = iter.next_back().unwrap().chars(); + last_iter.next_back(); + let current_position = last_iter.collect::().parse().unwrap(); + + Self { + positions, + current_position, + } + } + + fn advance(&mut self) { + self.current_position += 1; + self.current_position %= self.positions; + } +}