Skip to content

Commit

Permalink
A fairly dodgy answer to Day02 Part1
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Dec 2, 2024
1 parent c076819 commit 4f85fed
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub mod year2023 {

pub mod year2024 {
bench!(year2024, day01);
bench!(year2024, day02);
}

pub fn criterion_benchmark(c: &mut Criterion) {
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pub mod year2023 {

pub mod year2024 {
pub mod day01;
pub mod day02;
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ fn year2023() -> Vec<Solution> {
}

fn year2024() -> Vec<Solution> {
vec![solution!(year2024, day01)]
vec![solution!(year2024, day01), solution!(year2024, day02)]
}
38 changes: 38 additions & 0 deletions src/year2024/day02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![warn(clippy::pedantic, clippy::nursery)]

use crate::util::parse;

#[must_use]
pub fn parse(input: &str) -> Vec<Vec<u32>> {
input
.lines()
.map(|line| {
parse::ParseUnsigned::<u32>::new(line.bytes()).collect::<Vec<_>>()
})
.collect()
}

#[must_use]
pub fn part1(input: &[Vec<u32>]) -> 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>]) -> u32 {
// todo!()
0
}

// For my input, the correct answer is:
// Part 1: 407
// Part 2:
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub mod year2023 {

pub mod year2024 {
mod day01_test;
mod day02_test;
}
31 changes: 31 additions & 0 deletions tests/year2024/day02_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 4f85fed

Please sign in to comment.