bastien.monsarrat 6 жил өмнө
parent
commit
8b821d452d

+ 12 - 0
Adv2015.sln

@@ -51,6 +51,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D13.1", "D13.1\D13.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.2", "D13.2\D13.2.csproj", "{46445805-4831-436D-A531-9D973306DD2E}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D14.1", "D14.1\D14.1.csproj", "{276D6A7D-A62C-48C9-B841-1AFFB11E93B0}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D14.2", "D14.2\D14.2.csproj", "{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -153,6 +157,14 @@ Global
 		{46445805-4831-436D-A531-9D973306DD2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{46445805-4831-436D-A531-9D973306DD2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{46445805-4831-436D-A531-9D973306DD2E}.Release|Any CPU.Build.0 = Release|Any CPU
+		{276D6A7D-A62C-48C9-B841-1AFFB11E93B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{276D6A7D-A62C-48C9-B841-1AFFB11E93B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{276D6A7D-A62C-48C9-B841-1AFFB11E93B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{276D6A7D-A62C-48C9-B841-1AFFB11E93B0}.Release|Any CPU.Build.0 = Release|Any CPU
+		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 106 - 0
D14.1/Program.cs

@@ -0,0 +1,106 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text.RegularExpressions;
+
+namespace D14._1
+{
+    public enum EnumState
+    {
+        Running, Resting
+    };
+
+    public class Reindeer
+    {
+        public int Speed;
+        public int SpeedTime;
+        public int RestTime;
+        public string Name;
+
+        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)
+            {
+                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;
+                        }
+                    }
+                }
+            }
+
+            int maxDistance = 0;
+            foreach (var r in reindeers)
+                if (r.DistanceTraveled > maxDistance) maxDistance = r.DistanceTraveled;
+
+            Console.WriteLine($"The answer is : {maxDistance}");
+        }
+
+        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);
+                }
+            }
+        }
+    }
+}

+ 8 - 0
D14.1/Properties/launchSettings.json

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

+ 9 - 0
D14.1/input.txt

@@ -0,0 +1,9 @@
+Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
+Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
+Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
+Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
+Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
+Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
+Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
+Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
+Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.

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

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

+ 113 - 0
D14.2/Program.cs

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

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

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