1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//! # Inverse Captcha
//!
//! Modern hardware is so good at shuffling memory around that it's faster to rotate the entire
//! array instead of stepping through elements one at a time with an index modulo array length.
use crate::util::parse::*;

pub fn parse(input: &str) -> &[u8] {
    input.trim().as_bytes()
}

pub fn part1(input: &[u8]) -> u32 {
    captcha(input, 1)
}

pub fn part2(input: &[u8]) -> u32 {
    captcha(input, input.len() / 2)
}

fn captcha(input: &[u8], offset: usize) -> u32 {
    let mut rotated = input.to_vec();
    rotated.rotate_left(offset);

    input
        .iter()
        .zip(rotated.iter())
        .filter_map(|(a, b)| (a == b).then_some(a.to_decimal() as u32))
        .sum()
}