From cb3a816c79786e847b74a7d28646e7b191f06efb Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 8 Dec 2018 12:53:00 -0800 Subject: [PATCH] [2018][rust][05.2] --- 2018/rust/src/bin/day_05.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/2018/rust/src/bin/day_05.rs b/2018/rust/src/bin/day_05.rs index b0d0343..8953ebd 100644 --- a/2018/rust/src/bin/day_05.rs +++ b/2018/rust/src/bin/day_05.rs @@ -15,10 +15,27 @@ fn main() -> Result<(), Box> { } fn solve(input: &str) -> Result> { - let reaction = Reaction { - polymer: input.trim().into(), - }; - Ok(reaction.last().map(|x| x.len().to_string()).unwrap()) + let polymer = input.trim(); + + // Part One + // let reaction = Reaction { + // polymer: polymer.into() + // }; + // Ok(reaction.last().map(|x| x.len().to_string()).unwrap()) + + // Part Two + let output = (b'a'..=b'z') + .map(|x| x as char) + .map(|x| { + polymer + .replace(x, "") + .replace(&x.to_uppercase().to_string(), "") + }) + .map(|polymer| Reaction { polymer }) + .flat_map(|reaction| reaction.last()) + .map(|polymer| polymer.len()) + .min(); + Ok(output.unwrap().to_string()) } #[test] @@ -28,7 +45,10 @@ fn test_solve() { let output = solve(input).unwrap(); // Part One - assert_eq!(output, "10".to_string()); + // assert_eq!(output, "10".to_string()); + + // Part Two + assert_eq!(output, "4".to_string()); } struct Reaction {