Skip to content

Commit dd304bd

Browse files
committed
feat(05/2015): solve first part
1 parent dcffe8f commit dd304bd

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
| [Day 2: I Was Told There Would Be No Math](src/solutions/year2015/day02.rs) | ⭐⭐ | 0.074 | 0.105 |
9292
| [Day 3: Perfectly Spherical Houses in a Vacuum](src/solutions/year2015/day03.rs) | ⭐⭐ | 0.358 | 0.373 |
9393
| [Day 4: The Ideal Stocking Stuffer](src/solutions/year2015/day04.rs) | ⭐⭐ | 66.769 | 1931.428 |
94-
| [Day 5: Doesn't He Have Intern-Elves For This?](src/solutions/year2015/day05.rs) | | - | - |
94+
| [Day 5: Doesn't He Have Intern-Elves For This?](src/solutions/year2015/day05.rs) | | 0.261 | - |
9595

9696
# TODO
9797

src/solutions/year2015/day05.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,78 @@
11
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"];
26

37
pub struct Day05;
48

59
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()
816
}
917

1018
fn part_two(&self, _input: &str) -> String {
1119
String::from("0")
1220
}
1321
}
1422

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+
1543
#[cfg(test)]
1644
mod tests {
1745
use super::*;
1846

1947
#[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"));
2277
}
2378
}

0 commit comments

Comments
 (0)