-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rs
79 lines (68 loc) · 1.67 KB
/
day02.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
fn process_input(input: &str) -> Vec<Vec<isize>> {
input
.lines()
.map(|line| {
line.split_whitespace()
.map(|x| x.parse().unwrap())
.collect()
})
.collect()
}
fn is_safe(report: &[isize]) -> bool {
let mut prev_diff = report[1] - report[0];
for i in 1..report.len() {
let diff = report[i] - report[i - 1];
if diff.abs() < 1 || diff.abs() > 3 || (diff.signum() != prev_diff.signum()) {
return false;
}
prev_diff = diff;
}
true
}
pub fn part_one(input: &str) -> i32 {
process_input(input).iter().filter(|b| is_safe(b)).count() as i32
}
pub fn part_two(input: &str) -> i32 {
let mut safe = 0;
for report in process_input(input).iter() {
if is_safe(report) {
safe += 1;
} else {
for idx in 0..report.len() {
let mut sample = report.clone();
sample.remove(idx);
if is_safe(&sample) {
safe += 1;
break;
}
}
}
}
safe
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9";
#[test]
fn test_part_one() {
let result = part_one(INPUT);
assert_eq!(result, 2);
}
#[test]
fn test_report_is_safe() {
assert!(!is_safe(&[1, 2, 8, 9, 11, 12]));
assert!(is_safe(&[72, 74, 76, 77, 78, 79, 81, 82]));
assert!(is_safe(&[7, 6, 4, 2, 1]));
}
#[test]
fn test_part_two() {
let result = part_two(INPUT);
assert_eq!(result, 4);
}
}