diff --git a/justfile b/justfile index a9838f5..187ddd4 100644 --- a/justfile +++ b/justfile @@ -3,7 +3,7 @@ _default: cargo run --release -- solve * year := `date +%Y` -day := `date +%d` +day := `nu -c 'date now | format date "%_d" | str trim'` p P in="": cargo run --release -- solve {{year}}:{{day}}:{{P}} {{in}} diff --git a/years/2024/src/day_2.rs b/years/2024/src/day_2.rs index 00430bc..7f6a4a5 100644 --- a/years/2024/src/day_2.rs +++ b/years/2024/src/day_2.rs @@ -3,15 +3,49 @@ use advent_core::{Day, day_stuff, ex_for_day}; pub struct Day2; +fn line_valid(line: &[u64]) -> bool { + let mut increasing: Option = None; + line.windows(2).all(|w| { + let (x, y) = (w[0], w[1]); + if x == y || x.abs_diff(y) > 3 { + false + } else if let Some(increasing) = increasing { + !((increasing && y <= x) || (!increasing && x <= y)) + } else { + increasing = Some(x < y); + true + } + }) +} + impl Day for Day2 { - day_stuff!(2, "", ""); + day_stuff!(2, "2", "4", Vec>); - fn part_1(_input: Self::Input) -> Option { - None + fn part_1(input: Self::Input) -> Option { + Some(input.into_iter().filter(|v| line_valid(&v)).count().to_string()) } - fn part_2(_input: Self::Input) -> Option { - None + fn part_2(input: Self::Input) -> Option { + Some(input.into_iter().filter(|line| { + let valid = line_valid(&line); + if valid { + true + } else { + (0..line.len()).any(|ie| { + let v = line.iter() + .enumerate() + .filter(|(i, _)| *i != ie) + .map(|(_, e)| *e) + .collect::>(); + + line_valid(&v) + }) + } + }).count().to_string()) + } + + fn parse_input(input: &str) -> Self::Input { + input.split('\n').map(|l| l.split_ascii_whitespace().map(|x| x.parse::().unwrap()).collect()).collect() } } diff --git a/years/2024/src/examples/day_2/1.txt b/years/2024/src/examples/day_2/1.txt index e69de29..82cd679 100644 --- a/years/2024/src/examples/day_2/1.txt +++ b/years/2024/src/examples/day_2/1.txt @@ -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 \ No newline at end of file diff --git a/years/2024/src/examples/day_2/2.txt b/years/2024/src/examples/day_2/2.txt index e69de29..82cd679 100644 --- a/years/2024/src/examples/day_2/2.txt +++ b/years/2024/src/examples/day_2/2.txt @@ -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 \ No newline at end of file