-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rs
56 lines (48 loc) · 1.51 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
use std::collections::HashSet;
fn parse_input_pt1(input: &str) -> Vec<char> {
input
.lines()
.flat_map(|line| {
let half = line.len() / 2;
let first_set: HashSet<char> = line[..half].chars().collect();
let second_half = &line[half..];
second_half.chars().find(|c| first_set.contains(&c))
})
.collect()
}
fn parse_input_pt2(input: &str) -> Vec<char> {
input
.lines()
.collect::<Vec<&str>>()
.chunks(3)
.flat_map(|chunks| {
let first_set: HashSet<char> = chunks[0].chars().collect();
let second_set: HashSet<char> = chunks[1].chars().collect();
let third_chunk = chunks[2];
third_chunk
.chars()
.find(|c| first_set.contains(&c) && second_set.contains(&c))
})
.collect()
}
fn sum_priorities(items: Vec<char>) -> u32 {
items
.iter()
.map(|c| match *c {
'A'..='Z' => *c as u32 - 'A' as u32 + 27, // 'A' has priority 27
'a'..='z' => *c as u32 - 'a' as u32 + 1, // 'a' has priority 1
_ => unreachable!(),
})
.sum()
}
fn part_one(input: &str) -> u32 {
sum_priorities(parse_input_pt1(input))
}
fn part_two(input: &str) -> u32 {
sum_priorities(parse_input_pt2(input))
}
pub fn main() {
let input = include_str!("../inputs/03.txt");
println!("part one: {}", part_one(input)); // 7875
println!("part two: {}", part_two(input)); // 2479
}