Day 02 use just iterators
This commit is contained in:
parent
3509e0916e
commit
1801f30cd6
1 changed files with 10 additions and 19 deletions
|
@ -24,7 +24,7 @@ pub fn main() {
|
||||||
fn is_safe(report: &Vec<i32>) -> bool {
|
fn is_safe(report: &Vec<i32>) -> bool {
|
||||||
let increasing = report[0] < report[1];
|
let increasing = report[0] < report[1];
|
||||||
report.array_windows::<2>().all(|window| {
|
report.array_windows::<2>().all(|window| {
|
||||||
let increase = window[1] as i32 - window[0] as i32;
|
let increase = window[1] - window[0];
|
||||||
if increasing {
|
if increasing {
|
||||||
1 <= increase && increase <= 3
|
1 <= increase && increase <= 3
|
||||||
} else {
|
} else {
|
||||||
|
@ -34,27 +34,18 @@ fn is_safe(report: &Vec<i32>) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_1(input: &Vec<Vec<i32>>) -> i32 {
|
fn part_1(input: &Vec<Vec<i32>>) -> i32 {
|
||||||
let mut safe = 0;
|
input.iter().filter(|&report| is_safe(report)).count() as i32
|
||||||
for report in input {
|
|
||||||
if is_safe(report) {
|
|
||||||
safe += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
safe
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(input: &Vec<Vec<i32>>) -> i32 {
|
fn part_2(input: &Vec<Vec<i32>>) -> i32 {
|
||||||
let mut safe = 0;
|
input
|
||||||
for report in input {
|
.iter()
|
||||||
for i in 0..report.len() {
|
.filter(|report| {
|
||||||
let modified_report = [&report[..i], &report[i + 1..]].concat();
|
(0..report.len())
|
||||||
if is_safe(&modified_report) {
|
.map(|i| [&report[..i], &report[i + 1..]].concat())
|
||||||
safe += 1;
|
.any(|modified_report| is_safe(&modified_report))
|
||||||
break;
|
})
|
||||||
}
|
.count() as i32
|
||||||
}
|
|
||||||
}
|
|
||||||
safe
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue