-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_04.rs
42 lines (32 loc) · 1013 Bytes
/
day_04.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
use common::{Answer, Solution};
pub struct Day04;
impl Solution for Day04 {
fn name(&self) -> &'static str {
"Camp Cleanup"
}
fn part_a(&self, input: &str) -> Answer {
let mut out = 0;
for (p1, p2) in assignment_loop(input) {
out += ((p1.0 >= p2.0 && p1.1 <= p2.1) || (p2.0 >= p1.0 && p2.1 <= p1.1)) as usize;
}
out.into()
}
fn part_b(&self, input: &str) -> Answer {
let mut out = 0;
for (p1, p2) in assignment_loop(input) {
out += (p1.0.max(p2.0) <= p1.1.min(p2.1)) as usize;
}
out.into()
}
}
fn assignment_loop(raw: &str) -> Vec<((u32, u32), (u32, u32))> {
raw.trim()
.lines()
.map(|x| x.split_once(',').unwrap())
.map(|(a, b)| (split_range(a), split_range(b)))
.collect()
}
fn split_range(range: &str) -> (u32, u32) {
let mut range = range.split('-').map(|x| x.parse::<u32>().unwrap());
(range.next().unwrap(), range.next().unwrap())
}