bastien.monsarrat há 7 anos atrás
pai
commit
d08d51d4d4
6 ficheiros alterados com 278 adições e 5 exclusões
  1. 9 3
      Adv2018.sln
  2. 4 2
      D6.2/Program.cs
  3. 10 0
      D7.2/D07.2.csproj
  4. 146 0
      D7.2/Program.cs
  5. 8 0
      D7.2/Properties/launchSettings.json
  6. 101 0
      D7.2/input.txt

+ 9 - 3
Adv2018.sln

@@ -1,7 +1,7 @@
 
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio Version 16
-VisualStudioVersion = 16.0.28315.86
+# Visual Studio 15
+VisualStudioVersion = 15.0.28307.271
 MinimumVisualStudioVersion = 10.0.40219.1
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D01.1", "D1.1\D01.1.csproj", "{75F7258F-26A1-40DD-BF7C-68BE0A79AD46}"
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D01.1", "D1.1\D01.1.csproj", "{75F7258F-26A1-40DD-BF7C-68BE0A79AD46}"
 EndProject
 EndProject
@@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D06.2", "D6.2\D06.2.csproj"
 EndProject
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D06.Bonus", "D6.Bonus\D06.Bonus.csproj", "{A14766AE-E4A7-442E-809D-EE1938B3B1BF}"
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D06.Bonus", "D6.Bonus\D06.Bonus.csproj", "{A14766AE-E4A7-442E-809D-EE1938B3B1BF}"
 EndProject
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D07.1", "D7.1\D07.1.csproj", "{223AFFCA-23D6-4370-A95C-AFA55741CE63}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D07.1", "D7.1\D07.1.csproj", "{223AFFCA-23D6-4370-A95C-AFA55741CE63}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D07.2", "D7.2\D07.2.csproj", "{1FFA5ACA-AB3A-4858-A92D-D741853F7A89}"
 EndProject
 EndProject
 Global
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -93,6 +95,10 @@ Global
 		{223AFFCA-23D6-4370-A95C-AFA55741CE63}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{223AFFCA-23D6-4370-A95C-AFA55741CE63}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{223AFFCA-23D6-4370-A95C-AFA55741CE63}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{223AFFCA-23D6-4370-A95C-AFA55741CE63}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{223AFFCA-23D6-4370-A95C-AFA55741CE63}.Release|Any CPU.Build.0 = Release|Any CPU
 		{223AFFCA-23D6-4370-A95C-AFA55741CE63}.Release|Any CPU.Build.0 = Release|Any CPU
+		{1FFA5ACA-AB3A-4858-A92D-D741853F7A89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{1FFA5ACA-AB3A-4858-A92D-D741853F7A89}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{1FFA5ACA-AB3A-4858-A92D-D741853F7A89}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{1FFA5ACA-AB3A-4858-A92D-D741853F7A89}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 		HideSolutionNode = FALSE

+ 4 - 2
D6.2/Program.cs

@@ -33,7 +33,7 @@ namespace D6._2
             {
             {
                 for (uint y = 0; y < size; y++)
                 for (uint y = 0; y < size; y++)
                 {
                 {
-                    var total = GetTotalDistance(coordinates, (x, y));
+                    var total = GetTotalDistance(ceil, coordinates, (x, y));
                     if (total < ceil) area++;
                     if (total < ceil) area++;
                 }
                 }
             }
             }
@@ -41,7 +41,7 @@ namespace D6._2
             Console.WriteLine($"Answer : {area}");
             Console.WriteLine($"Answer : {area}");
         }
         }
 
 
-        private static int GetTotalDistance(List<(uint x, uint y)> coordinates, (uint x, uint y) point)
+        private static int GetTotalDistance(int ceil, List<(uint x, uint y)> coordinates, (uint x, uint y) point)
         {
         {
             int total = 0;
             int total = 0;
             for (int i = 0; i < coordinates.Count; i++)
             for (int i = 0; i < coordinates.Count; i++)
@@ -49,6 +49,8 @@ namespace D6._2
                 var (x, y) = coordinates[i];
                 var (x, y) = coordinates[i];
                 var manhattan = Math.Abs((int)point.x - (int)x) + Math.Abs((int)point.y - (int)y);
                 var manhattan = Math.Abs((int)point.x - (int)x) + Math.Abs((int)point.y - (int)y);
                 total += manhattan;
                 total += manhattan;
+
+                if (total >= ceil) break;
             }
             }
             return total;
             return total;
         }
         }

