diff --git a/rust/src/day2.rs b/rust/src/day2.rs index f417d53..4d53d9a 100644 --- a/rust/src/day2.rs +++ b/rust/src/day2.rs @@ -95,9 +95,8 @@ impl FromStr for Revelation { type Err = String; fn from_str(s: &str) -> Result { - let revelation_results: Vec<_> = s.split(", ").map(ColourRevelation::from_str).collect(); - let revelations_result = traverse_results(revelation_results); - revelations_result.and_then(|revelations| { + let revelation_result: Result, _> = s.split(", ").map(ColourRevelation::from_str).collect(); + revelation_result.and_then(|revelations| { let reds = extract_colour(revelations.clone(), String::from("red")); let greens = extract_colour(revelations.clone(), String::from("green")); let blues = extract_colour(revelations, String::from("blue")); @@ -135,10 +134,10 @@ impl FromStr for Game { .replace("Game ", "") .parse() .map_err(|e| format!("Invalid game id: {}", e)); - let revelations_results: Vec> = + let revelations_result: Result, _> = revelations_str.split("; ").map(|x| x.parse()).collect(); game_id_result.and_then(|id| { - traverse_results(revelations_results) + revelations_result .and_then(|revelations| Ok(Game { id, revelations })) }) } @@ -148,23 +147,5 @@ impl FromStr for Game { } fn parse_games(s: &str) -> Result, String> { - traverse_results(s.split('\n').map(Game::from_str).collect()) -} - -// Me: Mom, can we have travese. -// Mom: We got traverse at home. -// Traverse at home: -fn traverse_results(results: Vec>) -> Result, B> -where - Vec: Clone, -{ - results.into_iter().fold(Ok(Vec::new()), |acc, res| { - acc.and_then(|vec| { - res.and_then(|r| { - let mut vectemp = vec.clone(); - vectemp.push(r); - Ok(vectemp) - }) - }) - }) + s.split('\n').map(Game::from_str).collect() }