From 26ac38d39fcd798ce1f4afa61e76c4c7edddb37d Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 2 Dec 2017 11:30:09 -0800 Subject: [PATCH] [2017][rust][1.0] refactor --- 2017/rust/src/day_01.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/2017/rust/src/day_01.rs b/2017/rust/src/day_01.rs index 389e8e3..12b3a14 100644 --- a/2017/rust/src/day_01.rs +++ b/2017/rust/src/day_01.rs @@ -1,20 +1,21 @@ use failure::*; pub fn solve(input: &str) -> Result { - let mut input: Vec = input + let input: Vec = input .trim() .chars() .map(|x| x.to_digit(10).ok_or_else(|| format_err!(""))) .collect::>()?; // 1 - let last = input[input.len() - 1]; - input.push(last); + let offset = 1; + let offset_iter = input.iter().skip(offset).cycle().take(input.len()); Ok( input - .windows(2) - .filter(|&a| a[0] == a[1]) - .map(|a| a[0]) + .iter() + .zip(offset_iter) + .filter(|&a| a.0 == a.1) + .map(|a| a.0) .sum::() .to_string(), )