+ 10 - 0
D7.2/D07.2.csproj

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

+ 146 - 0
D7.2/Program.cs

@@ -0,0 +1,146 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace D7._2
+{
+    class Steps : SortedDictionary<char, HashSet<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 steps = new Steps();
+            var builders = new (char task, int time)?[5];
+            int totalWorkTime = 0;
+
+            do
+            {
+                var line = file.ReadLine();
+                if (line == null) break;
+
+                AddDependency(steps, line);
+
+            } while (true);
+
+            do
+            {
+                // Assign tasks to available workers
+                (char task, int time)? quickest = AssignWorkers(steps, builders);
+
+                // Work assigned tasks
+
+                if (quickest.HasValue == false) DumpData(steps, builders);
+                else WorkIt(steps, builders, quickest.Value);
+
+                totalWorkTime += quickest.Value.time;
+
+                Console.WriteLine($"Total elapsed time : '{totalWorkTime}'");
+
+            } while (steps.Count > 0);
+
+            Console.WriteLine($"===== ANSWER ==========");
+            Console.WriteLine($"Total elapsed time : '{totalWorkTime}'");
+        }
+
+        private static void WorkIt(Steps steps, (char task, int time)?[] builders, (char task, int time) quickest)
+        {
+
+            for (int i = 0; i < builders.Length; ++i)
+            {
+                if (builders[i].HasValue == false) continue;
+
+                var builder = builders[i].Value;
+                builder.time -= quickest.time;
+
+                if (builder.time <= 0)
+                {
+                    builders[i] = null;
+                    removeDependency(steps, quickest.task);
+
+                    Console.WriteLine($"Worker #{i} finnished task '{builder.task}'");
+                }
+                else builders[i] = builder;
+            }
+        }
+
+        private static (char task, int time)? AssignWorkers(Steps steps, (char task, int time)?[] builders)
+        {
+            (char task, int time)? quickest = null;
+
+            for (int i = 0; i < builders.Length; ++i)
+            {
+                if (builders[i].HasValue == false)
+                {
+
+                    (char task, int time)? taskN = getFirstTask(steps);
+                    if (taskN.HasValue == false) continue;
+
+                    var t = taskN.Value;
+
+                    steps.Remove(t.task);
+                    builders[i] = t;
+
+                    Console.WriteLine($"Assigned task '{t.task}' to worker #{i} (load : {t.time}s)");
+                }
+
+                if (quickest.HasValue == false || builders[i].Value.time < quickest.Value.time)
+                    quickest = builders[i];
+            }
+
+            return quickest;
+        }
+
+        static void removeDependency(Steps steps, char dep)
+        {
+            foreach (var step in steps)
+                step.Value.Remove(dep);
+        }
+
+        static (char task, int time)? getFirstTask(Steps steps)
+        {
+            foreach (var step in steps)
+            {
+                if (step.Value.Count == 0)
+                    return (step.Key, (int)(step.Key - 'A' + 1) + 60);
+            }
+            return null;
+        }
+
+        static void AddDependency(IDictionary<char, HashSet<char>> steps, string line)
+        {
+            var left = line.Substring(5, 1)[0];
+            var right = line.Substring(36, 1)[0];
+
+            if (steps.ContainsKey(left) == false) steps.Add(left, new HashSet<char>());
+            if (steps.ContainsKey(right) == false) steps.Add(right, new HashSet<char>());
+
+            steps[right].Add(left);
+        }
+
+        // ---------------------------------------------------------------------------------------
+
+        private static void DumpData(Steps steps, (char task, int time)?[] builders)
+        {
+            Console.WriteLine($"===== DUMP WORKERS ==========");
+            for (int i = 0; i < builders.Length; ++i)
+            {
+                var builder = builders[i];
+                if (builder.HasValue)
+                    Console.WriteLine($"Worker #{i} state : Task '{builder.Value.task}', {builder.Value.time}s left");
+                else
+                    Console.WriteLine($"Worker #{i} state : VACANT");
+            }
+            Console.WriteLine($"===== DUMP STEPS ==========");
+            foreach (var step in steps)
+            {
+                Console.WriteLine($"Step '{step.Key}' state : {step.Value.Count} dependencies left");
+            }
+        }
+    }
+}

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

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

