solution: day 2

This commit is contained in:
Matej Janezic 2024-12-02 07:00:45 +01:00
parent 000000305b
commit 0000004082
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
2 changed files with 87 additions and 0 deletions

6
data/examples/02.txt Normal file
View File

@ -0,0 +1,6 @@
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

81
src/bin/02.rs Normal file
View File

@ -0,0 +1,81 @@
use itertools::Itertools;
fn parse_input(input: &str) -> impl Iterator<Item = Vec<isize>> + '_ {
input.lines().map(|l| {
l.split(' ')
.filter_map(|x| x.parse::<isize>().ok())
.collect_vec()
})
}
fn check_report(mut r: impl Iterator<Item = isize>) -> bool {
let mut low = 0;
let mut high = 0;
let mut prev = r.next().expect("Invalid input file?");
for n in r {
let diff = n - prev;
prev = n;
if diff == 0 {
return false;
};
if !(-3..=3).contains(&diff) {
return false;
}
if diff > 0 {
high += 1;
}
if diff < 0 {
low += 1;
}
}
!(low > 0 && high > 0)
}
pub fn part_one(input: &str) -> Option<usize> {
parse_input(input)
.map(|r| check_report(r.into_iter()))
.filter(|&x| x)
.count()
.into()
}
pub fn part_two(input: &str) -> Option<usize> {
Some(
parse_input(input)
.map(|r| {
(0..r.len()).any(|i| {
check_report(
// skip nth idx
r.iter()
.enumerate()
.filter(|&(j, _)| j != i)
.map(|(_, &v)| v),
)
})
})
.filter(|x| *x)
.count(),
)
}
aoc::solution!(2);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
assert_eq!(part_one(&aoc::template::read_file("examples", 2)), None);
}
#[test]
fn test_part_two() {
assert_eq!(part_two(&aoc::template::read_file("examples", 2)), None);
}
}