[2017][rust][1.0] refactor

sorbet
Alpha Chen 7 years ago
parent c5365a4532
commit 26ac38d39f

@ -1,20 +1,21 @@
use failure::*; use failure::*;
pub fn solve(input: &str) -> Result<String, Error> { pub fn solve(input: &str) -> Result<String, Error> {
let mut input: Vec<u32> = input let input: Vec<u32> = input
.trim() .trim()
.chars() .chars()
.map(|x| x.to_digit(10).ok_or_else(|| format_err!(""))) .map(|x| x.to_digit(10).ok_or_else(|| format_err!("")))
.collect::<Result<_, _>>()?; .collect::<Result<_, _>>()?;
// 1 // 1
let last = input[input.len() - 1]; let offset = 1;
input.push(last); let offset_iter = input.iter().skip(offset).cycle().take(input.len());
Ok( Ok(
input input
.windows(2) .iter()
.filter(|&a| a[0] == a[1]) .zip(offset_iter)
.map(|a| a[0]) .filter(|&a| a.0 == a.1)
.map(|a| a.0)
.sum::<u32>() .sum::<u32>()
.to_string(), .to_string(),
) )

Loading…
Cancel
Save