--- Day 5: Doesn’t He Have Intern-Elves For This? ---
In today’s puzzle, we need to help Santa figuring out which strings in his text file are naughty or nice.
Table of Contents
Part 1
A nice string is one with all of the following properties:
- It contains at least three vowels (aeiou only), like
aei
,xazegov
, oraeiouaeiouaeiou
. - It contains at least one letter that appears twice in a row, like
xx
,abcdde
(dd), oraabbccdd
(aa, bb, cc, or dd). - It does not contain the strings
ab
,cd
,pq
, orxy
, even if they are part of one of the other requirements.
The easiest way to solve this part is to use regular expressions.
public string Part1(IEnumerable<string> input)
{
bool HasThreeVowels(string str) =>
Regex.Matches(str, @"[aeiou]").Count >= 3;
bool HasDoubledLetter(string str) =>
Regex.IsMatch(str, @"(\w)\1");
bool ContainsNaughtyStrings(string str) =>
Regex.IsMatch(str, @"ab|cd|pq|xy");
bool IsNiceString(string str) =>
HasThreeVowels(str)
&& HasDoubledLetter(str)
&& !ContainsNaughtyStrings(str);
var niceStrings = input.Count(IsNiceString);
return niceStrings.ToString();
}
Part 2
In the second part we have new properties:
- It contains a pair of any two letters that appears at least twice in the string without overlapping, like
xyxy
(xy) oraabcdefgaa
(aa), but not likeaaa
(aa, but it overlaps). - It contains at least one letter which repeats with exactly one letter between them, like
xyx
,abcdefeghi
(efe), or evenaaa
.
The solution is very much like the first part, except now we have two methods and different regular expressions.
public string Part2(IEnumerable<string> input)
{
bool HasPair(string str) =>
Regex.IsMatch(str, @"(\w{2}).*\1");
bool HasDuplicate(string str) =>
Regex.IsMatch(str, @"(\w).\1");
bool IsNiceString(string str) =>
HasDuplicate(str)
&& HasPair(str);
var niceStrings = input.Count(IsNiceString);
return niceStrings.ToString();
}
Links: