From efccc0cf3305c9f7c0fcadd3df81f7b7fddd713f Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 2 Dec 2024 19:23:31 +1100 Subject: [PATCH] hating part 2 rn --- aoc-02/src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/aoc-02/src/main.rs b/aoc-02/src/main.rs index b70de9f..bb6ef55 100644 --- a/aoc-02/src/main.rs +++ b/aoc-02/src/main.rs @@ -67,6 +67,54 @@ impl ReportList { } println!("{}", safe); } + + fn solve2(&mut self) { + let mut safe = 0; + // direction: 0 Ascending, 1 Descending + let mut direction = 0; + for report in &self.reports { + let mut report_safe = true; + // Skip the first iteration + for (pos, entry) in report.iter().enumerate() { + if pos == 0 { + continue; + } + if pos == 1 { + // Check first two values and see if we need to be ascending or decending + if &report[pos - 1] < entry { + // We are ascending + direction = 0; + } else { + // We are descending + direction = 1; + } + } + + // Do the actual solution + if direction == 0 { + // Check all values ascending + if (entry - &report[pos - 1]) > 3 { + report_safe = false; + } + if (entry - &report[pos - 1]) <= 0 { + report_safe = false + } + } else { + // Check all values descending + if (&report[pos - 1] - entry) > 3 { + report_safe = false; + } + if (&report[pos - 1] - entry) <= 0 { + report_safe = false + } + } + } + if report_safe { + safe += 1; + } + } + println!("{}", safe); + } } fn load_file(path: String) -> String { @@ -84,4 +132,5 @@ fn main() { reportlist.parse_data(data); reportlist.solve(); + reportlist.solve2(); }