diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2024-12-02 18:34:53 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2024-12-02 18:34:53 +0100 |
| commit | fdd1768897ec9493f726ca946201c8ad6c22db5f (patch) | |
| tree | 18d1ab7291d7e44ac8b0e42aabeb16d23610f3a1 /src | |
| parent | 5dbecaa72f58f299826c24488328118c98a09b37 (diff) | |
day2 solution 2
Diffstat (limited to 'src')
| -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); } } |
