Skip to content

Commit b0983d6

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

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
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) | | 0.261 | - |
94+
| [Day 5: Doesn't He Have Intern-Elves For This?](src/solutions/year2015/day05.rs) | | 0.261 | 3.733 |
9595

9696
# TODO
9797

src/solutions/year2015/day05.rs

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ impl Solution for Day05 {
1010
fn part_one(&self, input: &str) -> String {
1111
input
1212
.lines()
13-
.filter(|word| self.is_nice(word))
13+
.filter(|word| self.is_nice_part_one(word))
1414
.count()
1515
.to_string()
1616
}
1717

18-
fn part_two(&self, _input: &str) -> String {
19-
String::from("0")
18+
fn part_two(&self, input: &str) -> String {
19+
input
20+
.lines()
21+
.filter(|word| self.is_nice_part_two(word))
22+
.count()
23+
.to_string()
2024
}
2125
}
2226

2327
impl Day05 {
24-
fn is_nice(&self, word: &str) -> bool {
28+
fn is_nice_part_one(&self, word: &str) -> bool {
2529
self.vowels(word) && self.letter_twice_in_row(word) && self.not_contains_banned(word)
2630
}
2731

@@ -38,12 +42,45 @@ impl Day05 {
3842
.tuple_windows()
3943
.all(|(c1, c2)| !BANNED.contains(&format!("{}{}", c1, c2).as_str()))
4044
}
45+
46+
fn is_nice_part_two(&self, word: &str) -> bool {
47+
self.pair_twice(word) && self.repeated_with_letter_between(word)
48+
}
49+
50+
fn pair_twice(&self, word: &str) -> bool {
51+
let vec = word.chars().collect_vec();
52+
53+
for (i, w) in vec.windows(2).enumerate() {
54+
let pair = format!("{}{}", w[0], w[1]);
55+
56+
for j in i + 2..vec.len() - 1 {
57+
if pair == format!("{}{}", vec[j], vec[j + 1]) {
58+
return true;
59+
}
60+
}
61+
}
62+
63+
false
64+
}
65+
66+
fn repeated_with_letter_between(&self, word: &str) -> bool {
67+
word.chars().tuple_windows().any(|(c1, _, c2)| c1 == c2)
68+
}
4169
}
4270

4371
#[cfg(test)]
4472
mod tests {
4573
use super::*;
4674

75+
#[test]
76+
fn is_nice_part_one() {
77+
assert!(Day05.is_nice_part_one("ugknbfddgicrmopn"));
78+
assert!(Day05.is_nice_part_one("aaa"));
79+
assert!(!Day05.is_nice_part_one("jchzalrnumimnmhp"));
80+
assert!(!Day05.is_nice_part_one("haegwjzuvuyypxyu"));
81+
assert!(!Day05.is_nice_part_one("dvszwmarrgswjxmb"));
82+
}
83+
4784
#[test]
4885
fn vowels() {
4986
assert!(Day05.vowels("aei"));
@@ -53,7 +90,7 @@ mod tests {
5390
}
5491

5592
#[test]
56-
fn letter_twice_in_rowe() {
93+
fn letter_twice_in_row() {
5794
assert!(Day05.letter_twice_in_row("abcdde"));
5895
assert!(!Day05.letter_twice_in_row("abcde"));
5996
assert!(Day05.letter_twice_in_row("xxabcde"));
@@ -68,11 +105,25 @@ mod tests {
68105
}
69106

70107
#[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"));
108+
fn is_nice_part_two() {
109+
assert!(Day05.is_nice_part_two("qjhvhtzxzqqjkmpb"));
110+
assert!(Day05.is_nice_part_two("xxyxx"));
111+
assert!(!Day05.is_nice_part_two("uurcxstgmygtbstg"));
112+
assert!(!Day05.is_nice_part_two("ieodomkazucvgmuy"));
113+
}
114+
115+
#[test]
116+
fn pair_twice() {
117+
assert!(Day05.pair_twice("xyxy"));
118+
assert!(Day05.pair_twice("aabcdefgaa"));
119+
assert!(!Day05.pair_twice("aaa"));
120+
}
121+
122+
#[test]
123+
fn repeated_with_letter_between() {
124+
assert!(Day05.repeated_with_letter_between("xyx"));
125+
assert!(!Day05.repeated_with_letter_between("xx"));
126+
assert!(Day05.repeated_with_letter_between("abcdefeghi"));
127+
assert!(Day05.repeated_with_letter_between("aaa"));
77128
}
78129
}

0 commit comments

Comments
 (0)