1
0
Fork 0

day06 solution

main
Andrew Coleman 2023-12-06 10:01:10 -05:00
parent b750d99261
commit da50cbd57b
2 changed files with 94 additions and 0 deletions

93
2023/src/day06.rs Normal file
View File

@ -0,0 +1,93 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug)]
struct Race {
time: u64,
distance: u64,
}
impl Race {
fn winning_times(&self) -> Vec<u64> {
let mut result: Vec<u64> = vec![];
for t in 0..=self.time {
if t * (self.time - t) > self.distance {
result.push(t);
}
}
result
}
}
#[aoc_generator(day6)]
fn parse(input: &str) -> Vec<Race> {
let mut lines = input.lines();
let times: Vec<u64> = lines.next().unwrap().split(": ").collect::<Vec<&str>>()[1]
.trim_start()
.split(" ")
.filter_map(|t| t.parse::<u64>().ok())
.collect();
let distances: Vec<u64> = lines.next().unwrap().split(": ").collect::<Vec<&str>>()[1]
.trim_start()
.split(" ")
.filter_map(|t| t.parse::<u64>().ok())
.collect();
assert_eq!(times.len(), distances.len());
(0..times.len())
.map(|i| Race {
time: times[i],
distance: distances[i],
})
.collect()
}
#[aoc(day6, part1)]
fn part1(input: &[Race]) -> u64 {
input
.iter()
.map(|race| race.winning_times().len() as u64)
.collect::<Vec<u64>>()
.iter()
.product()
}
#[aoc(day6, part2)]
fn part2(input: &[Race]) -> u64 {
let time = input
.iter()
.map(|race| race.time)
.collect::<Vec<u64>>()
.iter()
.map(|t| t.to_string())
.collect::<Vec<String>>()
.join("")
.parse::<u64>()
.unwrap();
let distance = input
.iter()
.map(|race| race.distance)
.collect::<Vec<u64>>()
.iter()
.map(|t| t.to_string())
.collect::<Vec<String>>()
.join("")
.parse::<u64>()
.unwrap();
let race = Race { time, distance };
race.winning_times().len() as u64
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_DATA: &'static str = r#"Time: 7 15 30
Distance: 9 40 200
"#;
#[test]
fn sample_data() {
let races = parse(&SAMPLE_DATA);
assert_eq!(part1(&races), 288);
assert_eq!(part2(&races), 71503);
}
}

View File

@ -5,5 +5,6 @@ mod day02;
mod day03;
mod day04;
mod day05;
mod day06;
aoc_lib! { year = 2023 }