-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.rs
71 lines (61 loc) · 1.74 KB
/
day04.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
pub fn part_one(input: &str) -> u32 {
input
.lines()
.map(|l| {
let mut it = l.split(',').map(|r| {
let mut it = r.split('-');
(
it.next().unwrap().parse::<u32>().unwrap(),
it.next().unwrap().parse::<u32>().unwrap(),
)
});
let (a_from, a_to) = it.next().unwrap();
let (b_from, b_to) = it.next().unwrap();
if (a_from <= b_from && a_to >= b_to) || (b_from <= a_from && b_to >= a_to) {
1
} else {
0
}
})
.sum()
}
pub fn part_two(input: &str) -> u32 {
input
.lines()
.map(|l| {
let mut it = l.split(',').map(|r| {
let mut it = r.split('-');
(
it.next().unwrap().parse::<u32>().unwrap(),
it.next().unwrap().parse::<u32>().unwrap(),
)
});
let (a_from, a_to) = it.next().unwrap();
let (b_from, b_to) = it.next().unwrap();
if a_from <= b_to && b_from <= a_to {
1
} else {
0
}
})
.sum()
}
#[cfg(test)]
mod tests {
#[test]
fn part_one_example() {
assert_eq!(super::part_one(include_str!("input/day04_example.txt")), 2);
}
#[test]
fn part_one() {
assert_eq!(super::part_one(include_str!("input/day04.txt")), 651);
}
#[test]
fn part_two_example() {
assert_eq!(super::part_two(include_str!("input/day04_example.txt")), 4);
}
#[test]
fn part_two() {
assert_eq!(super::part_two(include_str!("input/day04.txt")), 956);
}
}