// Santa needs help figuring out which strings in his text file are naughty or nice. // // A nice string is one with all of the following properties: // // It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou. // It contains at least one letter that appears twice in a row, like xx, abcdde (dd), // or aabbccdd (aa, bb, cc, or dd). // It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other // requirements. // For example: // // ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...), a double letter // (...dd...), and none of the disallowed substrings. // aaa is nice because it has at least three vowels and a double letter, even though the letters // used by different rules overlap. // jchzalrnumimnmhp is naughty because it has no double letter. // haegwjzuvuyypxyu is naughty because it contains the string xy. // dvszwmarrgswjxmb is naughty because it contains only one vowel. // How many strings are nice? // use core::read_data; fn no_banned_parts(to_check: &str) -> bool { !to_check.contains("ab") && !to_check.contains("cd") && !to_check.contains("pq") && !to_check.contains("xy") } fn has_doubled_letter(to_check: &str) -> bool { let mut has_doubled = false; let mut last_char = ' '; for current in to_check.chars() { if current == last_char { has_doubled = true } last_char = current } has_doubled } fn contains_three_vowels(to_check: &str) -> bool { let mut num_vowels = 0; for current in to_check.chars() { match current { 'a' | 'e' | 'i' | 'o' | 'u' => { num_vowels += 1; } _ => {} } } num_vowels >= 3 } fn is_nice(to_check: &str) -> bool { let vowels = contains_three_vowels(to_check); let has_doubled = has_doubled_letter(to_check); let no_banned = no_banned_parts(to_check); // println!("{} -> V{} D{} B{}", to_check, vowels, has_doubled, no_banned); vowels && has_doubled && no_banned } fn main() { println!("ugknbfddgicrmopn is nice -> {}", is_nice("ugknbfddgicrmopn")); println!("aaa is nice -> {}", is_nice("aaa")); println!("jchzalrnumimnmhp is NOT nice -> {}", is_nice("jchzalrnumimnmhp")); println!("haegwjzuvuyypxyu is NOT nice -> {}", is_nice("haegwjzuvuyypxyu")); println!("dvszwmarrgswjxmb is NOT nice -> {}", is_nice("dvszwmarrgswjxmb")); let binding = read_data("2015_05_data.txt"); let lines = binding.lines(); let mut nice = 0; let num_lines = lines.clone().count(); for line in lines { if is_nice(line) { nice += 1; } } println!("There are {} nice out of {} words.", nice, num_lines); } // 236