|
1 | 1 | use crate::solutions::Solution; |
| 2 | +use itertools::Itertools; |
| 3 | + |
| 4 | +const VOWELS: [char; 5] = ['a', 'e', 'i', 'o', 'u']; |
| 5 | +const BANNED: [&str; 4] = ["ab", "cd", "pq", "xy"]; |
2 | 6 |
|
3 | 7 | pub struct Day05; |
4 | 8 |
|
5 | 9 | impl Solution for Day05 { |
6 | | - fn part_one(&self, _input: &str) -> String { |
7 | | - String::from("0") |
| 10 | + fn part_one(&self, input: &str) -> String { |
| 11 | + input |
| 12 | + .lines() |
| 13 | + .filter(|word| self.is_nice(word)) |
| 14 | + .count() |
| 15 | + .to_string() |
8 | 16 | } |
9 | 17 |
|
10 | 18 | fn part_two(&self, _input: &str) -> String { |
11 | 19 | String::from("0") |
12 | 20 | } |
13 | 21 | } |
14 | 22 |
|
| 23 | +impl Day05 { |
| 24 | + fn is_nice(&self, word: &str) -> bool { |
| 25 | + self.vowels(word) && self.letter_twice_in_row(word) && self.not_contains_banned(word) |
| 26 | + } |
| 27 | + |
| 28 | + fn vowels(&self, word: &str) -> bool { |
| 29 | + word.chars().filter(|c| VOWELS.contains(c)).count() >= 3 |
| 30 | + } |
| 31 | + |
| 32 | + fn letter_twice_in_row(&self, word: &str) -> bool { |
| 33 | + word.chars().tuple_windows().any(|(c1, c2)| c1 == c2) |
| 34 | + } |
| 35 | + |
| 36 | + fn not_contains_banned(&self, word: &str) -> bool { |
| 37 | + word.chars() |
| 38 | + .tuple_windows() |
| 39 | + .all(|(c1, c2)| !BANNED.contains(&format!("{}{}", c1, c2).as_str())) |
| 40 | + } |
| 41 | +} |
| 42 | + |
15 | 43 | #[cfg(test)] |
16 | 44 | mod tests { |
17 | 45 | use super::*; |
18 | 46 |
|
19 | 47 | #[test] |
20 | | - fn part_one_example_test() { |
21 | | - assert_eq!("0", Day05.part_one("")); |
| 48 | + fn vowels() { |
| 49 | + assert!(Day05.vowels("aei")); |
| 50 | + assert!(Day05.vowels("xazegov")); |
| 51 | + assert!(Day05.vowels("aeiouaeiouaeiou")); |
| 52 | + assert!(!Day05.vowels("aed")); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn letter_twice_in_rowe() { |
| 57 | + assert!(Day05.letter_twice_in_row("abcdde")); |
| 58 | + assert!(!Day05.letter_twice_in_row("abcde")); |
| 59 | + assert!(Day05.letter_twice_in_row("xxabcde")); |
| 60 | + assert!(Day05.letter_twice_in_row("abcdezz")); |
| 61 | + assert!(!Day05.letter_twice_in_row("ababababab")); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn not_contains_banned() { |
| 66 | + assert!(!Day05.not_contains_banned("abcdde")); |
| 67 | + assert!(Day05.not_contains_banned("other")); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn is_nice() { |
| 72 | + assert!(Day05.is_nice("ugknbfddgicrmopn")); |
| 73 | + assert!(Day05.is_nice("aaa")); |
| 74 | + assert!(!Day05.is_nice("jchzalrnumimnmhp")); |
| 75 | + assert!(!Day05.is_nice("haegwjzuvuyypxyu")); |
| 76 | + assert!(!Day05.is_nice("dvszwmarrgswjxmb")); |
22 | 77 | } |
23 | 78 | } |
0 commit comments