Replace for loops

This commit is contained in:
Adrian Groh 2024-12-02 10:25:24 +01:00
parent 5b1f405183
commit d3cd5d777e
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771

View File

@ -19,29 +19,21 @@ fn is_safe(report: &[u32]) -> bool {
} }
fn part1(parsed_input: &[Vec<u32>]) -> u32 { fn part1(parsed_input: &[Vec<u32>]) -> u32 {
parsed_input parsed_input.iter().filter(|report| is_safe(report)).count() as u32
.iter()
.filter(|report| is_safe(report))
.count() as u32
} }
fn part2(parsed_input: &[Vec<u32>]) -> u32 { fn part2(parsed_input: &[Vec<u32>]) -> u32 {
let mut res = 0; parsed_input
for report in parsed_input { .iter()
if is_safe(report) { .filter(|report| {
res += 1; is_safe(report)
continue; || (0..report.len()).any(|idx| {
} let mut modified_report = (*report).clone();
for idx in 0..report.len() {
let mut modified_report = report.clone();
modified_report.remove(idx); modified_report.remove(idx);
if is_safe(&modified_report) { is_safe(&modified_report)
res += 1; })
break; })
} .count() as u32
}
}
res
} }
fn main() { fn main() {