Skip to content

Commit

Permalink
Day 2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
CcydtN committed Dec 13, 2024
1 parent e2b6ce6 commit 154ded5
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,62 @@
use itertools::Itertools;
use num::ToPrimitive;

advent_of_code::solution!(2);

fn part_one_report_check(report: &[i64]) -> bool {
let check = if report[0] > report[1] {
|a, b| a - b >= 1 && a - b <= 3
} else {
|a, b| b - a >= 1 && b - a <= 3
};
report.windows(2).all(|r| check(r[0], r[1]))
}

pub fn part_one(input: &str) -> Option<u64> {
None
// dbg!(input);
let reports = input
.lines()
.map(|line| {
line.split_ascii_whitespace()
.map(|level| level.parse::<i64>().unwrap())
.collect_vec()
})
.collect_vec();

reports
.into_iter()
.filter(|report| part_one_report_check(&report))
.count()
.to_u64()
}

fn part_two_report_check(report: &[i64]) -> bool {
// Brute force
for i in 0..report.len() {
let mut clone = report.to_vec();
clone.remove(i);
if part_one_report_check(&clone) {
return true;
}
}
false
}

pub fn part_two(input: &str) -> Option<u64> {
None
let reports = input
.lines()
.map(|line| {
line.split_ascii_whitespace()
.map(|level| level.parse::<i64>().unwrap())
.collect_vec()
})
.collect_vec();

reports
.into_iter()
.filter(|report| part_two_report_check(&report))
.count()
.to_u64()
}

#[cfg(test)]
Expand All @@ -14,13 +65,13 @@ mod tests {

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
let result = part_one(&advent_of_code::template::read_file("inputs", DAY));
assert_eq!(result, Some(486));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
let result = part_two(&advent_of_code::template::read_file("inputs", DAY));
assert_eq!(result, Some(540));
}
}

0 comments on commit 154ded5

Please sign in to comment.