1
0
bastien.monsarrat 6 жил өмнө
parent
commit
b099b88372

+ 7 - 1
Adv2018.sln

@@ -49,7 +49,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D11.1", "D11.1\D11.1.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D11.2", "D11.2\D11.2.csproj", "{9CAA4935-5795-4D99-8557-53A8B1250477}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.1", "D12.1\D12.1.csproj", "{6DE5A0FF-CE37-4454-B6CE-7BA4CBFC4C69}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D12.1", "D12.1\D12.1.csproj", "{6DE5A0FF-CE37-4454-B6CE-7BA4CBFC4C69}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.2", "D12.2\D12.2.csproj", "{86070C5D-C898-4A3D-882C-611B7A19709C}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -153,6 +155,10 @@ Global
 		{6DE5A0FF-CE37-4454-B6CE-7BA4CBFC4C69}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{6DE5A0FF-CE37-4454-B6CE-7BA4CBFC4C69}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{6DE5A0FF-CE37-4454-B6CE-7BA4CBFC4C69}.Release|Any CPU.Build.0 = Release|Any CPU
+		{86070C5D-C898-4A3D-882C-611B7A19709C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{86070C5D-C898-4A3D-882C-611B7A19709C}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{86070C5D-C898-4A3D-882C-611B7A19709C}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{86070C5D-C898-4A3D-882C-611B7A19709C}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 7 - 1
D12.1/Program.cs

@@ -33,13 +33,19 @@ namespace D12._1
             {
                 string nstate = "";
 
-                for (int j = -2; j < state.Length+2; ++j)
+                for (int j = -2; j < state.Length + 2; ++j)
                     nstate += getState(state, j, ruleset);
 
                 left -= 2;
                 state = nstate;
+
+                CalcResult(state, left);
             }
+        }
 
+        private static void CalcResult(string state, int l)
+        {
+            int left = l;
             int result = 0;
             foreach (char c in state)
             {

+ 9 - 0
D12.2/D12.2.csproj

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

+ 67 - 0
D12.2/Program.cs

@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace D12._2
+{
+    class Rules : Dictionary<string, char> { }
+    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]);
+
+            var state = file.ReadLine().Substring(@"initial state: ".Length);
+            file.ReadLine();
+
+            var ruleset = new Rules();
+
+            do
+            {
+                var line = file.ReadLine();
+                if (line == null) break;
+
+                var s = line.Split(" => ");
+                ruleset.Add(s[0], s[1][0]);
+
+            } while (true);
+
+            int left = 0;
+            for (int i = 0; i < 20; ++i)
+            {
+                string nstate = "";
+
+                for (int j = -2; j < state.Length + 2; ++j)
+                    nstate += getState(state, j, ruleset);
+
+                left -= 2;
+                state = nstate;
+
+                CalcResult(state, left);
+            }
+        }
+
+        private static void CalcResult(string state, int l)
+        {
+            int left = l;
+            int result = 0;
+            foreach (char c in state)
+            {
+                if (c == '#') result += left;
+                left++;
+            }
+
+            Console.WriteLine(result);
+        }
+
+        static char getState(string state, int p, Rules rules)
+        {
+            var sb = "";
+            for (var i = -2; i <= 2; ++i)
+                sb += p + i < 0 || p + i >= state.Length ? '.' : state[p + i];
+            return rules[sb];
+        }
+    }
+}

+ 8 - 0
D12.2/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D12.2": {
+      "commandName": "Project",
+      "commandLineArgs": "\"D:\\adv\\Adv2018\\D12.1\\input.txt\" "
+    }
+  }
+}