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)
.unwrap()
.replace(' ', "")
.parse::<usize>().unwrap();
.parse::<usize>()
.unwrap();
let distance = lines
.next()
@ -73,10 +74,11 @@ impl Races {
.nth(1)
.unwrap()
.replace(' ', "")
.parse::<usize>().unwrap();
.parse::<usize>()
.unwrap();
Races {
records: Vec::from([RaceRecord { time, distance}])
records: Vec::from([RaceRecord { time, distance }]),
}
}
@ -87,6 +89,10 @@ impl Races {
.collect()
}
fn calculate_win_count(&self) -> usize {
winning_possibilities(self.records[0]).len()
}
fn calculate_possible_win_product(&self) -> usize {
self.calculate_winning_counts()
.into_iter()
@ -113,11 +119,24 @@ fn winning_possibilities(race: RaceRecord) -> Vec<usize> {
fn part_one() {
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() {
part_one();
part_two();
}
#[cfg(test)]
@ -137,4 +156,10 @@ mod tests {
let races = Races::parse(EXAMPLE_FILE_STR);
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());
}
}