Browse Source

Day 15 OK

bastien.monsarrat 6 years ago
parent
commit
d98e53b147
5 changed files with 99 additions and 5 deletions
  1. 11 5
      Adv2016.sln
  2. 9 0
      D15.1/D15.csproj
  3. 65 0
      D15.1/Program.cs
  4. 8 0
      D15.1/Properties/launchSettings.json
  5. 6 0
      D15.1/input.txt

+ 11 - 5
Adv2016.sln

@@ -43,15 +43,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D10.2", "D10.2\D10.2.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D11", "D11.1\D11.csproj", "{560638CF-1FEA-4962-B944-B079E89B1CF0}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.1", "D12.1\D12.1.csproj", "{CE31711D-F000-4A27-811A-4C5E5F92FE7F}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D12.1", "D12.1\D12.1.csproj", "{CE31711D-F000-4A27-811A-4C5E5F92FE7F}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.2", "D12.2\D12.2.csproj", "{873596CF-84EA-4318-A43F-909284891A82}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D12.2", "D12.2\D12.2.csproj", "{873596CF-84EA-4318-A43F-909284891A82}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.1", "D13.1\D13.1.csproj", "{D7808973-C957-41B8-81B4-875F207C16FA}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D13.1", "D13.1\D13.1.csproj", "{D7808973-C957-41B8-81B4-875F207C16FA}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.2", "D13.2\D13.2.csproj", "{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D13.2", "D13.2\D13.2.csproj", "{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D14", "D14.1\D14.csproj", "{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D14", "D14.1\D14.csproj", "{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D15", "D15.1\D15.csproj", "{209B250E-7768-4444-9C94-D77208C3CFC4}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -159,6 +161,10 @@ Global
 		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{A85C3AA5-EBED-4081-8F07-BD3DF9E08668}.Release|Any CPU.Build.0 = Release|Any CPU
+		{209B250E-7768-4444-9C94-D77208C3CFC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{209B250E-7768-4444-9C94-D77208C3CFC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{209B250E-7768-4444-9C94-D77208C3CFC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{209B250E-7768-4444-9C94-D77208C3CFC4}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D15.1/D15.csproj

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

+ 65 - 0
D15.1/Program.cs

@@ -0,0 +1,65 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using System.Text.RegularExpressions;
+
+namespace D15._1
+{
+    class Program
+    {
+        public class R : StreamReader, IEnumerable
+        {
+            public R(Stream stream) : base(stream) { }
+            public IEnumerator GetEnumerator()
+            {
+                while (true)
+                {
+                    var line = ReadLine();
+                    if (line == null) break;
+                    yield return line;
+                }
+            }
+        }
+
+        static void Main(string[] args)
+        {
+            List<(int positions, int position)> discs = new List<(int, int)>();
+
+            var regex = new Regex(@"Disc #\d has (?<n>\d+) positions; at time=0, it is at position (?<t>\d+).");
+            using (var file = new R(File.OpenText(args[0]).BaseStream))
+            {
+                foreach (string line in file)
+                {
+                    var result = regex.Match(line);
+                    discs.Add((int.Parse(result.Groups["n"].Value), int.Parse(result.Groups["t"].Value)));
+                }
+            }
+
+            Console.WriteLine($"Part 1 answer is : {RunMachine(discs)}");
+            discs.Add((11, 0));
+            Console.WriteLine($"Part 2 answer is : {RunMachine(discs)}");
+        }
+
+        private static int RunMachine(List<(int positions, int position)> discs)
+        {
+            for (int startTime = 0; ; ++startTime)
+            {
+                int ball = 0;
+                foreach (var (positions, position) in discs)
+                {
+                    ball++;
+                    var t = startTime + ball;
+                    if ((position + t) % positions != 0)
+                    {
+                        ball = 0;
+                        break;
+                    }
+                }
+
+                if (ball == discs.Count)
+                    return startTime;
+            }
+        }
+    }
+}

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

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

+ 6 - 0
D15.1/input.txt

@@ -0,0 +1,6 @@
+Disc #1 has 13 positions; at time=0, it is at position 10.
+Disc #2 has 17 positions; at time=0, it is at position 15.
+Disc #3 has 19 positions; at time=0, it is at position 17.
+Disc #4 has 7 positions; at time=0, it is at position 1.
+Disc #5 has 5 positions; at time=0, it is at position 0.
+Disc #6 has 3 positions; at time=0, it is at position 1.