From 48d6f0eaa3b976ff5244912363838f322f516c11 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Fri, 11 Nov 2022 17:02:28 -0500 Subject: [PATCH] 2015 day 5 --- AOC.Test/Test2015.cs | 2 ++ AOC2015/Day05.cs | 34 ++++++++++++++++++++++++++++++---- AOC2015/input2015/test05.in | 7 +++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 AOC2015/input2015/test05.in diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 08adb01..6843069 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -10,6 +10,7 @@ public class Test2015 [DataRow(typeof(Day02),"1586300", "3737498")] [DataRow(typeof(Day03), "2081", "2341")] // [DataRow(typeof(Day04), "346386", "9958218")] + [DataRow(typeof(Day05), "258", "53")] public void TestAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -20,6 +21,7 @@ public class Test2015 [DataRow(typeof(Day02), "58", "34")] [DataRow(typeof(Day03), "2", "11")] // [DataRow(typeof(Day04), "609043", "6742839")] + [DataRow(typeof(Day05), "1", "1")] public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/AOC2015/Day05.cs b/AOC2015/Day05.cs index 687c5f0..00822ff 100644 --- a/AOC2015/Day05.cs +++ b/AOC2015/Day05.cs @@ -3,13 +3,39 @@ /// /// Day 5: /// -public sealed class Day05 : Day +public sealed partial class Day05 : Day { - public Day05() : base(2015, 5, "Puzzle Name") + private readonly List _strings; + private static readonly List Vowels = new() { 'a', 'e', 'i', 'o', 'u' }; + + [GeneratedRegex(@"(.)\1")] + private static partial Regex DoubleLetter(); + + [GeneratedRegex(@"(.).\1")] + private static partial Regex LetterSandwich(); + + [GeneratedRegex(@"(..).*\1")] + private static partial Regex TwoPairs(); + + public Day05() : base(2015, 5, "Doesn't He Have Intern-Elves For This?") { + _strings = Input.Where(line => !string.IsNullOrEmpty(line)).ToList(); } - public override object Part1() => ""; + public override object Part1() => + _strings.Count(s => + { + // bad substrings + if (s.Contains("ab") || s.Contains("cd") || s.Contains("pq") || s.Contains("xy")) + return false; - public override object Part2() => ""; + // must have at least 3 vowels + if (s.Count(c => Vowels.Contains(c)) < 3) return false; + + // must have a double-letter somewhere + return DoubleLetter().Matches(s).Count >= 1; + }); + + public override object Part2() => + _strings.Count(s => LetterSandwich().Matches(s).Count >= 1 && TwoPairs().Matches(s).Count == 1); } diff --git a/AOC2015/input2015/test05.in b/AOC2015/input2015/test05.in new file mode 100644 index 0000000..e7ab0e9 --- /dev/null +++ b/AOC2015/input2015/test05.in @@ -0,0 +1,7 @@ +ugknbfddgicrmopn +jchzalrnumimnmhp +haegwjzuvuyypxyu +dvszwmarrgswjxmb +qjhvhtzxzqqjkmpb +uurcxstgmygtbstg +ieodomkazucvgmuy