-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.rs
84 lines (72 loc) · 1.61 KB
/
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Day 4: Camp Cleanup
* See [https://adventofcode.com/2022/day/4]
*/
use aoc2022::util;
#[derive(Debug, Clone, Copy)]
struct SRange {
l: u32,
h: u32
}
impl SRange {
fn within(&self, other: &SRange) -> bool {
let d = self.h - self.l;
let od = other.h - other.l;
if od >= d {
self.l >= other.l && self.h <= other.h
} else {
other.l >= self.l && other.h <= self.h
}
}
fn overlap(&self, other: &SRange) -> bool {
let r = self.l..=self.h;
self.within(other) || r.contains(&other.l) || r.contains(&other.h)
}
}
fn pair_range_str(s: &str) -> (SRange, SRange) {
let (p1, p2) = util::split_str_on(s, ',');
(range_str(p1), range_str(p2))
}
fn range_str(s: &str) -> SRange {
let (a, b) = util::split_str_on(s, '-');
SRange {
l: a.parse().unwrap(),
h: b.parse().unwrap()
}
}
pub fn part_2(input: &str) -> Option<u32> {
let lines = input.lines();
let mut sum = 0;
for l in lines {
let (r1, r2) = pair_range_str(l);
if r1.overlap(&r2) {
sum += 1;
}
}
Some(sum)
}
pub fn part_1(input: &str) -> Option<u32> {
let lines = input.lines();
let mut sum = 0;
for l in lines {
let (r1, r2) = pair_range_str(l);
if r1.within(&r2) {
sum += 1;
}
}
Some(sum)
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use aoc2022::assert_ex;
use super::*;
#[test]
fn test_part_1() {
assert_ex!(part_1, 2);
}
#[test]
fn test_part_2() {
assert_ex!(part_2, 4);
}
}