feat(day2): init
This commit is contained in:
parent
4df3715c5d
commit
0437caa597
2 changed files with 87 additions and 1 deletions
85
src/solutions/day_02.rs
Normal file
85
src/solutions/day_02.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
use itertools::Itertools;
|
||||
|
||||
use crate::common::{Answer, Solution};
|
||||
|
||||
pub struct Day02;
|
||||
|
||||
impl Solution for Day02 {
|
||||
fn name(&self) -> &'static str {
|
||||
""
|
||||
}
|
||||
|
||||
fn part_a(&self, input: &str) -> Answer {
|
||||
let reports = input
|
||||
.lines()
|
||||
.filter(|l| {
|
||||
let nums = l.split_whitespace().map(|n| n.parse::<i32>().unwrap());
|
||||
|
||||
is_valid(&nums)
|
||||
})
|
||||
.count();
|
||||
|
||||
Answer::Number(reports as u64)
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
let reports = input
|
||||
.lines()
|
||||
.filter(|l| {
|
||||
let nums = l.split_whitespace().map(|n| n.parse::<i32>().unwrap());
|
||||
let valid = is_valid(&nums);
|
||||
|
||||
if !valid {
|
||||
let len = nums.clone().count();
|
||||
for skip_i in 0..len {
|
||||
let new_nums = nums
|
||||
.clone()
|
||||
.enumerate()
|
||||
.filter_map(|(i, n)| (skip_i != i).then_some(n));
|
||||
|
||||
if is_valid(&new_nums) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
valid
|
||||
})
|
||||
.count();
|
||||
|
||||
Answer::Number(reports as u64)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_valid<T: Iterator<Item = i32> + Clone>(nums: &T) -> bool {
|
||||
let diffs = nums.clone().tuple_windows().map(|(a, b)| a - b);
|
||||
|
||||
diffs.clone().all(|d| (-3..=-1).contains(&d)) || diffs.clone().all(|d| (1..=3).contains(&d))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::Day02;
|
||||
use crate::common::Solution;
|
||||
|
||||
use indoc::indoc;
|
||||
|
||||
const INPUT: &str = indoc! {"
|
||||
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
|
||||
"};
|
||||
|
||||
#[test]
|
||||
fn part_a() {
|
||||
assert_eq!(Day02.part_a(INPUT), 2.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_b() {
|
||||
assert_eq!(Day02.part_b(INPUT), 4.into());
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
use crate::common::Solution;
|
||||
|
||||
mod day_01;
|
||||
mod day_02;
|
||||
|
||||
pub const SOLUTIONS: &[&dyn Solution] = &[&day_01::Day01];
|
||||
pub const SOLUTIONS: &[&dyn Solution] = &[&day_01::Day01, &day_02::Day02];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue