-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): add solutions for 2024 day 2
- Loading branch information
1 parent
6c11850
commit 57c026e
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ macro_rules! preload_challenges { | |
01, 02, 03 | ||
) | ||
2024 ( | ||
01 | ||
01, 02 | ||
) | ||
} | ||
}; | ||
|