Skip to content

Commit

Permalink
feat(rust): add solutions for 2024 day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 3, 2024
1 parent 6c11850 commit 57c026e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
55 changes: 55 additions & 0 deletions rust/src/2024/02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::ops::Index;

use advent_of_code::utils::challenges::prelude::*;

type Report = Vec<i32>;

fn parse(input: &PuzzleInput) -> Vec<Report> {
input.trim().lines().map(|line| {
line.split_ascii_whitespace().map(|level| level.parse::<i32>().unwrap()).collect()
}).collect()
}

fn is_safe(report: &Report) -> bool {
let mut sign: i32 = 0;
for i in 0..report.len() - 1 {
let (a, b) = (report.index(i), report.index(i + 1));

let diff = a - b;
let diffabs = diff.abs();
if diffabs < 1 || diffabs > 3 { return false }

let diffsign = diff.signum();
if sign != 0 && sign != diffsign { return false }
sign = diffsign
}

true
}

fn is_safe_with_removal(report: &Report) -> bool {
for index in 0..report.len() {
let mut variation = report.clone();
variation.remove(index);
if is_safe(&variation) {
return true
}
}
false
}

fn part_one(input: &PuzzleInput, _args: &RawPuzzleArgs) -> Solution {
let reports = parse(input);

let safe = reports.into_iter().filter(is_safe);
Answer(safe.count() as u64)
}

fn part_two(input: &PuzzleInput, _args: &RawPuzzleArgs) -> Solution {
let reports = parse(input);

let safe = reports.into_iter().filter(is_safe_with_removal);
Answer(safe.count() as u64)
}

solve!(part_one, part_two);
2 changes: 1 addition & 1 deletion rust/src/utils/challenges/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! preload_challenges {
01, 02, 03
)
2024 (
01
01, 02
)
}
};
Expand Down

0 comments on commit 57c026e

Please sign in to comment.