-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
138 lines (127 loc) · 3.92 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
data.lines()
.map(|l| {
let mut nums = l.split_whitespace().map(|num| num.parse::<u8>().unwrap());
let mut prev = nums.next().unwrap();
let curr = nums.next().unwrap();
let increasing = curr > prev;
if !right_gap(prev, curr, increasing) {
return false;
}
prev = curr;
for curr in nums {
if !right_gap(prev, curr, increasing) {
return false;
}
prev = curr;
}
true
})
.filter(|&v| v)
.count()
}
fn part_two(data: &str) -> usize {
data.lines()
.map(|l| {
let mut numbers = l.split_whitespace().map(|num| num.parse::<u8>().unwrap());
let (Some(num1), Some(num2), Some(num3), Some(num4)) = (
numbers.next(),
numbers.next(),
numbers.next(),
numbers.next(),
) else {
unreachable!();
};
let mut error = false;
let (increasing, mut prev, mut prev_prev) = match find_alignment(num1, num2, num3, num4)
{
Aligned::Not => return false,
Aligned::OneError(dir, last) => {
error = true;
(dir, last, 0)
}
Aligned::All(dir) => (dir, num4, num3),
};
for curr in numbers {
if right_gap(prev, curr, increasing) {
prev_prev = prev;
prev = curr;
} else {
if error {
return false;
}
if right_gap(prev_prev, curr, increasing) {
prev = curr;
//prev_prev no longer relevant
}
error = true;
}
}
true
})
.filter(|&v| v)
.count()
}
const fn right_gap(num1: u8, num2: u8, increasing: bool) -> bool {
if ((num2 < num1) && increasing) || ((num2 > num1) && !increasing) {
return false;
}
let diff = if increasing { num2 - num1 } else { num1 - num2 };
diff >= 1 && diff <= 3
}
const fn find_alignment(num1: u8, num2: u8, num3: u8, num4: u8) -> Aligned {
if let Some(increasing) = three_aligned(num1, num2, num3) {
if right_gap(num3, num4, increasing) {
return Aligned::All(increasing);
}
if right_gap(num2, num4, increasing) {
return Aligned::OneError(increasing, num4);
}
return Aligned::OneError(increasing, num3);
}
if let Some(increasing) = three_aligned(num1, num2, num4) {
return Aligned::OneError(increasing, num4);
}
if let Some(increasing) = three_aligned(num1, num3, num4) {
return Aligned::OneError(increasing, num4);
}
if let Some(increasing) = three_aligned(num2, num3, num4) {
return Aligned::OneError(increasing, num4);
}
Aligned::Not
}
enum Aligned {
All(bool),
OneError(bool, u8),
Not,
}
const fn three_aligned(num1: u8, num2: u8, num3: u8) -> Option<bool> {
if num1 == num2 || num2 == num3 {
return None;
}
let increasing = num2 > num1;
if !right_gap(num1, num2, increasing) || !right_gap(num2, num3, increasing) {
return None;
}
Some(increasing)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(2, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(4, part_two(data));
}
}