Complete 2023 day 6

This commit is contained in:
Timothy Warren 2023-12-14 14:04:53 -05:00
parent 8d65055a2a
commit a28411f503

View File

@ -65,7 +65,8 @@ impl Races {
.nth(1) .nth(1)
.unwrap() .unwrap()
.replace(' ', "") .replace(' ', "")
.parse::<usize>().unwrap(); .parse::<usize>()
.unwrap();
let distance = lines let distance = lines
.next() .next()
@ -73,10 +74,11 @@ impl Races {
.nth(1) .nth(1)
.unwrap() .unwrap()
.replace(' ', "") .replace(' ', "")
.parse::<usize>().unwrap(); .parse::<usize>()
.unwrap();
Races { Races {
records: Vec::from([RaceRecord { time, distance}]) records: Vec::from([RaceRecord { time, distance }]),
} }
} }
@ -87,6 +89,10 @@ impl Races {
.collect() .collect()
} }
fn calculate_win_count(&self) -> usize {
winning_possibilities(self.records[0]).len()
}
fn calculate_possible_win_product(&self) -> usize { fn calculate_possible_win_product(&self) -> usize {
self.calculate_winning_counts() self.calculate_winning_counts()
.into_iter() .into_iter()
@ -113,11 +119,24 @@ fn winning_possibilities(race: RaceRecord) -> Vec<usize> {
fn part_one() { fn part_one() {
let records = Races::parse(FILE_STR); let records = Races::parse(FILE_STR);
println!("Part 1 Product of winning possibilitiesL {}", records.calculate_possible_win_product()); println!(
"Part 1 Product of winning possibilities: {}",
records.calculate_possible_win_product()
);
}
fn part_two() {
let record = Races::parse_single(FILE_STR);
println!("{:#?}", record);
println!(
"Part 2 winning possibilities: {}",
record.calculate_win_count()
);
} }
fn main() { fn main() {
part_one(); part_one();
part_two();
} }
#[cfg(test)] #[cfg(test)]
@ -137,4 +156,10 @@ mod tests {
let races = Races::parse(EXAMPLE_FILE_STR); let races = Races::parse(EXAMPLE_FILE_STR);
assert_eq!(288, races.calculate_possible_win_product()); assert_eq!(288, races.calculate_possible_win_product());
} }
#[test]
fn test_calculate_win_count() {
let races = Races::parse_single(EXAMPLE_FILE_STR);
assert_eq!(71503, races.calculate_win_count());
}
} }