This commit is contained in:
Adrian Groh 2024-12-02 08:31:52 +01:00
parent e55a2e38c4
commit 5b1f405183
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771
3 changed files with 65 additions and 0 deletions

7
day2/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day2"
version = "0.1.0"

6
day2/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day2"
version = "0.1.0"
edition = "2021"
[dependencies]

52
day2/src/main.rs Normal file
View File

@ -0,0 +1,52 @@
fn parse(input: &str) -> Vec<Vec<u32>> {
input
.lines()
.map(|line| {
line.split_whitespace()
.map(|level| level.parse::<u32>().unwrap())
.collect()
})
.collect()
}
fn is_safe(report: &[u32]) -> bool {
let pairs: Vec<(u32, u32)> = report
.windows(2)
.map(|w| (*w.first().unwrap(), *w.get(1).unwrap()))
.collect();
pairs.iter().all(|(x, y)| x < y && x + 3 >= *y)
|| pairs.iter().all(|(x, y)| x > y && *x <= y + 3)
}
fn part1(parsed_input: &[Vec<u32>]) -> u32 {
parsed_input
.iter()
.filter(|report| is_safe(report))
.count() as u32
}
fn part2(parsed_input: &[Vec<u32>]) -> u32 {
let mut res = 0;
for report in parsed_input {
if is_safe(report) {
res += 1;
continue;
}
for idx in 0..report.len() {
let mut modified_report = report.clone();
modified_report.remove(idx);
if is_safe(&modified_report) {
res += 1;
break;
}
}
}
res
}
fn main() {
let input = include_str!("../input.txt");
let parsed_input = parse(input);
println!("{}", part1(&parsed_input));
println!("{}", part2(&parsed_input));
}