-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_04.rs
36 lines (27 loc) · 860 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
use common::{solution, Answer};
solution!("Camp Cleanup", 4);
fn part_a(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(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())
}