[2017][rust][1.0] refactor

sorbet
Alpha Chen 7 years ago
parent c5365a4532
commit 26ac38d39f

@ -1,20 +1,21 @@
use failure::*;
pub fn solve(input: &str) -> Result<String, Error> {
let mut input: Vec<u32> = input
let input: Vec<u32> = input
.trim()
.chars()
.map(|x| x.to_digit(10).ok_or_else(|| format_err!("")))
.collect::<Result<_, _>>()?;
// 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::<u32>()
.to_string(),
)

Loading…
Cancel
Save