|
|
@@ -0,0 +1,113 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
+
|
|
|
+namespace D14._2
|
|
|
+{
|
|
|
+ public enum EnumState
|
|
|
+ {
|
|
|
+ Running, Resting
|
|
|
+ };
|
|
|
+
|
|
|
+ public class Reindeer
|
|
|
+ {
|
|
|
+ public int Speed;
|
|
|
+ public int SpeedTime;
|
|
|
+ public int RestTime;
|
|
|
+ public string Name;
|
|
|
+
|
|
|
+ public int Points = 0;
|
|
|
+ public int DistanceTraveled = 0;
|
|
|
+ public EnumState State = EnumState.Running;
|
|
|
+ public int Remaining = 0;
|
|
|
+
|
|
|
+ public override bool Equals(object obj)
|
|
|
+ {
|
|
|
+ var reindeer = obj as Reindeer;
|
|
|
+ return reindeer != null &&
|
|
|
+ Name == reindeer.Name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override int GetHashCode()
|
|
|
+ {
|
|
|
+ return HashCode.Combine(Name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class Program
|
|
|
+ {
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ if (args.Length < 1) throw new ArgumentException();
|
|
|
+ if (File.Exists(args[0]) == false) throw new FileNotFoundException();
|
|
|
+
|
|
|
+ var reindeers = new HashSet<Reindeer>();
|
|
|
+
|
|
|
+ ParseFile(args[0], reindeers);
|
|
|
+
|
|
|
+ for (int second = 0; second <= 2503; ++second)
|
|
|
+ {
|
|
|
+ int maxDistance = 0;
|
|
|
+ foreach (var r in reindeers)
|
|
|
+ {
|
|
|
+ if (r.State == EnumState.Running)
|
|
|
+ {
|
|
|
+ r.DistanceTraveled += r.Speed;
|
|
|
+ r.Remaining--;
|
|
|
+
|
|
|
+ if (r.Remaining == 0)
|
|
|
+ {
|
|
|
+ r.Remaining = r.RestTime;
|
|
|
+ r.State = EnumState.Resting;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ r.Remaining--;
|
|
|
+ if (r.Remaining == 0)
|
|
|
+ {
|
|
|
+ r.Remaining = r.SpeedTime;
|
|
|
+ r.State = EnumState.Running;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (r.DistanceTraveled > maxDistance) maxDistance = r.DistanceTraveled;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (var r in reindeers)
|
|
|
+ if (r.DistanceTraveled == maxDistance) r.Points++;
|
|
|
+ }
|
|
|
+
|
|
|
+ int maxPoints = 0;
|
|
|
+ foreach (var r in reindeers)
|
|
|
+ if (r.Points > maxPoints) maxPoints = r.Points;
|
|
|
+
|
|
|
+ Console.WriteLine($"The answer is : {maxPoints}");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void ParseFile(string f, HashSet<Reindeer> reindeers)
|
|
|
+ {
|
|
|
+ var regex = new Regex(@"(?<n>\w+) can fly (?<s>\d+) km/s for (?<st>\d+) seconds, but then must rest for (?<rt>\d+) seconds.");
|
|
|
+ using (var file = File.OpenText(f))
|
|
|
+ {
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ var line = file.ReadLine();
|
|
|
+ if (line == null) break;
|
|
|
+
|
|
|
+ var result = regex.Match(line);
|
|
|
+ var r = new Reindeer();
|
|
|
+ r.Name = result.Groups["n"].Value;
|
|
|
+ r.Speed = int.Parse(result.Groups["s"].Value);
|
|
|
+ r.SpeedTime = int.Parse(result.Groups["st"].Value);
|
|
|
+ r.RestTime = int.Parse(result.Groups["rt"].Value);
|
|
|
+
|
|
|
+ r.Remaining = r.SpeedTime;
|
|
|
+
|
|
|
+ reindeers.Add(r);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|