From f06aa3b59072589418aaf8d852edc37b53df8550 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Wed, 4 Dec 2019 18:47:39 -0500 Subject: [PATCH] Day 4 --- Advent.csproj.DotSettings | 3 +- Day4/Day4.cs | 63 +++++++++++++++++++++++++++++++++++++++ Program.cs | 3 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 Day4/Day4.cs diff --git a/Advent.csproj.DotSettings b/Advent.csproj.DotSettings index eec121f..05fd445 100644 --- a/Advent.csproj.DotSettings +++ b/Advent.csproj.DotSettings @@ -1,4 +1,5 @@  True True - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/Day4/Day4.cs b/Day4/Day4.cs new file mode 100644 index 0000000..c78f839 --- /dev/null +++ b/Day4/Day4.cs @@ -0,0 +1,63 @@ +using System; +using System.Linq; + +namespace Advent +{ + public static class Day4 + { + public static void Execute() + { + //Console.WriteLine(IsValidPassword("112233")); + //Console.WriteLine(IsValidPassword("123444")); + //Console.WriteLine(IsValidPassword("111122")); + + var count = 0; + + for (var i = 109165; i <= 576723; i++) + { + if (IsValidPassword(i.ToString())) + count++; + } + + Console.WriteLine(count); + } + + private static bool IsValidPassword(string password) + { + var digits = password.ToCharArray(); + + var sortedDigits = password.ToCharArray().OrderBy(c => c); + + if (!digits.SequenceEqual(sortedDigits)) + return false; + + var hasDouble = false; + var index = 0; + var letter = digits[0]; + + while (index < 6) + { + var count = 1; + + while (index < 5 && digits[index + 1] == letter) + { + count++; + index++; + } + + if (count == 2) + { + hasDouble = true; + break; + } + + index++; + + if (index < 6) + letter = digits[index]; + } + + return hasDouble; + } + } +} diff --git a/Program.cs b/Program.cs index 4ee98b7..9bfb434 100644 --- a/Program.cs +++ b/Program.cs @@ -6,7 +6,8 @@ { //Day1.Execute(); //Day2.Execute(); - Day3.Execute(); + //Day3.Execute(); + Day4.Execute(); } } }