aoc/year2017/
day01.rs

1//! # Inverse Captcha
2use crate::util::parse::*;
3
4pub fn parse(input: &str) -> &[u8] {
5    input.trim().as_bytes()
6}
7
8pub fn part1(input: &[u8]) -> u32 {
9    let last = input.len() - 1;
10    sum(&input[..last], &input[1..]) + sum(&input[..1], &input[last..])
11}
12
13pub fn part2(input: &[u8]) -> u32 {
14    let (first, second) = input.split_at(input.len() / 2);
15    2 * sum(first, second)
16}
17
18fn sum(a: &[u8], b: &[u8]) -> u32 {
19    a.iter().zip(b).filter_map(|(a, b)| (a == b).then_some(a.to_decimal() as u32)).sum()
20}