-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
68 lines (58 loc) · 2.01 KB
/
day06.rs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::collections::HashMap;
use std::fs;
use itertools::Itertools;
pub(crate) fn day06() {
println!("{}", part_a(fs::read_to_string("input/2016/day06/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2016/day06/input.txt").unwrap()));
}
fn part_a(input: String) -> String {
let (frequency, max_pos) = get_frequencies(input);
let mut ans = String::new();
for i in 0..=max_pos {
let most = frequency.get(&i).unwrap().iter()
.sorted_by(|(_, count1), (_, count2)| count2.cmp(count1))
.map(|(c, _)| *c)
.collect_vec();
ans.push(most[0])
}
ans
}
fn part_b(input: String) -> String {
let (frequency, max_pos) = get_frequencies(input);
let mut ans = String::new();
for i in 0..=max_pos {
let most = frequency.get(&i).unwrap().iter()
.sorted_by(|(_, count1), (_, count2)| count1.cmp(count2))
.map(|(c, _)| *c)
.collect_vec();
ans.push(most[0])
}
ans
}
fn get_frequencies(input: String) -> (HashMap<usize, HashMap<char, i32>>, usize) {
let mut frequency = HashMap::new();
let mut max_pos = 0;
input.lines().for_each(|line| {
line.chars().enumerate().for_each(|(pos, c)| {
if pos > max_pos { max_pos = pos }
let pos_map = frequency.entry(pos).or_insert(HashMap::new());
*pos_map.entry(c).or_insert(0) += 1;
});
});
(frequency, max_pos)
}
#[cfg(test)]
mod day06_tests {
use std::fs;
use crate::y2016::day06::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!("easter", part_a(fs::read_to_string("input/2016/day06/test.txt").unwrap()));
assert_eq!("advent", part_b(fs::read_to_string("input/2016/day06/test.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!("gebzfnbt", part_a(fs::read_to_string("input/2016/day06/input.txt").unwrap()));
assert_eq!("fykjtwyn", part_b(fs::read_to_string("input/2016/day06/input.txt").unwrap()));
}
}