Skip to content

Commit

Permalink
feat: complete day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzHayden committed Dec 2, 2024
1 parent dea9bf4 commit 09b77d3
Showing 1 changed file with 59 additions and 21 deletions.
80 changes: 59 additions & 21 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,82 @@
advent_of_code::solution!(2);

fn is_safe_report(levels: &[u32]) -> bool {
fn parse_reports(input: &str) -> Vec<Vec<u32>> {
input
.lines()
.map(|line| {
line.split_whitespace()
.map(|num| num.parse::<u32>().unwrap())
.collect()
})
.collect()
}

fn is_safe_report(report: &[u32]) -> bool {
// Count the number of levels
let n = levels.len();
let n = report.len();

// Check base case for a single level
// Check base case (single level)
if n == 1 {
return true;
}

// Determine direction
let increasing = levels[0] < levels[1];
let decreasing = levels[0] > levels[1];
let increasing = report[0] < report[1];
let decreasing = report[0] > report[1];

// Iterate through pairs of adjacent values
for i in 0..n - 1 {
// Check if adjacent levels differ by least one and at most three
let diff = if levels[i] < levels[i + 1] {
levels[i + 1] - levels[i]
let diff = if report[i] < report[i + 1] {
report[i + 1] - report[i]
} else {
levels[i] - levels[i + 1]
report[i] - report[i + 1]
};
if diff < 1 || diff > 3 {
if !(1..=3).contains(&diff) {
return false;
}

// Check if the levels are consistently increasing
if increasing && levels[i + 1] <= levels[i] {
if increasing && report[i + 1] <= report[i] {
return false;
}

// Check if the levels are consistently decreasing
if decreasing && levels[i + 1] >= levels[i] {
if decreasing && report[i + 1] >= report[i] {
return false;
}
}

true
}

fn is_safe_report_problem_dampener(report: &[u32]) -> bool {
let report_status = is_safe_report(report);
if report_status {
// If the base report is safe, return true
return true;
}

for i in 0..report.len() {
// If the base report is not safe, try mutations with each level removed
let mut report_mutation = report.to_owned();
report_mutation.remove(i);
if is_safe_report(&report_mutation) {
return true;
}
}

false
}

pub fn part_one(input: &str) -> Option<u32> {
let mut safe_reports: u32 = 0;

// Parse each report (line) into levels (values)
for line in input.lines() {
let levels: Vec<u32> = line
.split_whitespace()
.map(|num| num.parse::<u32>().unwrap())
.collect();

// Check if the report is safe (satisfies the constraints)
if is_safe_report(&levels) {
let reports: Vec<Vec<u32>> = parse_reports(input);
for report in &reports {
// Check if the report is safe
if is_safe_report(report) {
safe_reports += 1;
}
}
Expand All @@ -60,7 +86,19 @@ pub fn part_one(input: &str) -> Option<u32> {
}

pub fn part_two(input: &str) -> Option<u32> {
None
let mut safe_reports: u32 = 0;

// Parse each report (line) into levels (values)
let reports: Vec<Vec<u32>> = parse_reports(input);
for report in &reports {
// Check if the report is safe using the problem dampener
if is_safe_report_problem_dampener(report) {
safe_reports += 1;
}
}

// Return the number of safe reports
Some(safe_reports)
}

#[cfg(test)]
Expand All @@ -76,6 +114,6 @@ mod tests {
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(4));
}
}

0 comments on commit 09b77d3

Please sign in to comment.