:)

day2 part2

+29
+29
src/lib.rs
··· 65 let invalid_sum: u64 = invalid.sum(); 66 invalid_sum.to_string() 67 } 68 fn parse(input: &str) -> Vec<RangeInclusive<u64>> { 69 input 70 .split(',') ··· 82 first_half != second_half 83 } 84 } 85 } 86 87 #[cfg(test)] 88 mod tests { 89 use super::*; 90 91 #[test] 92 fn day2_part1_test() { 93 let test_result = day2::day2_part1(include_str!("../input/day2.test.txt")); 94 assert_eq!(test_result, "1227775554"); 95 let result = day2::day2_part1(include_str!("../input/day2.txt")); 96 assert_eq!(result, "38158151648"); 97 } 98 99 #[test]
··· 65 let invalid_sum: u64 = invalid.sum(); 66 invalid_sum.to_string() 67 } 68 + pub fn day2_part2(input: &str) -> String { 69 + let ranges = parse(input); 70 + let invalid = ranges.into_iter().flatten().filter(|&id| !is_id_doubly_valid(id)); 71 + let invalid_sum: u64 = invalid.sum(); 72 + invalid_sum.to_string() 73 + } 74 fn parse(input: &str) -> Vec<RangeInclusive<u64>> { 75 input 76 .split(',') ··· 88 first_half != second_half 89 } 90 } 91 + fn is_id_doubly_valid(id: u64) -> bool { 92 + let chars = id.to_string().chars().collect::<Vec<char>>(); 93 + let charnum = chars.len(); 94 + for chunk_count in 2..=charnum { 95 + if charnum % chunk_count != 0 { 96 + continue; 97 + } 98 + let chunks = chars.chunks_exact(charnum / chunk_count).collect::<Vec<_>>(); 99 + if chunks.iter().all(|&chunk| chunk == chunks[0]) { 100 + return false 101 + } 102 + } 103 + true 104 + } 105 } 106 107 #[cfg(test)] 108 mod tests { 109 use super::*; 110 111 + #[ignore] 112 #[test] 113 fn day2_part1_test() { 114 let test_result = day2::day2_part1(include_str!("../input/day2.test.txt")); 115 assert_eq!(test_result, "1227775554"); 116 let result = day2::day2_part1(include_str!("../input/day2.txt")); 117 assert_eq!(result, "38158151648"); 118 + } 119 + #[ignore] 120 + #[test] 121 + fn day2_part2_test() { 122 + let test_result = day2::day2_part2(include_str!("../input/day2.test.txt")); 123 + assert_eq!(test_result, "4174379265"); 124 + let result = day2::day2_part2(include_str!("../input/day2.txt")); 125 + assert_eq!(result, "45283684555"); 126 } 127 128 #[test]