Advent of Code solutions
at main 53 lines 1.4 kB view raw
1use advent_core::{day_stuff, ex_for_day, Day}; 2use utils::misc::{all_combos_remove_one, follows_diff_range, FollowRangeResult}; 3 4pub struct Day2; 5 6fn line_valid(line: &[i64]) -> bool { 7 let res = follows_diff_range(line, -3..4, true, false); 8 matches!( 9 res, 10 FollowRangeResult::Increasing | FollowRangeResult::Decreasing 11 ) 12} 13 14impl Day for Day2 { 15 day_stuff!(2, "2", "4", Vec<Vec<i64>>); 16 17 fn part_1(input: Self::Input) -> Option<String> { 18 Some( 19 input 20 .into_iter() 21 .filter(|v| line_valid(v)) 22 .count() 23 .to_string(), 24 ) 25 } 26 27 fn part_2(input: Self::Input) -> Option<String> { 28 Some( 29 input 30 .into_iter() 31 .filter(|line| { 32 line_valid(line) 33 || all_combos_remove_one(line).any(|combo| { 34 let v = combo.copied().collect::<Vec<_>>(); 35 line_valid(&v) 36 }) 37 }) 38 .count() 39 .to_string(), 40 ) 41 } 42 43 fn parse_input(input: &str) -> Self::Input { 44 input 45 .split('\n') 46 .map(|l| { 47 l.split_ascii_whitespace() 48 .map(|x| x.parse::<i64>().unwrap()) 49 .collect() 50 }) 51 .collect() 52 } 53}