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 = (0..26).max_by_key(|&i| freq[i]).unwrap();
21            let min = (0..26).filter(|&i| freq[i] > 0).min_by_key(|&i| freq[i]).unwrap();
22            (to_char(max), to_char(min))
23        })
24        .unzip()
25}
26
27pub fn part1(input: &Input) -> &str {
28    &input.0
29}
30
31pub fn part2(input: &Input) -> &str {
32    &input.1
33}