diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 3944b09..e1eafe4 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -16,6 +16,7 @@ public class Test2015 [DataRow(typeof(Day08), "1342", "2074")] [DataRow(typeof(Day09), "117", "909")] [DataRow(typeof(Day10), "492982", "6989950")] + [DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); diff --git a/AOC2015/Day11.cs b/AOC2015/Day11.cs index 1ab6032..2afbd51 100644 --- a/AOC2015/Day11.cs +++ b/AOC2015/Day11.cs @@ -5,11 +5,64 @@ namespace AOC2015; /// public sealed class Day11 : Day { - public Day11() : base(2015, 11, "Puzzle Name") + private char[] _password; + + public Day11() : base(2015, 11, "Corporate Policy") { + _password = Input.First().ToCharArray(); } - public override object Part1() => ""; + public override object Part1() + { + while (!IsValid(ref _password)) Increment(ref _password); - public override object Part2() => ""; -} + return new string(_password); + } + + public override object Part2() + { + Increment(ref _password); + while (!IsValid(ref _password)) Increment(ref _password); + + return new string(_password); + } + + private static bool IsValid(ref char[] password) + { + bool check1 = false, check3 = false; + for (var j = 2; j < password.Length; j++) + { + if (password[j] == 'i' || password[j] == 'o' || password[j] == 'l') + return false; + + if (password[j - 2] + 1 == password[j - 1] && password[j - 1] + 1 == password[j]) + check1 = true; + + if (j <= 2) continue; + + for (var k = 0; k + 2 < j; k++) + if (password[j - 3 - k] == password[j - 2 - k] + && password[j - 1] == password[j] + && password[j - 2 - k] != password[j - 1]) + { + check3 = true; + } + } + + return check1 && check3; + } + + private static void Increment(ref char[] password, int at = -1) + { + while (true) + { + if (at == -1) at = password.Length - 1; + + password[at]++; + if (password[at] == 'i' || password[at] == 'o' || password[at] == 'l') password[at]++; + if (password[at] <= 'z') return; + password[at] = 'a'; + at--; + } + } +} \ No newline at end of file