|
|
@@ -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; }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|