-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
50 lines (43 loc) · 1.17 KB
/
main.rs
File metadata and controls
50 lines (43 loc) · 1.17 KB
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
use std::collections::HashSet;
fn load_input() -> String {
std::fs::read_to_string("dec06/src/input.txt").expect("Unable to read input file")
}
fn puzzle_1() -> usize {
let input = load_input();
input
.split("\n\n")
.map(|group| {
group
.replace("\n", "")
.chars()
.collect::<HashSet<char>>()
.len()
})
.sum()
}
fn puzzle_2() -> usize {
let input = load_input();
input
.split("\n\n")
.map(|group| {
group
.split('\n')
.map(|p| p.chars().collect::<HashSet<char>>())
.fold(None, |acc, person| -> Option<HashSet<char>> {
if let Some(prev) = acc {
Some(prev.intersection(&person).cloned().collect())
} else {
Some(person)
}
})
.unwrap()
.len()
})
.sum()
}
fn main() {
let result = puzzle_1();
println!("Puzzle 1 output: {}", result);
let result = puzzle_2();
println!("Puzzle 2 output: {}", result);
}