generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
70 lines (56 loc) · 1.8 KB
/
03.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
69
70
advent_of_code::solution!(3);
use std::collections::BTreeSet;
fn parse_data(input: &str) -> Vec<&[u8]> {
input.lines().map(|x| x.as_bytes()).collect()
}
fn priority(s: u8) -> u32 {
if s.is_ascii_lowercase() {
(s as u32) - 96
} else {
(s as u32) - 38
}
}
pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);
let result = data
.into_iter()
.map(|x| {
let p1 = x[..x.len() / 2].iter().collect::<BTreeSet<&u8>>();
let p2 = x[x.len() / 2..].iter().collect::<BTreeSet<&u8>>();
let mut intersection_iter = p1.intersection(&p2);
let element = intersection_iter.next().unwrap();
priority(**element)
})
.sum();
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);
let result = data
.chunks_exact(3)
.map(|c| {
let p1 = c[0].iter().collect::<BTreeSet<&u8>>();
let p2 = c[1].iter().collect::<BTreeSet<&u8>>();
let p3 = c[2].iter().collect::<BTreeSet<&u8>>();
let intersection = p2.intersection(&p3).copied().collect(); // TODO: zakaj mora biti tukaj copied; bi se dalo brez tega?
let mut intersection_iter = p1.intersection(&intersection);
let element = intersection_iter.next().unwrap();
priority(**element)
})
.sum();
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(157));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(70));
}
}