bastien.monsarrat 6 anni fa
parent
commit
91cc70710e
5 ha cambiato i file con 87 aggiunte e 0 eliminazioni
  1. 6 0
      Adv2018.sln
  2. 10 0
      D22.1/D22.1.csproj
  3. 61 0
      D22.1/Program.cs
  4. 8 0
      D22.1/Properties/launchSettings.json
  5. 2 0
      D22.1/input.txt

+ 6 - 0
Adv2018.sln

@@ -87,6 +87,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D20.1and2", "D20.1\D20.1and
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D21.1and2", "D21.1\D21.1and2.csproj", "{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D22.1", "D22.1\D22.1.csproj", "{E3050FCD-AA84-418E-9257-24E0C174AE82}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -261,6 +263,10 @@ Global
 		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Release|Any CPU.Build.0 = Release|Any CPU
+		{E3050FCD-AA84-418E-9257-24E0C174AE82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{E3050FCD-AA84-418E-9257-24E0C174AE82}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{E3050FCD-AA84-418E-9257-24E0C174AE82}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{E3050FCD-AA84-418E-9257-24E0C174AE82}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 10 - 0
D22.1/D22.1.csproj

@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D22._1</RootNamespace>
+    <LangVersion>7.3</LangVersion>
+  </PropertyGroup>
+
+</Project>

+ 61 - 0
D22.1/Program.cs

@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace D22._1
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) return;
+            if (File.Exists(args[0]) == false) return;
+
+            var file = File.OpenText(args[0]);
+
+            int depth = int.Parse(file.ReadLine().Substring("depth: ".Length));
+            int[] arrayCoord = file.ReadLine().Substring("target: ".Length).Split(",").Select(s => int.Parse(s)).ToArray();
+            (int x, int y) coord = (arrayCoord[0], arrayCoord[1]);
+            file.Close();
+
+            Dictionary<(int, int), int> erosionLevels = new Dictionary<(int, int), int>();
+
+            int risk = 0;
+
+            for (int i = 0; i <= Math.Min(coord.x, coord.y); ++i)
+            {
+                for (int y = i; y <= coord.y; ++y)
+                {
+                    var pos = (i, y);
+                    risk += ComputeRisk(coord, erosionLevels, pos, depth);
+                }
+
+                for (int x = i + 1; x <= coord.x; ++x)
+                {
+                    var pos = (x, i);
+                    risk += ComputeRisk(coord, erosionLevels, pos, depth);
+                }
+            }
+
+            Console.WriteLine($"Total risk is : {risk}");
+        }
+
+        private static int ComputeRisk((int x, int y) coord, Dictionary<(int, int), int> erosionLevels, (int x, int y) pos, int depth)
+        {
+            int geo = 0;
+            if (pos != (0, 0) && pos != coord)
+            {
+                if (pos.x == 0) geo = pos.y * 48271;
+                else if (pos.y == 0) geo = pos.x * 16807;
+                else geo = erosionLevels[(pos.x - 1, pos.y)] * erosionLevels[(pos.x, pos.y - 1)];
+            }
+
+            int ero = (geo + depth) % 20183;
+            int type = ero % 3;
+
+            erosionLevels.Add(pos, ero);
+            return type;
+        }
+    }
+}

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

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D22.1": {
+      "commandName": "Project",
+      "commandLineArgs": "\"..\\..\\..\\..\\D22.1\\input.txt\" "
+    }
+  }
+}

+ 2 - 0
D22.1/input.txt

@@ -0,0 +1,2 @@
+depth: 3339
+target: 10,715