Skip to content

Commit

Permalink
day 6 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
madser123 committed Jun 24, 2024
1 parent 19696f6 commit e137fa3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ fn day_6() {
println!("# Day 6");

let input = get_input!("day6");
let races = Races::from_multiple_races(&input);
let races = Races::from_multiple_races(&input).expect("Failed to parse races");
let winning_product = races.get_winning_product();

println!("Winning product for multiple races: {winning_product}");

let race = Races::from_single_race(&input);
let race = Races::from_single_race(&input).expect("Failed to parse race");
let winning_product = race.get_winning_product();

println!("Winning product for single race: {winning_product}");
Expand Down
37 changes: 23 additions & 14 deletions lib/boat_race/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use std::num::ParseIntError;

#[derive(Debug)]
pub enum ParseRaceError {
ParseInt(ParseIntError),
Invalid(String),
}

pub struct Race {
time: u64,
distance: u64,
Expand Down Expand Up @@ -64,46 +70,45 @@ impl Races {
self.races.iter().map(Race::find_winning_conditions_amount).product()
}

pub fn from_single_race(races: &str) -> Self {
pub fn from_single_race(races: &str) -> Result<Self, ParseRaceError> {
let numbers = races
.lines()
.map(|line| {
line.split(':')
.last()
.expect("Failed to get numbers")
.ok_or_else(|| ParseRaceError::Invalid("Failed to get numbers".to_string()))?
.split_ascii_whitespace()
.collect::<String>()
.parse::<u64>()
.expect("Failed to parse number")
.map_err(ParseRaceError::ParseInt)
})
.collect::<Vec<u64>>();
.collect::<Result<Vec<u64>, ParseRaceError>>()?;

let races = vec![Race::new(numbers[0], numbers[1])];

Self { races }
Ok(Self { races })
}

pub fn from_multiple_races(races: &str) -> Self {
pub fn from_multiple_races(races: &str) -> Result<Self, ParseRaceError> {
let numbers = races
.lines()
.map(|line| {
line.split(':')
.last()
.expect("Failed to get numbers")
.ok_or_else(|| ParseRaceError::Invalid("Failed to get numbers".to_string()))?
.split_ascii_whitespace()
.map(|n| n.parse::<u64>())
.collect::<Result<Vec<u64>, ParseIntError>>()
.expect("Failed to parse numbers")
.map(|n| n.parse::<u64>().map_err(ParseRaceError::ParseInt))
.collect::<Result<Vec<u64>, ParseRaceError>>()
})
.collect::<Vec<Vec<u64>>>();
.collect::<Result<Vec<Vec<u64>>, ParseRaceError>>()?;

let races = numbers[0]
.iter()
.zip(numbers[1].iter())
.map(|x| Race::new(*x.0, *x.1))
.collect::<Vec<Race>>();

Self { races }
Ok(Self { races })
}
}

Expand All @@ -116,13 +121,17 @@ Distance: 9 40 200";

#[test]
fn solution_1() {
let product = Races::from_multiple_races(EXAMPLE).get_winning_product();
let product = Races::from_multiple_races(EXAMPLE)
.expect("Failed to parse races")
.get_winning_product();
assert_eq!(product, 288)
}

#[test]
fn solution_2() {
let product = Races::from_single_race(EXAMPLE).get_winning_product();
let product = Races::from_single_race(EXAMPLE)
.expect("Failed to parse race")
.get_winning_product();
assert_eq!(product, 71503);
}
}

0 comments on commit e137fa3

Please sign in to comment.