-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.rs
51 lines (48 loc) · 1.47 KB
/
06.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
use std::collections::HashSet;
use std::fs;
#[doc(hidden)]
fn main() {
// Read input
// * First vector = group
// * Second vector = person in group
// * Hash set = answers of person
let data: Vec<Vec<HashSet<char>>> = fs::read_to_string("input/06.txt")
.expect("Can't read input file")
.split("\n\n")
.map(|group| {
group
.lines()
.map(|person| person.chars().collect())
.collect()
})
.collect();
// Part 1
// For each group compute the union of all answers of all people in that group.
// The count for that group is the size of the union. Sum all counts.
let part_1: usize = data
.iter()
.map(|group| {
group
.iter()
.fold(HashSet::new(), |acc, hs| acc.union(hs).cloned().collect())
.len()
})
.sum();
println!("Part 1: {}", part_1);
// Part 2
// For each group compute the intersection of all answers of all people in that group.
// The count for that group is the size of the intersection. Sum all counts.
let part_2: usize = data
.iter()
.map(|group| {
group
.iter()
.skip(1)
.fold(group[0].clone(), |acc, hs| {
acc.intersection(hs).cloned().collect()
})
.len()
})
.sum();
println!("Part 2: {}", part_2);
}