aoc/year2016/
day06.rs

1//! # Signals and Noise
2//!
3//! The cardinality of uppercase letters is only 26 so we can use a fixed size array to
4//! count the frequency of each character efficiently.
5type Input = (String, String);
6
7pub fn parse(input: &str) -> Input {
8    let width = input.lines().next().unwrap().len();
9    let stride = width + 1;
10    let input = input.as_bytes();
11
12    let to_index = |b: u8| (b - b'a') as usize;
13    let to_char = |i: usize| ((i as u8) + b'a') as char;
14
15    (0..width)
16        .map(|offset| {
17            let mut freq = [0; 26];
18            input.iter().skip(offset).step_by(stride).for_each(|&b| freq[to_index(b)] += 1);
19
20            let (max, _) =
21                freq.iter().enumerate().filter(|(_, f)| **f > 0).max_by_key(|(_, f)| **f).unwrap();
22            let (min, _) =
23                freq.iter().enumerate().filter(|(_, f)| **f > 0).min_by_key(|(_, f)| **f).unwrap();
24
25            (to_char(max), to_char(min))
26        })
27        .unzip()
28}
29
30pub fn part1(input: &Input) -> &str {
31    &input.0
32}
33
34pub fn part2(input: &Input) -> &str {
35    &input.1
36}