From e31809a3b08aa7e2d5fca98282e2f949213f0ee0 Mon Sep 17 00:00:00 2001 From: timeshifter Date: Tue, 23 Jun 2026 19:03:41 +0200 Subject: [PATCH] add day 14 --- day14/Cargo.lock | 7 +++ day14/Cargo.toml | 6 ++ day14/input.txt | 9 +++ day14/src/main.rs | 144 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 day14/Cargo.lock create mode 100644 day14/Cargo.toml create mode 100644 day14/input.txt create mode 100644 day14/src/main.rs diff --git a/day14/Cargo.lock b/day14/Cargo.lock new file mode 100644 index 0000000..8513521 --- /dev/null +++ b/day14/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day14" +version = "0.1.0" diff --git a/day14/Cargo.toml b/day14/Cargo.toml new file mode 100644 index 0000000..86e034f --- /dev/null +++ b/day14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day14" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day14/input.txt b/day14/input.txt new file mode 100644 index 0000000..6cf5489 --- /dev/null +++ b/day14/input.txt @@ -0,0 +1,9 @@ +Dancer can fly 27 km/s for 5 seconds, but then must rest for 132 seconds. +Cupid can fly 22 km/s for 2 seconds, but then must rest for 41 seconds. +Rudolph can fly 11 km/s for 5 seconds, but then must rest for 48 seconds. +Donner can fly 28 km/s for 5 seconds, but then must rest for 134 seconds. +Dasher can fly 4 km/s for 16 seconds, but then must rest for 55 seconds. +Blitzen can fly 14 km/s for 3 seconds, but then must rest for 38 seconds. +Prancer can fly 3 km/s for 21 seconds, but then must rest for 40 seconds. +Comet can fly 18 km/s for 6 seconds, but then must rest for 103 seconds. +Vixen can fly 18 km/s for 5 seconds, but then must rest for 84 seconds. diff --git a/day14/src/main.rs b/day14/src/main.rs new file mode 100644 index 0000000..d47a925 --- /dev/null +++ b/day14/src/main.rs @@ -0,0 +1,144 @@ +use crate::State::{Flying, Resting}; + +fn main() { + let data: Vec<_> = std::fs::read_to_string("input.txt") + .unwrap() + .lines() + .map(Reindeer::parse) + .collect(); + + part1(&data); + part2(&data); +} + +fn part2(data: &[Reindeer]) { + let mut race = Race::new(data.to_vec()); + + while race.clock != 2503 { + race.tick(); + } + + let result = race + .participants + .iter() + .map(|p| p.reindeer.points) + .max() + .unwrap(); + println!("{result}"); +} + +fn part1(data: &[Reindeer]) { + let mut race = Race::new(data.to_vec()); + + while race.clock != 1000 { + race.tick(); + } + + let result = race + .participants + .iter() + .map(|p| p.reindeer.distance_travelled) + .max() + .unwrap(); + println!("{result}"); +} + +#[derive(Debug)] +struct Race { + pub participants: Vec, + pub clock: usize, +} + +impl Race { + fn new(r: Vec) -> Self { + let participants = r.into_iter().map(Participant::new).collect(); + + Self { + participants, + clock: 0, + } + } + + fn tick(&mut self) { + self.clock += 1; + for p in &mut self.participants { + if p.state == Flying { + p.reindeer.distance_travelled += p.reindeer.speed; + p.current_activity_time += 1; + if p.current_activity_time == p.reindeer.fly_time { + p.current_activity_time = 0; + p.state = Resting; + } + } else { + p.current_activity_time += 1; + if p.current_activity_time == p.reindeer.rest_time { + p.state = Flying; + p.current_activity_time = 0; + } + } + } + let max_distance = self + .participants + .iter() + .map(|p| p.reindeer.distance_travelled) + .max() + .unwrap(); + for p in &mut self.participants { + if p.reindeer.distance_travelled == max_distance { + p.reindeer.points += 1; + } + } + } +} + +#[derive(Debug)] +struct Participant { + pub reindeer: Reindeer, + pub state: State, + pub current_activity_time: usize, +} + +impl Participant { + fn new(r: Reindeer) -> Self { + Self { + reindeer: r, + state: State::Flying, + current_activity_time: 0, + } + } +} + +#[derive(Debug, PartialEq)] +enum State { + Flying, + Resting, +} + +#[derive(Debug, Clone)] +struct Reindeer { + pub speed: usize, + pub fly_time: usize, + pub rest_time: usize, + pub distance_travelled: usize, + pub points: usize, +} + +impl Reindeer { + fn parse(s: &str) -> Self { + let mut iter = s.split_whitespace(); + + let speed = iter.nth(3).unwrap().parse().unwrap(); + let fly_time = iter.nth(2).unwrap().parse().unwrap(); + let rest_time = iter.nth_back(1).unwrap().parse().unwrap(); + let distance_travelled = 0; + let points = 0; + + Self { + speed, + fly_time, + rest_time, + distance_travelled, + points, + } + } +}