+ 101 - 0
D7.2/input.txt

@@ -0,0 +1,101 @@
+Step I must be finished before step Q can begin.
+Step B must be finished before step O can begin.
+Step J must be finished before step M can begin.
+Step W must be finished before step Y can begin.
+Step U must be finished before step X can begin.
+Step T must be finished before step Q can begin.
+Step G must be finished before step M can begin.
+Step K must be finished before step C can begin.
+Step F must be finished before step Z can begin.
+Step D must be finished before step A can begin.
+Step N must be finished before step Y can begin.
+Step Y must be finished before step Q can begin.
+Step Q must be finished before step Z can begin.
+Step V must be finished before step E can begin.
+Step A must be finished before step X can begin.
+Step E must be finished before step C can begin.
+Step O must be finished before step R can begin.
+Step P must be finished before step L can begin.
+Step H must be finished before step R can begin.
+Step M must be finished before step R can begin.
+Step C must be finished before step Z can begin.
+Step R must be finished before step L can begin.
+Step L must be finished before step S can begin.
+Step S must be finished before step X can begin.
+Step Z must be finished before step X can begin.
+Step T must be finished before step O can begin.
+Step D must be finished before step Z can begin.
+Step P must be finished before step R can begin.
+Step M must be finished before step Z can begin.
+Step L must be finished before step Z can begin.
+Step W must be finished before step N can begin.
+Step Q must be finished before step R can begin.
+Step P must be finished before step C can begin.
+Step U must be finished before step O can begin.
+Step F must be finished before step O can begin.
+Step K must be finished before step X can begin.
+Step G must be finished before step K can begin.
+Step M must be finished before step C can begin.
+Step Y must be finished before step Z can begin.
+Step A must be finished before step O can begin.
+Step D must be finished before step P can begin.
+Step K must be finished before step S can begin.
+Step I must be finished before step E can begin.
+Step G must be finished before step F can begin.
+Step S must be finished before step Z can begin.
+Step N must be finished before step V can begin.
+Step F must be finished before step D can begin.
+Step A must be finished before step Z can begin.
+Step F must be finished before step X can begin.
+Step T must be finished before step Y can begin.
+Step W must be finished before step H can begin.
+Step D must be finished before step H can begin.
+Step W must be finished before step G can begin.
+Step J must be finished before step X can begin.
+Step T must be finished before step X can begin.
+Step U must be finished before step R can begin.
+Step O must be finished before step P can begin.
+Step L must be finished before step X can begin.
+Step I must be finished before step B can begin.
+Step M must be finished before step L can begin.
+Step C must be finished before step R can begin.
+Step R must be finished before step X can begin.
+Step F must be finished before step N can begin.
+Step V must be finished before step H can begin.
+Step K must be finished before step A can begin.
+Step W must be finished before step O can begin.
+Step U must be finished before step Q can begin.
+Step O must be finished before step C can begin.
+Step K must be finished before step V can begin.
+Step R must be finished before step S can begin.
+Step E must be finished before step S can begin.
+Step J must be finished before step A can begin.
+Step E must be finished before step X can begin.
+Step K must be finished before step Y can begin.
+Step Y must be finished before step X can begin.
+Step P must be finished before step Z can begin.
+Step W must be finished before step X can begin.
+Step Y must be finished before step A can begin.
+Step V must be finished before step X can begin.
+Step O must be finished before step M can begin.
+Step I must be finished before step J can begin.
+Step W must be finished before step L can begin.
+Step I must be finished before step G can begin.
+Step D must be finished before step O can begin.
+Step D must be finished before step N can begin.
+Step M must be finished before step X can begin.
+Step I must be finished before step R can begin.
+Step Y must be finished before step M can begin.
+Step F must be finished before step M can begin.
+Step U must be finished before step M can begin.
+Step Y must be finished before step H can begin.
+Step K must be finished before step D can begin.
+Step N must be finished before step O can begin.
+Step H must be finished before step S can begin.
+Step G must be finished before step L can begin.
+Step T must be finished before step D can begin.
+Step J must be finished before step N can begin.
+Step K must be finished before step M can begin.
+Step K must be finished before step P can begin.
+Step E must be finished before step R can begin.
+Step N must be finished before step H can begin.