feat(2015-05): Add day 5 solution

This commit is contained in:
Josh Creek
2022-10-17 22:55:53 +01:00
parent 6ea37b30d4
commit 5e1f597fdf
3 changed files with 1222 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+191
View File
@@ -0,0 +1,191 @@
using Creek.HelpfulExtensions;
using System.Reflection;
using System.Text.RegularExpressions;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day05
{
protected string GetThisClassName() { return this.GetType().Name; }
private string[] realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"));
}
private static bool HasRepeatedCharacters(string input)
{
bool hasRepeatedCharacters = false;
if (input.Length >= 2)
{
for (int index = 0; index < input.Length - 1; index++)
{
if (input[index] == input[index + 1])
{
hasRepeatedCharacters = true;
}
}
}
return hasRepeatedCharacters;
}
private static bool IsNice(string checkString)
{
// Count non-unique vowels
Regex rx = new("[aeiou]");
int vowelCount = rx.Matches(checkString).Count;
// Check for at least one letter appearing twice in a row
bool hasRepeatedCharacters = HasRepeatedCharacters(checkString);
// Check it does not contain the strings ab, cd, pq, or xy
bool containsForbiddenStrings = checkString.Contains("ab")
|| checkString.Contains("cd")
|| checkString.Contains("pq")
|| checkString.Contains("xy");
return vowelCount >= 3 && hasRepeatedCharacters && !containsForbiddenStrings;
}
private class Pair
{
public int LeftIndex { get; set; }
public int RightIndex { get; set; }
public string? Text { get; set; }
}
private static bool TwiceWithoutOverlap(string checkString)
{
// It contains a pair of any two letters that appears at least twice in the string without overlapping,
// like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
// Split the string into 'pairs'
List<Pair> pairs = new();
for (int i = 0; i < checkString.Length - 1; i++)
{
Pair pair = new()
{
LeftIndex = i,
RightIndex = i + 1,
Text = $"{checkString[i]}{checkString[i + 1]}",
};
pairs.Add(pair);
}
List<Pair> possibleNonOverlappingPairs = new();
// Check if any of those pairs occurs more than once
foreach (Pair pair in pairs)
{
int count = Regex.Matches(checkString, pair.Text).Count;
if (count > 1)
{
possibleNonOverlappingPairs.Add(pair);
}
}
// Check if any do not overlap, if they don't then return true
foreach (Pair pair in possibleNonOverlappingPairs)
{
if (possibleNonOverlappingPairs.Any(p =>
p.Text == pair.Text
&& (p.LeftIndex != pair.RightIndex || p.RightIndex != pair.LeftIndex)
))
{
return true;
}
}
return false;
}
private static bool CheckRepeatedCharacters(string checkString)
{
// It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
for (int i = 0; i < checkString.Length; i++)
{
if ((i + 2) < checkString.Length)
{
if (checkString[i] == checkString[i + 2] && checkString[i] != checkString[i + 1])
{
return true;
}
}
}
return false;
}
private static bool IsNiceV2(string checkString)
{
// It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
bool twiceWithoutOverlapping = TwiceWithoutOverlap(checkString);
// It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
bool hasRepeatedCharacters = CheckRepeatedCharacters(checkString);
return twiceWithoutOverlapping && hasRepeatedCharacters;
}
[TestCase("ugknbfddgicrmopn", 1)]
[TestCase("aaa", 1)]
[TestCase("jchzalrnumimnmhp", 0)]
[TestCase("haegwjzuvuyypxyu", 0)]
[TestCase("dvszwmarrgswjxmb", 0)]
[TestCase(null, 258)] // The actual answer
public void Part1(string input, int? expected)
{
string[] lines = input != null ? new[] { input } : realData;
int niceCount = 0;
foreach (string line in lines)
{
if (IsNice(line))
{
niceCount += 1;
}
}
if (expected != null)
{
Assert.That(niceCount, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {niceCount}");
}
[TestCase("qjhvhtzxzqqjkmpb", 1)]
[TestCase("xxyxx", 1)]
[TestCase("uurcxstgmygtbstg", 0)]
[TestCase("ieodomkazucvgmuy", 0)]
[TestCase(null, 53)] // The actual answer
public void Part2(string input, int? expected)
{
string[] lines = input != null ? new[] { input } : realData;
int niceCount = 0;
foreach (string line in lines)
{
if (IsNiceV2(line))
{
niceCount += 1;
}
}
if (expected != null)
{
Assert.That(niceCount, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {niceCount}");
}
}
}
@@ -0,0 +1,31 @@
--- Day 5: Doesn't He Have Intern-Elves For This? ---
Santa needs help figuring out which strings in his text file are naughty or nice.
A nice string is one with all of the following properties:
It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou.
It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd).
It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
For example:
ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...), a double letter (...dd...), and none of the disallowed substrings.
aaa is nice because it has at least three vowels and a double letter, even though the letters used by different rules overlap.
jchzalrnumimnmhp is naughty because it has no double letter.
haegwjzuvuyypxyu is naughty because it contains the string xy.
dvszwmarrgswjxmb is naughty because it contains only one vowel.
How many strings are nice?
--- Part Two ---
Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
Now, a nice string is one with all of the following properties:
It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
For example:
qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz).
xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap.
uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them.
ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
How many strings are nice under these new rules?