y2015d14
This commit is contained in:
parent
b55df3d448
commit
15be81c564
@ -10,11 +10,11 @@ fn main() {
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/14_input.txt")).unwrap();
|
||||
println!("{}", d14::process_part1(&content));
|
||||
println!("{}", d14::process_part1(&content, 2503));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/14_input.txt")).unwrap();
|
||||
println!("{}", d14::process_part2(&content));
|
||||
println!("{}", d14::process_part2(&content, 2503));
|
||||
}
|
||||
|
@ -1,37 +1,125 @@
|
||||
pub fn process_part1(input: &str) -> i32 {
|
||||
0
|
||||
use std::fmt::Display;
|
||||
|
||||
pub fn process_part1(input: &str, time: u32) -> u32 {
|
||||
let mut reindeer_stats = vec![];
|
||||
input.lines().for_each(|line| {
|
||||
let words = line.split_whitespace().collect::<Vec<&str>>();
|
||||
let name = words[0].to_string();
|
||||
let speed = words[3].parse::<u32>().unwrap();
|
||||
let travel_time = words[6].parse::<u32>().unwrap();
|
||||
let rest_time = words[13].parse::<u32>().unwrap();
|
||||
let reindeer = Reindeer {
|
||||
name,
|
||||
speed,
|
||||
travel_time,
|
||||
rest_time,
|
||||
score: 0,
|
||||
distance: 0,
|
||||
};
|
||||
reindeer_stats.push(reindeer.distance_traveled(time));
|
||||
});
|
||||
*reindeer_stats.iter().max().unwrap()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i32 {
|
||||
0
|
||||
#[derive(Debug, Clone)]
|
||||
struct Reindeer {
|
||||
name: String,
|
||||
speed: u32,
|
||||
travel_time: u32,
|
||||
rest_time: u32,
|
||||
score: u32,
|
||||
distance: u32,
|
||||
}
|
||||
|
||||
impl Display for Reindeer {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let string = format!(
|
||||
"{}: Speed {} for {}, rest {}",
|
||||
self.name, self.speed, self.travel_time, self.rest_time
|
||||
);
|
||||
writeln!(f, "{string}")
|
||||
}
|
||||
}
|
||||
|
||||
impl Reindeer {
|
||||
fn distance_traveled(&self, seconds: u32) -> u32 {
|
||||
let timeframe = self.travel_time + self.rest_time;
|
||||
let mut traveled = (seconds / timeframe) * self.speed * self.travel_time;
|
||||
let last_timeframe = seconds % timeframe;
|
||||
if last_timeframe >= self.travel_time {
|
||||
traveled += self.speed * self.travel_time;
|
||||
} else {
|
||||
traveled += self.speed * last_timeframe;
|
||||
}
|
||||
traveled
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str, time: u32) -> u32 {
|
||||
let mut reindeer_stats = vec![];
|
||||
input.lines().for_each(|line| {
|
||||
let words = line.split_whitespace().collect::<Vec<&str>>();
|
||||
let name = words[0].to_string();
|
||||
let speed = words[3].parse::<u32>().unwrap();
|
||||
let travel_time = words[6].parse::<u32>().unwrap();
|
||||
let rest_time = words[13].parse::<u32>().unwrap();
|
||||
let reindeer = Reindeer {
|
||||
name,
|
||||
speed,
|
||||
travel_time,
|
||||
rest_time,
|
||||
score: 0,
|
||||
distance: 0,
|
||||
};
|
||||
reindeer_stats.push(reindeer);
|
||||
});
|
||||
for second in 1..=time {
|
||||
let mut furthest = (String::new(), 0);
|
||||
let mut reindeers = reindeer_stats
|
||||
.iter()
|
||||
.map(|reindeer| {
|
||||
let reindeer = reindeer.clone();
|
||||
Reindeer {
|
||||
distance: reindeer.distance_traveled(second),
|
||||
..reindeer
|
||||
}
|
||||
})
|
||||
.collect::<Vec<Reindeer>>();
|
||||
reindeers.iter().for_each(|reindeer| {
|
||||
if reindeer.distance >= furthest.1 {
|
||||
furthest = (reindeer.name.clone(), reindeer.distance);
|
||||
}
|
||||
});
|
||||
reindeers.iter_mut().for_each(|reindeer| {
|
||||
if reindeer.name == furthest.0 {
|
||||
reindeer.score += 1;
|
||||
}
|
||||
});
|
||||
reindeer_stats = reindeers;
|
||||
}
|
||||
reindeer_stats
|
||||
.iter()
|
||||
.map(|reindeer| reindeer.score)
|
||||
.max()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "Alice would gain 54 happiness units by sitting next to Bob.
|
||||
Alice would lose 79 happiness units by sitting next to Carol.
|
||||
Alice would lose 2 happiness units by sitting next to David.
|
||||
Bob would gain 83 happiness units by sitting next to Alice.
|
||||
Bob would lose 7 happiness units by sitting next to Carol.
|
||||
Bob would lose 63 happiness units by sitting next to David.
|
||||
Carol would lose 62 happiness units by sitting next to Alice.
|
||||
Carol would gain 60 happiness units by sitting next to Bob.
|
||||
Carol would gain 55 happiness units by sitting next to David.
|
||||
David would gain 46 happiness units by sitting next to Alice.
|
||||
David would lose 7 happiness units by sitting next to Bob.
|
||||
David would gain 41 happiness units by sitting next to Carol.";
|
||||
const INPUT: &str = "Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
|
||||
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 330);
|
||||
let result = process_part1(INPUT, 1000);
|
||||
assert_eq!(result, 1120);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, 0);
|
||||
let result = process_part2(INPUT, 1000);
|
||||
assert_eq!(result, 689);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user