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

+ 6 - 0
Adv2018.sln

@@ -73,6 +73,8 @@ 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}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -219,6 +221,10 @@ Global
 		{584D1E60-73D7-44F9-A944-65525DF9A7A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{584D1E60-73D7-44F9-A944-65525DF9A7A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{584D1E60-73D7-44F9-A944-65525DF9A7A1}.Release|Any CPU.Build.0 = Release|Any CPU
+		{92B12539-90F5-4B96-B9BD-C8BC25A961B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{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
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 128 - 0
D18.1/Program.cs

@@ -0,0 +1,128 @@
+using System;
+using System.IO;
+
+namespace D18._1
+{
+    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);
+
+            int minutes = 1;
+            do
+            {
+                FillAdjacency(size, map);
+                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++;
+                    }
+                }
+
+            } while (minutes++ < 10);
+
+            int result = count.l * count.t;
+            Console.WriteLine($"The result is : {result}");
+        }
+
+        private static void FillAdjacency(int size, (EnumType type, (int o, int t, int l) adj)[,] map)
+        {
+            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;
+                }
+        }
+
+        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.1/Properties/launchSettings.json

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

+ 50 - 0
D18.1/input.txt

@@ -0,0 +1,50 @@
+|...||..|....|..#..|.|..#.|......#|.||.||.|..|....
+.#.....#....#|||#|#..||..|.#..|....#...||.#.#|.|.|
+.|...|#..|||..|.#....#..|.|....|...||...#.#|...|#.
+.........|.|.#||.##.#|.||.|#..||..||...##.##......
+..|.||.#.|.|.|....||#.....|.|.||##.#|##.|....|.#..
+#..#|#..|#...|...#...#....#|##......#..|||#..#.#..
+...#.#.#|...||...|.#...||#.....|##|||.....#.#.#.|.
+.#..|.###|#.|.##......||..#...#.#..|#.|.##.#.#.|..
+..||.|.|.....###...#|...###..##|.#.#....#|..##.|.|
+.##..#.|.#....##...||.|#..#.####..|#..##.|...#....
+..#...#|..|..|.|.#|#.|.|..|..||......#|.|...##..|.
+..||...##|#.....#|..|..|.|.##.|.##.|#|.##|.||...#.
+..|.||......#...#......|......|.##...|..|.|.#...#.
+||#.#|...#.......#......|..|#.#.|.....#...#.|##..|
+.....||.|..|##.#|..#..#|..#|.##....##|.##.||...###
+#|..#..#....|...|.|....#.##...#..|.|..#........|#|
+....#.|.....##.|..##.....##..#.....|#.|.#.|.#.###.
+...###........|.....|..|#..#..|##..###.....#||..||
+#.#...#|..##...|..|||#.....|#|.......#..#.......#|
+#.....||||.#..##.|..|#|.#..|.......|##.|#|#....#.|
+#..##|...|....#.#.#.||.|..|.....#|...|....#|..##|#
+#..|||#..#..|..#|....#.........#...#...#...|.|...#
+.........|.|...#...|...#|##..#.....#.|.#..||.#...#
+.|#...#|...#.###.|#.||.|..#..#.####..#..||.#.#|..|
+.|#....||#|#|.|.|#....##..||.##|....#....#||...|.|
+...|....|...|.||#...#...........#..#....##|.#....|
+....#|#..#....|##...#..|#..#.|......|#........|#..
+.|..#...|..#.|.#....#....#.|...#..|........|..|...
+...##|....||...##...|......|#..|..#..|.........##.
+..##.|.#..........##.|....|##|.....|.#.#.|.....|.#
+#|.|.......#|..#..#.#.|..|.|.#.||#.#...|.#..|.....
+||.#............#...|...#..|#|..||##...#.|#|##|.#|
+..|..||####.|....|##.#.|...|||#..|.|.|.........##.
+...|....|..||.#..##||.|||#..|..#...#.##...||.|..||
+....|...|##...#|.|.|..#|..|#|..##.|....||..|.|.||.
+#..#.......|.#..#..||...#|..#.....#|#..|..|...##..
+#...#..##..|.#...#.....|......###|..|...||.....|..
+#.|..#..|#..|#|..#..||#......|#.......|.#....###..
+#...#||...##|.#.#|####|...|......|.#.#..|#.##...|.
+|.#..#....#..#.....|#..|#.|..##.#..|#|##........||
+#.........##..|....|#.........||..||##...||.|..|.|
+.#....#.......#..|#.|...|....#.#.#........##.##.|.
+.#.#..|.|#..|..#..###...|.#......|.|#.|.##..|.|##.
+..||..|.##.##|...|....#||##....#..#.|#|.|#||#..|..
+.......#|#||.....|.||.|##..|.|....|....|.#...#..|.
+#||##|.||..|##.|..|#.#.|.|....#|.#|.....|||..#.||.
+.||#.|#...||#.#..#..|.|#.....#.#.|#.|...#..#...#..
+#........|..#...|#..###.|.#|...#..#...........#.|#
+.|...#|....||.|...#.|..|.||##.|..#|.#|..|.#....#..
+||....###...#...|||.||..#.|.....||.#|..###..|....#