parent
180a73c1d1
commit
3e2cbb4454
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,54 @@
|
||||
use std::io;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use day::Day;
|
||||
|
||||
pub struct Day05 {
|
||||
input: String,
|
||||
}
|
||||
|
||||
impl Day05 {
|
||||
fn is_nice(string: &String) -> bool {
|
||||
Day05::has_three_vowels(string) &&
|
||||
Day05::has_double_letters(string) &&
|
||||
Day05::has_no_substrings(string)
|
||||
}
|
||||
|
||||
fn has_three_vowels(string: &String) -> bool {
|
||||
let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
|
||||
three_vowels.is_match(string)
|
||||
}
|
||||
|
||||
fn has_double_letters(string: &String) -> bool {
|
||||
for win in string.as_bytes().windows(2) {
|
||||
if win[0] == win[1] {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn has_no_substrings(string: &String) -> bool {
|
||||
!vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl Day for Day05 {
|
||||
fn new(input: String) -> Day05 {
|
||||
Day05 { input: input }
|
||||
}
|
||||
|
||||
fn solve(&self) -> io::Result<i32> {
|
||||
Ok(self.input.lines().filter(|&s| Day05::is_nice(&s.to_string())).count() as i32)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nice() {
|
||||
assert!( Day05::is_nice(&"ugknbfddgicrmopn".to_string()));
|
||||
assert!( Day05::is_nice(&"aaa".to_string()));
|
||||
assert!(!Day05::is_nice(&"jchzalrnumimnmhp".to_string()));
|
||||
assert!(!Day05::is_nice(&"haegwjzuvuyypxyu".to_string()));
|
||||
assert!(!Day05::is_nice(&"dvszwmarrgswjxmb".to_string()));
|
||||
}
|
Loading…
Reference in new issue