-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_02.rs
146 lines (134 loc) · 3.5 KB
/
day_02.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
139
140
141
142
143
144
145
146
use crate::prelude::*;
pub struct Day02;
type DataType = i32;
impl Puzzle for Day02 {
fn new(_ops: &super::RootOpt) -> Box<dyn Puzzle> {
Box::new(Self)
}
fn part_one(&self, _input: &str) -> super::PuzzleResult {
let reports = _input.trim().lines().map(|l| {
// dbg!(l);
let chunks = l.split_whitespace().map(|n_str| {
n_str
.parse::<DataType>()
.expect("Failed to unwrap data value")
});
// println!("{:?}", chunks.clone().collect_vec());
return chunks;
});
let safety_scores = reports.map(report_is_safe).filter(|s| s.clone());
let count = safety_scores.count();
return Ok(count.to_string());
}
fn part_two(&self, _input: &str) -> super::PuzzleResult {
let reports = _input.trim().lines().map(|l| {
// dbg!(l);
let chunks = l.split_whitespace().map(|n_str| {
n_str
.parse::<DataType>()
.expect("Failed to unwrap data value")
});
// println!("{:?}", chunks.clone().collect_vec());
return chunks;
});
let safety_scores = reports
.map(|iter| {
let values = iter.collect_vec();
if report_is_safe(values.iter().copied()) {
return true;
}
return (0..values.len())
.map(|idx| skipper_iter(0, idx, values.iter().copied()))
.any(report_is_safe);
})
.filter(|s| s.clone());
let count = safety_scores.count();
return Ok(count.to_string());
}
}
struct SkippingIndexIterator<T, Itor>
where
Itor: Iterator<Item = T>,
{
index: usize,
skip_target: usize,
iter: Itor,
}
impl<T, I> Iterator for SkippingIndexIterator<T, I>
where
I: Iterator<Item = T>,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let mut value = self.iter.next()?;
if self.index == self.skip_target {
value = self.iter.next()?;
self.index += 1;
}
self.index += 1;
Some(value)
}
}
fn skipper_iter<T, I>(current_index: usize, skip: usize, iter: I) -> SkippingIndexIterator<T, I>
where
I: Iterator<Item = T>,
{
SkippingIndexIterator {
index: current_index,
skip_target: skip,
iter,
}
}
fn report_is_safe(iter: impl Iterator<Item = DataType>) -> bool {
let mut first: DataType = 0;
let mut second: DataType;
let mut ascending = false;
let mut last: DataType = 0;
for (idx, val) in iter.enumerate() {
match idx {
0 => {
first = val;
continue;
}
1 => {
second = val;
last = first;
ascending = first < second;
}
_ => {}
}
if ascending != (last < val) {
return false;
}
let still_safe = (1..=3).contains(&last.abs_diff(val));
if !still_safe {
return false;
}
last = val
}
return true;
}
#[test]
fn sample_02_1() {
let input = r#"
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
"#;
println!("{:?}", Day02.part_one(input).unwrap());
}
#[test]
fn sample_02_2() {
let input = r#"
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
"#;
println!("{:?}", Day02.part_two(input).unwrap());
}