fn main() { let input = advent::read_lines(5); println!("5a: {}", count_nice(&input, is_nice)); println!("5b: {}", count_nice(&input, is_nice2)); } fn is_nice(word: &str) -> bool { let mut prev = ' '; let mut vowel_count = 0; let mut double_letter = false; for c in word.chars() { if ['a', 'e', 'i', 'o', 'u'].contains(&c) { vowel_count += 1; } if prev == c { double_letter = true; } let pair = format!("{}{}", prev, c); if ["ab".to_string(), "cd".to_string(), "pq".to_string(), "xy".to_string()].contains(&pair) { return false; } prev = c; } double_letter && vowel_count >= 3 } fn has_two_pairs(word: &str) -> bool { let mut prev = ' '; for c in word.chars() { let pair = format!("{}{}", prev, c); if word.matches(&pair).count() > 1 { return true; } prev = c; } false } fn has_one_letter_repeat(word: &str) -> bool { let mut prev = ' '; let mut prevprev = ' '; for c in word.chars() { if c == prevprev { return true; } prevprev = prev; prev = c; } false } fn is_nice2(word: &str) -> bool { has_two_pairs(word) && has_one_letter_repeat(word) } fn count_nice(input: &[String], f: fn(&str) -> bool) -> usize { input.iter() .filter(|&x| f(x)) .count() } #[cfg(test)] mod tests { use super::*; #[test] fn test() { assert!(is_nice("ugknbfddgicrmopn")); assert!(is_nice("aaa")); assert!(!is_nice("jchzalrnumimnmhp")); assert!(!is_nice("haegwjzuvuyypxyu")); assert!(!is_nice("dvszwmarrgswjxmb")); assert!(is_nice2("qjhvhtzxzqqjkmpb")); assert!(is_nice2("xxyxx")); assert!(!is_nice2("uurcxstgmygtbstg")); assert!(!is_nice2("ieodomkazucvgmuy")); } }