bastien.monsarrat 6 years ago
parent
commit
a1c6cb81e8
6 changed files with 134 additions and 0 deletions
  1. 9 0
      14.1/14.1.csproj
  2. 12 0
      14.1/Program.cs
  3. 6 0
      Adv2016.sln
  4. 9 0
      D14.1/D14.csproj
  5. 90 0
      D14.1/Program.cs
  6. 8 0
      D14.1/Properties/launchSettings.json

+ 9 - 0
14.1/14.1.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>_14._1</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 12 - 0
14.1/Program.cs

@@ -0,0 +1,12 @@
+using System;
+
+namespace _14._1
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            Console.WriteLine("Hello World!");
+        }
+    }
+}

+ 6 - 0
Adv2016.sln

@@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.1", "D13.1\D13.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.2", "D13.2\D13.2.csproj", "{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D14", "D14.1\D14.csproj", "{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -153,6 +155,10 @@ Global
 		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Release|Any CPU.Build.0 = Release|Any CPU
+		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D14.1/D14.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D14._1</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 90 - 0
D14.1/Program.cs

@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Generic;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace D14._1
+{
+    class Program
+    {
+        static byte[] ToB(string str) => Encoding.ASCII.GetBytes(str);
+        static string ToS(byte[] b) => BitConverter.ToString(b).Replace("-", "").ToLower();
+
+        static bool HasQuintuplet(string str, char c)
+        {
+            for (int i = 4; i < str.Length; ++i)
+                if (str[i - 4] == str[i - 3] && str[i - 3] == str[i - 2] && str[i - 2] == str[i - 1] && str[i - 1] == str[i] && str[i] == c)
+                    return true;
+            return false;
+        }
+
+        static char? HasTriplet(string str)
+        {
+            for (int i = 2; i < str.Length; ++i)
+                if (str[i - 2] == str[i - 1] && str[i - 1] == str[i])
+                    return str[i];
+            return null;
+        }
+
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            string input = args[0];
+
+            int index1 = GetHashes(input, 0);
+            Console.WriteLine($"Index for Part 1 is : {index1}");
+
+            int index2 = GetHashes(input, 2016);
+            Console.WriteLine($"Index for Part 2 is : {index2}");
+        }
+
+        static Dictionary<int, string> cache = new Dictionary<int, string>();
+        private static int GetHashes(string input, int stretching)
+        {
+            cache.Clear();
+            int index = -1;
+
+            using (var md5 = MD5.Create())
+            {
+                var found = 0;
+
+                while (found < 64)
+                {
+                    index++;
+
+                    string hash = TryGetHash(input, index, md5, stretching);
+
+                    var triplet = HasTriplet(hash);
+                    if (triplet.HasValue == false) continue;
+
+                    for (var i = index + 1; i < index + 1000; ++i)
+                    {
+                        string h = TryGetHash(input, i, md5, stretching);
+                        if (HasQuintuplet(h, triplet.Value))
+                        {
+                            found++;
+                            break;
+                        }
+                    }
+                }
+            }
+
+            return index;
+        }
+
+        private static string TryGetHash(string input, int index, MD5 md5, int stretching)
+        {
+            if (cache.ContainsKey(index)) return cache[index];
+            var hash = GetHash(input, index, md5, stretching);
+            cache.Add(index, hash);
+            return hash;
+        }
+
+        private static string GetHash(string input, int index, MD5 md5, int stretching)
+        {
+            var hash = ToS(md5.ComputeHash(ToB($"{input}{index}")));
+            for (var i = 0; i < stretching; ++i) hash = ToS(md5.ComputeHash(ToB(hash)));
+            return hash;
+        }
+    }
+}

+ 8 - 0
D14.1/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D14.1": {
+      "commandName": "Project",
+      "commandLineArgs": "ahsbgdzn"
+    }
+  }
+}