hating part 2 rn

This commit is contained in:
Benjamyn Love 2024-12-02 19:23:31 +11:00
parent fb330b9b9f
commit efccc0cf33

View File

@ -67,6 +67,54 @@ impl ReportList {
} }
println!("{}", safe); 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 { fn load_file(path: String) -> String {
@ -84,4 +132,5 @@ fn main() {
reportlist.parse_data(data); reportlist.parse_data(data);
reportlist.solve(); reportlist.solve();
reportlist.solve2();
} }