bastien.monsarrat 6 years ago
parent
commit
08cdeed5e6
5 changed files with 72 additions and 0 deletions
  1. 6 0
      Adv2016.sln
  2. 9 0
      D18.1/D18.csproj
  3. 48 0
      D18.1/Program.cs
  4. 8 0
      D18.1/Properties/launchSettings.json
  5. 1 0
      D18.1/input.txt

+ 6 - 0
Adv2016.sln

@@ -61,6 +61,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D17.1", "D17.1\D17.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D17.2", "D17.2\D17.2.csproj", "{428544E0-4EB6-42FE-B6F2-8334726F34E5}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D18", "D18.1\D18.csproj", "{ABDF44A3-B648-41A0-9A17-356743AE264B}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -183,6 +185,10 @@ Global
 		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Release|Any CPU.Build.0 = Release|Any CPU
+		{ABDF44A3-B648-41A0-9A17-356743AE264B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{ABDF44A3-B648-41A0-9A17-356743AE264B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{ABDF44A3-B648-41A0-9A17-356743AE264B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{ABDF44A3-B648-41A0-9A17-356743AE264B}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D18.1/D18.csproj

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

+ 48 - 0
D18.1/Program.cs

@@ -0,0 +1,48 @@
+using System;
+using System.IO;
+using System.Linq;
+
+namespace D18._1
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            string input = File.ReadAllText(args[0]).Trim();
+
+            Console.WriteLine($"Part 1 : There is {CountSafeTiles(input, 40)} safe tiles.");
+            Console.WriteLine($"Part 2 : There is {CountSafeTiles(input, 400000)} safe tiles.");
+        }
+
+        private static int CountSafeTiles(string input, int rows)
+        {
+            var line = new bool[input.Length];
+            int safe = 0;
+            for (var i = 0; i < input.Length; ++i)
+            {
+                line[i] = input[i] == '^' ? true : false;
+                if (line[i] == false) safe++;
+            }
+
+            for (var r = 1; r < rows; ++r)
+            {
+                var nline = new bool[line.Length];
+                for (var i = 0; i < line.Length; ++i)
+                {
+                    var left = i == 0 ? false : line[i - 1];
+                    var center = line[i];
+                    var right = i == line.Length - 1 ? false : line[i + 1];
+
+                    if (left && center && right == false) nline[i] = true;
+                    else if (center && right && left == false) nline[i] = true;
+                    else if (left && center == false && right == false) nline[i] = true;
+                    else if (left == false && center == false && right) nline[i] = true;
+                    else safe++;
+                }
+                line = nline;
+            }
+
+            return safe;
+        }
+    }
+}

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

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

+ 1 - 0
D18.1/input.txt

@@ -0,0 +1 @@
+^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^.