#[path = "../lib.rs"] mod lib; fn check(a: u32, b: u32, increasing: bool) -> bool { return (increasing ^ (a > b)) && (1..=3).contains(&a.abs_diff(b)); } fn is_safe(report: &Vec) -> (bool, bool) { let mut x = 0; x += if report[1] > report[0] { 1 } else { -1 }; x += if report[2] > report[1] { 1 } else { -1 }; x += if report[3] > report[2] { 1 } else { -1 }; let inc = x > 0; let n = report.len(); let mut i = 1; let mut dampened = false; while i < n { if check(report[i - 1], report[i], inc) { i += 1; } else if dampened { return (false, false); } else if i + 1 == n || check(report[i - 1], report[i + 1], inc) { dampened = true; i += 2; } else if i == 1 || check(report[i - 2], report[i], inc) { dampened = true; i += 1; } else { return (false, false); } } return (!dampened, true); } fn main() { let mut count1 = 0; let mut count2 = 0; for line in lib::iter_input() { let report = line.split(' ').map(|s| s.parse::().unwrap()).collect(); let (safe, safe_dampened) = is_safe(&report); if safe { count1 += 1; } if safe_dampened { count2 += 1; } } println!("part1: {}", count1); println!("part2: {}", count2); }