You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
9 years ago
|
#![allow(dead_code)]
|
||
9 years ago
|
|
||
|
use regex::Regex;
|
||
|
|
||
9 years ago
|
pub fn solve(input: &str) -> i32 {
|
||
8 years ago
|
input.lines().filter(|&s| is_nice(&s.to_string())).count() as i32
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
fn is_nice(string: &str) -> bool {
|
||
|
has_pair_of_two_letters(string) && has_letter_sandwich(string)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
fn has_three_vowels(string: &str) -> bool {
|
||
|
let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
|
||
|
three_vowels.is_match(string)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
fn has_double_letters(string: &str) -> bool {
|
||
|
string.as_bytes().windows(2).any(|win| win[0] == win[1])
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
fn has_no_substrings(string: &str) -> bool {
|
||
|
!vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
fn has_pair_of_two_letters(string: &str) -> bool {
|
||
|
string.as_bytes().windows(2).any(|win| {
|
||
|
let s = String::from_utf8(win.to_vec()).unwrap();
|
||
|
string.split(&s).count() > 2
|
||
|
})
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
fn has_letter_sandwich(string: &str) -> bool {
|
||
|
string.as_bytes().windows(3).any(|win| win[0] == win[2])
|
||
9 years ago
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_nice() {
|
||
8 years ago
|
assert!(is_nice(&"qjhvhtzxzqqjkmpb".to_string()));
|
||
|
assert!(is_nice(&"xxyxx ".to_string()));
|
||
|
assert!(!is_nice(&"uurcxstgmygtbstg ".to_string()));
|
||
|
assert!(!is_nice(&"ieodomkazucvgmuy ".to_string()));
|
||
9 years ago
|
}
|