diff options
Diffstat (limited to 'src/bin/day2.rs')
| -rw-r--r-- | src/bin/day2.rs | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/bin/day2.rs b/src/bin/day2.rs index e34b8b0..3d66a45 100644 --- a/src/bin/day2.rs +++ b/src/bin/day2.rs @@ -2,8 +2,8 @@ static DAY: u8 = 2; fn main() { let input = advent::read_lines(DAY); - println!("{DAY}a: {}", safe_reports(&input)); - println!("{DAY}b: {}", 0); + println!("{DAY}a: {}", safe_reports(&input, false)); + println!("{DAY}b: {}", safe_reports(&input, true)); } fn is_report_safe(report: &[i32]) -> bool { @@ -20,7 +20,7 @@ fn is_report_safe(report: &[i32]) -> bool { true } -fn safe_reports(input: &[String]) -> usize { +fn safe_reports(input: &[String], with_dampener: bool) -> usize { let mut reports = Vec::new(); for line in input { let numbers = line.split(" ") @@ -28,9 +28,22 @@ fn safe_reports(input: &[String]) -> usize { .collect::<Vec<_>>(); reports.push(numbers); } - reports.iter() - .filter(|x| is_report_safe(x)) - .count() + let mut count = 0; + for report in reports { + if is_report_safe(&report) { + count += 1; + } else if with_dampener { + for i in 0 .. report.len() { + let mut dampened = report.clone(); + dampened.remove(i); + if is_report_safe(&dampened) { + count += 1; + break; + } + } + } + } + count } #[cfg(test)] @@ -47,6 +60,7 @@ mod tests { "8 6 4 4 1", "1 3 6 7 9", ].iter().map(|&x| String::from(x)).collect::<Vec<_>>(); - assert_eq!(safe_reports(&input), 2); + assert_eq!(safe_reports(&input, false), 2); + assert_eq!(safe_reports(&input, true), 4); } } |
