1
0
Glærferyn 6 жил өмнө
parent
commit
aa2e40c4dd

+ 7 - 1
Adv2018.sln

@@ -73,7 +73,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D16.2", "D16.2\D16.2.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D17.1and2", "D17.1\D17.1and2.csproj", "{584D1E60-73D7-44F9-A944-65525DF9A7A1}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D18.1", "D18.1\D18.1.csproj", "{92B12539-90F5-4B96-B9BD-C8BC25A961B9}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D18.1", "D18.1\D18.1.csproj", "{92B12539-90F5-4B96-B9BD-C8BC25A961B9}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D18.2", "D18.2\D18.2.csproj", "{8197F29E-B659-487A-B91B-DE58E19A4365}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -225,6 +227,10 @@ Global
 		{92B12539-90F5-4B96-B9BD-C8BC25A961B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{92B12539-90F5-4B96-B9BD-C8BC25A961B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{92B12539-90F5-4B96-B9BD-C8BC25A961B9}.Release|Any CPU.Build.0 = Release|Any CPU
+		{8197F29E-B659-487A-B91B-DE58E19A4365}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{8197F29E-B659-487A-B91B-DE58E19A4365}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{8197F29E-B659-487A-B91B-DE58E19A4365}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{8197F29E-B659-487A-B91B-DE58E19A4365}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 163 - 0
D18.2/Program.cs

@@ -0,0 +1,163 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace D18._2
+{
+    class Program
+    {
+        enum EnumType { Open = 0, Trees, Lumber }
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) return;
+            if (File.Exists(args[0]) == false) return;
+            var file = File.OpenText(args[0]);
+
+            const int size = 50;
+
+            var map = new (EnumType type, (int o, int t, int l) adj)[size, size];
+            CreateMap(file, map);
+
+            (int o, int t, int l) count = (0, 0, 0);
+
+            var prev = new Dictionary<(int, int, int), int>();
+            var dup = new Dictionary<(int, int, int), int>();
+
+            int minutes = 1;
+            do
+            {
+                FillAdjacency(size, map);
+
+                count = NextState(size, map);
+
+                if (prev.TryGetValue(count, out int prevminute))
+                {
+                    if (dup.TryGetValue(count, out int dupminute))
+                    {
+                        var period = minutes - prevminute;
+                        var leftover = minutes % period;
+                        if (minutes % period == 1_000_000_000 % period)
+                        {
+                            // this line seems not to make the schmilblick going forward
+                            // for (var i = 0; i < leftover; ++i) count = NextState(size, map);
+                            break;
+                        }
+                    }
+
+                    dup.TryAdd(count, minutes);
+                }
+
+                prev[count] = minutes;
+            } while (minutes++ > 0);
+
+            int result = count.l * count.t;
+            Console.WriteLine($"The result is : {result}");
+        }
+
+        private static (int o, int t, int l) NextState(int size, (EnumType type, (int o, int t, int l) adj)[,] map)
+        {
+            (int o, int t, int l) count = (0, 0, 0);
+            for (var y = 0; y < size; ++y)
+            {
+                for (var x = 0; x < size; ++x)
+                {
+                    var tile = map[y, x];
+                    if (tile.type == EnumType.Open)
+                    {
+                        if (tile.adj.t >= 3) map[y, x].type = EnumType.Trees;
+                    }
+
+                    else if (tile.type == EnumType.Trees)
+                    {
+                        if (tile.adj.l >= 3) map[y, x].type = EnumType.Lumber;
+                    }
+
+                    else if (tile.type == EnumType.Lumber)
+                    {
+                        if (tile.adj.l >= 1 && tile.adj.t >= 1) map[y, x].type = EnumType.Lumber;
+                        else map[y, x].type = EnumType.Open;
+                    }
+
+                    if (map[y, x].type == EnumType.Open) count.o++;
+                    if (map[y, x].type == EnumType.Trees) count.t++;
+                    if (map[y, x].type == EnumType.Lumber) count.l++;
+                }
+            }
+
+            return count;
+        }
+
+        private static (int o, int t, int l) FillAdjacency(int size, (EnumType type, (int o, int t, int l) adj)[,] map)
+        {
+            (int o, int t, int l) count = (0, 0, 0);
+            for (var y = 0; y < size; ++y)
+                for (var x = 0; x < size; ++x)
+                {
+                    int count1 = OfType(EnumType.Open, size, map, y, x);
+                    int count2 = OfType(EnumType.Trees, size, map, y, x);
+                    int count3 = OfType(EnumType.Lumber, size, map, y, x);
+
+                    var result = (count1, count2, count3);
+
+                    map[y, x].adj = result;
+
+                    if (map[y, x].type == EnumType.Open) count.o++;
+                    if (map[y, x].type == EnumType.Trees) count.t++;
+                    if (map[y, x].type == EnumType.Lumber) count.l++;
+                }
+            return count;
+        }
+
+        private static int OfType(EnumType type, int size, (EnumType type, (int o, int t, int l) adj)[,] map, int y, int x)
+        {
+            int count = 0;
+            if (y > 0 && x > 0) count += map[y - 1, x - 1].type == type ? 1 : 0;
+            if (y > 0) count += map[y - 1, x].type == type ? 1 : 0;
+            if (y > 0 && x < size - 1) count += map[y - 1, x + 1].type == type ? 1 : 0;
+
+            if (x > 0) count += map[y, x - 1].type == type ? 1 : 0;
+            if (x < size - 1) count += map[y, x + 1].type == type ? 1 : 0;
+
+            if (y < size - 1 && x > 0) count += map[y + 1, x - 1].type == type ? 1 : 0;
+            if (y < size - 1) count += map[y + 1, x].type == type ? 1 : 0;
+            if (y < size - 1 && x < size - 1) count += map[y + 1, x + 1].type == type ? 1 : 0;
+            return count;
+        }
+
+        private static void CreateMap(StreamReader file, (EnumType type, (int o, int t, int l) adj)[,] map)
+        {
+            int y = 0;
+            do
+            {
+                var line = file.ReadLine();
+                if (line == null) break;
+
+                int x = 0;
+                foreach (var c in line)
+                {
+                    EnumType type;
+                    switch (c)
+                    {
+                        case '|':
+                            type = EnumType.Trees;
+                            break;
+                        case '#':
+                            type = EnumType.Lumber;
+                            break;
+                        case '.':
+                        default:
+                            type = EnumType.Open;
+                            break;
+
+                    }
+
+                    map[y, x] = (type, (0, 0, 0));
+                    x++;
+                }
+
+
+                ++y;
+            } while (true);
+        }
+    }
+}

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

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