Day 6 Complete :) Super easy day!
This commit is contained in:
@@ -5,16 +5,42 @@ pub struct Day06 {}
|
||||
impl Solution for Day06 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> 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(×, &distances)?))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> 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(×, &distances)?))
|
||||
}
|
||||
|
||||
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
|
||||
#[cfg(test)]
|
||||
@@ -44,7 +90,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "288");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -61,6 +107,6 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "71503");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user