From 4f85fed18ac283aa328f87ede9cdf96c52cd9517 Mon Sep 17 00:00:00 2001 From: Toby Davis Date: Mon, 2 Dec 2024 21:29:12 +0000 Subject: [PATCH] A fairly dodgy answer to Day02 Part1 --- benches/benchmark.rs | 2 ++ src/lib.rs | 1 + src/main.rs | 2 +- src/year2024/day02.rs | 38 ++++++++++++++++++++++++++++++++++++ tests/test.rs | 1 + tests/year2024/day02_test.rs | 31 +++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/year2024/day02.rs create mode 100644 tests/year2024/day02_test.rs diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 8908146..9759a73 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -92,6 +92,7 @@ pub mod year2023 { pub mod year2024 { bench!(year2024, day01); + bench!(year2024, day02); } pub fn criterion_benchmark(c: &mut Criterion) { @@ -103,6 +104,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { // year2023::day22::bench(c); year2024::day01::bench(c); + year2024::day02::bench(c); } criterion_group!(benches, criterion_benchmark); diff --git a/src/lib.rs b/src/lib.rs index 3f21a48..e75da1b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,4 +25,5 @@ pub mod year2023 { pub mod year2024 { pub mod day01; + pub mod day02; } diff --git a/src/main.rs b/src/main.rs index f7f6559..bb1eb3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,5 +94,5 @@ fn year2023() -> Vec { } fn year2024() -> Vec { - vec![solution!(year2024, day01)] + vec![solution!(year2024, day01), solution!(year2024, day02)] } diff --git a/src/year2024/day02.rs b/src/year2024/day02.rs new file mode 100644 index 0000000..73cb305 --- /dev/null +++ b/src/year2024/day02.rs @@ -0,0 +1,38 @@ +#![warn(clippy::pedantic, clippy::nursery)] + +use crate::util::parse; + +#[must_use] +pub fn parse(input: &str) -> Vec> { + input + .lines() + .map(|line| { + parse::ParseUnsigned::::new(line.bytes()).collect::>() + }) + .collect() +} + +#[must_use] +pub fn part1(input: &[Vec]) -> usize { + input + .iter() + .filter(|row| { + (row.iter().zip(row.iter().skip(1)).all(|(&a, &b)| a < b) + || row.iter().zip(row.iter().skip(1)).all(|(&a, &b)| a > b)) + && row + .iter() + .zip(row.iter().skip(1)) + .all(|(&a, &b)| matches!(a.abs_diff(b), 1..4)) + }) + .count() +} + +#[must_use] +pub fn part2(input: &[Vec]) -> u32 { + // todo!() + 0 +} + +// For my input, the correct answer is: +// Part 1: 407 +// Part 2: diff --git a/tests/test.rs b/tests/test.rs index 13a948a..7903b2e 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -7,4 +7,5 @@ pub mod year2023 { pub mod year2024 { mod day01_test; + mod day02_test; } diff --git a/tests/year2024/day02_test.rs b/tests/year2024/day02_test.rs new file mode 100644 index 0000000..ec6e9a1 --- /dev/null +++ b/tests/year2024/day02_test.rs @@ -0,0 +1,31 @@ +use aoc::year2024::day02::*; + +const EXAMPLE_PART: &str = "\ +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"; + +const EXAMPLE_PART1_CUSTOM: &str = "\ +1 2 3 4 6 7 10 7 +1 2 3"; + +#[test] +fn test_sample_part1() { + let input = parse(EXAMPLE_PART); + assert_eq!(part1(&input), 2); +} + +#[test] +fn test_sample_part1_custom() { + let input = parse(EXAMPLE_PART1_CUSTOM); + assert_eq!(part1(&input), 0); +} + +#[test] +fn test_sample_part2() { + let input = parse(EXAMPLE_PART); + // assert_eq!(part2(&input), 31); +}