-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.rs
93 lines (88 loc) · 2.65 KB
/
2.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
85
86
87
88
89
90
91
92
93
use std::cmp::Ordering;
fn main() {
println!("{}", problem_1());
println!("{}", problem_2());
}
fn problem_1() -> i32 {
let mut count_safe = 0;
let reports = get_reports();
for report in reports {
if report_is_safe(&report) {
count_safe += 1;
}
}
count_safe
}
fn problem_2() -> i32 {
let mut count_safe = 0;
let reports = get_reports();
for report in reports {
if report_is_safe(&report) {
count_safe += 1;
continue;
}
// Horrible...
for (i, _) in report.iter().enumerate() {
let mut report_altered: Vec<i32> = Vec::new();
report_altered.extend_from_slice(&report[..i]);
report_altered.extend_from_slice(&report[i + 1..]);
if report_is_safe(&report_altered) {
count_safe += 1;
break;
}
}
}
count_safe
}
fn report_is_safe(report: &Vec<i32>) -> bool {
let mut last: Option<i32> = None;
let mut is_increasing: Option<bool> = None;
for value in report {
if last.is_some() {
let diff = (value - last.unwrap()).abs();
if !(1..=3).contains(&diff) {
return false;
}
}
match (last, is_increasing) {
(Some(last_value), None) => match value.cmp(&last_value) {
Ordering::Less => is_increasing = Some(false),
Ordering::Greater => is_increasing = Some(true),
Ordering::Equal => {
return false;
}
},
(Some(last_value), Some(is_increasing_value)) => {
match (value.cmp(&last_value), is_increasing_value) {
(Ordering::Less, true) => {
if is_increasing_value {
return false;
}
}
(Ordering::Greater, false) => {
if !is_increasing_value {
return false;
}
}
(Ordering::Equal, _) => {
return false;
}
_ => {}
}
}
_ => {}
}
last = Some(*value);
}
true
}
fn get_reports() -> Vec<Vec<i32>> {
let mut reports: Vec<Vec<i32>> = Vec::new();
if let Ok(lines) = shared::read_lines("./2.txt") {
for line in lines.map_while(Result::ok) {
let ints: Vec<i32> = line.split(' ').map(|x| x.parse::<i32>().unwrap()).collect();
reports.push(ints)
}
}
reports
}