aoc/year2017/
day01.rs

1//! # Inverse Captcha
2//!
3//! Modern hardware is so good at shuffling memory around that it's faster to rotate the entire
4//! array instead of stepping through elements one at a time with an index modulo array length.
5use crate::util::parse::*;
6
7pub fn parse(input: &str) -> &[u8] {
8    input.trim().as_bytes()
9}
10
11pub fn part1(input: &[u8]) -> u32 {
12    captcha(input, 1)
13}
14
15pub fn part2(input: &[u8]) -> u32 {
16    captcha(input, input.len() / 2)
17}
18
19fn captcha(input: &[u8], offset: usize) -> u32 {
20    let mut rotated = input.to_vec();
21    rotated.rotate_left(offset);
22
23    input
24        .iter()
25        .zip(rotated.iter())
26        .filter_map(|(a, b)| (a == b).then_some(a.to_decimal() as u32))
27        .sum()
28}