Day 6 Complete :) Super easy day!

This commit is contained in:
Luke Else 2023-12-06 12:40:36 +00:00
parent c1668c8397
commit c4c423871e
6 changed files with 65 additions and 10 deletions

View File

@ -0,0 +1,2 @@
Time: 59 70 78 78
Distance: 430 1218 1213 1276

View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

View File

@ -13,7 +13,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Box::new(day03::Day03 {}), Box::new(day03::Day03 {}),
Box::new(day04::Day04 {}), Box::new(day04::Day04 {}),
Box::new(day05::Day05 {}), Box::new(day05::Day05 {}),
// Box::new(day06::Day06 {}), Box::new(day06::Day06 {}),
// Box::new(day07::Day07 {}), // Box::new(day07::Day07 {}),
// Box::new(day08::Day08 {}), // Box::new(day08::Day08 {}),
// Box::new(day09::Day09 {}), // Box::new(day09::Day09 {}),
@ -58,6 +58,5 @@ async fn main() -> Result<(), Box<dyn Error>> {
} }
println!("Ran AoC 2023 in {}ms", SystemTime::now().duration_since(start_time)?.as_millis()); println!("Ran AoC 2023 in {}ms", SystemTime::now().duration_since(start_time)?.as_millis());
Ok(()) Ok(())
} }

View File

@ -5,16 +5,42 @@ pub struct Day06 {}
impl Solution for Day06 { impl Solution for Day06 {
fn part1( fn part1(
&self, &self,
_input: &mut Vec<String>, input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready")) let times: Vec<u64> = input[0]
.split_whitespace()
.skip(1)
.map(|t| t.parse().unwrap())
.collect();
let distances: Vec<u64> = input[1]
.split_whitespace()
.skip(1)
.map(|d| d.parse().unwrap())
.collect();
Ok(Box::new(self.num_winning_races(&times, &distances)?))
} }
fn part2( fn part2(
&self, &self,
_input: &mut Vec<String>, input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready")) let times = vec![input[0]
.split(":")
.nth(1)
.unwrap()
.replace(" ", "")
.parse::<u64>()?];
let distances = vec![input[1]
.split(":")
.nth(1)
.unwrap()
.replace(" ", "")
.parse::<u64>()?];
Ok(Box::new(self.num_winning_races(&times, &distances)?))
} }
fn get_day(&self) -> u8 { fn get_day(&self) -> u8 {
@ -22,7 +48,27 @@ impl Solution for Day06 {
} }
} }
impl Day06 {} impl Day06 {
fn num_winning_races(
&self,
times: &Vec<u64>,
distances: &Vec<u64>,
) -> Result<u32, Box<dyn std::error::Error>> {
let mut beats = vec![];
for (time, record) in times.iter().zip(distances.iter()) {
let mut count: u32 = 0;
for hold in 0..*time {
let dist = (hold) * (time - hold);
if dist > *record {
count += 1;
}
}
beats.push(count);
}
Ok(beats.iter().fold(1, |acc, &b| acc * b))
}
}
/// Test from puzzle input /// Test from puzzle input
#[cfg(test)] #[cfg(test)]
@ -44,7 +90,7 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "288");
} }
#[test] #[test]
@ -61,6 +107,6 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "71503");
} }
} }

View File

@ -34,6 +34,10 @@ pub trait Solution {
fn run(&self) -> Result<Run, Box<dyn std::error::Error>> { fn run(&self) -> Result<Run, Box<dyn std::error::Error>> {
let start_time = SystemTime::now(); let start_time = SystemTime::now();
self.part1(get_input(self.get_day(), utils::InputType::Test1)?.as_mut())?;
self.part2(get_input(self.get_day(), utils::InputType::Test2)?.as_mut())?;
let run = Run { let run = Run {
part1: self.part1(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?, part1: self.part1(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?,
part2: self.part2(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?, part2: self.part2(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?,