bastien.monsarrat 6 years ago
parent
commit
272f30a7a5
4 changed files with 109 additions and 0 deletions
  1. 6 0
      Adv2018.sln
  2. 9 0
      D9.2/D9.2.csproj
  3. 86 0
      D9.2/Program.cs
  4. 8 0
      D9.2/Properties/launchSettings.json

+ 6 - 0
Adv2018.sln

@@ -39,6 +39,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D08.2", "D8.2\D08.2.csproj"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D09.1", "D9.1\D09.1.csproj", "{9101C358-8060-4626-B7AB-359A0BA3D494}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D9.2", "D9.2\D9.2.csproj", "{892458FC-C47F-4E0A-9603-4C3473321F25}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -117,6 +119,10 @@ Global
 		{9101C358-8060-4626-B7AB-359A0BA3D494}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9101C358-8060-4626-B7AB-359A0BA3D494}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9101C358-8060-4626-B7AB-359A0BA3D494}.Release|Any CPU.Build.0 = Release|Any CPU
+		{892458FC-C47F-4E0A-9603-4C3473321F25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{892458FC-C47F-4E0A-9603-4C3473321F25}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{892458FC-C47F-4E0A-9603-4C3473321F25}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{892458FC-C47F-4E0A-9603-4C3473321F25}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 86 - 0
D9.2/Program.cs

@@ -0,0 +1,86 @@
+using System;
+using System.IO;
+using System.Linq;
+using System.Text.RegularExpressions;
+
+namespace D9._2
+{
+    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 text = file.ReadToEnd();
+            var matches = new Regex(@"(?<players>\d+) players; last marble is worth (?<points>\d+) points")
+                .Match(text);
+
+            UInt64 lastMarble = UInt64.Parse(matches.Groups["points"].Value) * 100;
+            int players = int.Parse(matches.Groups["players"].Value);
+
+            var scores = new UInt64[players];
+
+            var currentMarble = new Marble();
+            currentMarble.CounterClockwise = currentMarble;
+            currentMarble.Clockwise = currentMarble;
+
+            int currentPlayer = 1;
+            for (UInt64 marble = 1; marble <= lastMarble; ++marble)
+            {
+                // ------- Winning case --------
+
+                if (marble % 23 == 0)
+                {
+                    scores[currentPlayer - 1] += marble;
+                    var sevenCounterClockwiseMarble = currentMarble
+                        .CounterClockwise
+                        .CounterClockwise
+                        .CounterClockwise
+                        .CounterClockwise
+                        .CounterClockwise
+                        .CounterClockwise
+                        .CounterClockwise;
+                    scores[currentPlayer - 1] += sevenCounterClockwiseMarble.Value;
+
+                    sevenCounterClockwiseMarble.CounterClockwise.Clockwise = sevenCounterClockwiseMarble.Clockwise;
+                    currentMarble = sevenCounterClockwiseMarble.Clockwise;
+                }
+
+                // ------- Cas nominal ---------
+
+                else
+                {
+                    var oneClockwise = currentMarble.Clockwise;
+                    var twoClockwise = oneClockwise.Clockwise;
+
+                    var newMarble = new Marble()
+                    {
+                        CounterClockwise = oneClockwise,
+                        Clockwise = twoClockwise,
+                        Value = marble
+                    };
+
+                    oneClockwise.Clockwise = newMarble;
+                    twoClockwise.CounterClockwise = newMarble;
+
+                    currentMarble = newMarble;
+                }
+
+                currentPlayer += 1;
+                if (currentPlayer > players) currentPlayer = 1;
+            }
+
+            UInt64 max = scores.Max();
+            Console.WriteLine($"Result is : {max}");
+        }
+
+        class Marble
+        {
+            public Marble CounterClockwise { get; set; }
+            public Marble Clockwise { get; set; }
+            public UInt64 Value { get; set; }
+        }
+    }
+}

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

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