retroactive, to derust my rust

day2 part2

+38 -4
+38 -4
src/lib.rs
··· 40 40 } 41 41 42 42 pub mod day2 { 43 - pub fn day2_part1(input: &str) -> String { 44 - let reports: Vec<Vec<u32>> = input 43 + fn day2_preprocess(input: &str) -> Vec<Vec<u32>> { 44 + input 45 45 .lines() 46 46 .map(|report| { 47 47 report ··· 49 49 .map(|level| level.parse().unwrap()) 50 50 .collect() 51 51 }) 52 - .collect(); 53 - 52 + .collect() 53 + } 54 + pub fn day2_part1(input: &str) -> String { 55 + let reports = day2_preprocess(input); 54 56 reports 55 57 .into_iter() 56 58 .filter(gradual) ··· 58 60 .count() 59 61 .to_string() 60 62 } 63 + pub fn day2_part2(input: &str) -> String { 64 + let reports = day2_preprocess(input); 65 + reports 66 + .into_iter() 67 + .filter(should_filter_out_part2) 68 + .count() 69 + .to_string() 70 + } 71 + fn should_filter_out_part2(report: &Vec<u32>) -> bool { 72 + if gradual(report) && monotonic(report) { 73 + return true; 74 + } 75 + for index_to_skip in 0..report.len() { 76 + let skipped_report: Vec<u32> = report 77 + .iter() 78 + .enumerate() 79 + .filter(|(i, _)| *i != index_to_skip) 80 + .map(|(_, elem)| *elem) 81 + .collect(); 82 + if gradual(&skipped_report) && monotonic(&skipped_report) { 83 + return true; 84 + } 85 + } 86 + false 87 + } 61 88 62 89 fn gradual(report: &Vec<u32>) -> bool { 63 90 for i in 1..report.len() { ··· 95 122 assert_eq!(test_result, "2"); 96 123 let result = day2::day2_part1(include_str!("../input/day2.txt")); 97 124 assert_eq!(result, "213"); 125 + } 126 + #[test] 127 + fn day2_part2_test() { 128 + let test_result = day2::day2_part2(include_str!("../input/day2.test.txt")); 129 + assert_eq!(test_result, "4"); 130 + let result = day2::day2_part2(include_str!("../input/day2.txt")); 131 + assert_eq!(result, "285"); 98 132 } 99 133 100 134 #[test]