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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::input::Input;
use std::collections::HashMap;
pub fn solve(input: &mut Input) -> Result<String, String> {
let mut counts: [HashMap<u8, u32>; 8] = [
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
];
for line in input.text.lines() {
for (index, c) in line.bytes().take(counts.len()).enumerate() {
let count: &mut HashMap<u8, u32> = &mut counts[index];
*count.entry(c).or_insert(0) += 1;
}
}
Ok(counts
.iter()
.map(|count| {
count
.iter()
.max_by(|a, b| {
if input.is_part_one() {
a.1.cmp(b.1)
} else {
b.1.cmp(a.1)
}
})
.map(|(&key, _value)| key as char)
.unwrap_or('?')
})
.collect())
}
#[test]
pub fn tests() {
use crate::input::{test_part_one, test_part_two};
let real_input = include_str!("day06_input.txt");
test_part_one!(real_input => "qzedlxso".to_string());
test_part_two!(real_input => "ucmifjae".to_string());
}