add day 14
This commit is contained in:
Generated
+7
@@ -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"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day14"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -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.
|
||||||
@@ -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<Participant>,
|
||||||
|
pub clock: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Race {
|
||||||
|
fn new(r: Vec<Reindeer>) -> 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user