Bladeren bron

Day 6 part 1

Lukas Angerer 2 jaren geleden
bovenliggende
commit
be3c47b1b4
5 gewijzigde bestanden met toevoegingen van 58 en 0 verwijderingen
  1. 17 0
      Day6/Program.cs
  2. 11 0
      Day6/RaceStats.cs
  3. 26 0
      Day6/RaceTimeParser.cs
  4. 2 0
      Day6/inputs/input-lar.txt
  5. 2 0
      Day6/inputs/sample1.txt

+ 17 - 0
Day6/Program.cs

@@ -7,5 +7,22 @@ if (args.Length < 1)
 }
 
 var inputFile = args[0];
+var parser = new RaceTimeParser();
+
+var races = parser.Parse(inputFile);
+var total = 1;
+
+foreach (var race in races)
+{
+    var bound = race.LowerBound();
+    Console.WriteLine($"{race.Duration} / {race.Distance} => {bound}");
+    var options = race.Duration - 2 * bound + 1;
+    Console.WriteLine($"Options: {options}");
+
+    total *= options;
+}
+
+Console.WriteLine();
+Console.WriteLine($"Total: {total}");
 
 return 0;

+ 11 - 0
Day6/RaceStats.cs

@@ -0,0 +1,11 @@
+namespace Day6;
+
+public record RaceStats(int Duration, int Distance)
+{
+    public int LowerBound()
+    {
+        var lowerBound = (-Duration + Math.Sqrt(Duration * Duration - 4 * Distance)) / (-2.0);
+        var nextWhole = (int)Math.Ceiling(lowerBound);
+        return nextWhole > lowerBound ? nextWhole : nextWhole + 1;
+    }
+}

+ 26 - 0
Day6/RaceTimeParser.cs

@@ -0,0 +1,26 @@
+using System.Text.RegularExpressions;
+
+namespace Day6;
+
+public partial class RaceTimeParser
+{
+    [GeneratedRegex(@"\d+")]
+    private partial Regex NumberList();
+    
+    public IList<RaceStats> Parse(string inputFile)
+    {
+        using var reader = File.OpenText(inputFile);
+
+        var timesLine = reader.ReadLine()!;
+        var times = NumberList().Matches(timesLine.Substring("Time:".Length)).Cast<Match>().Select(x => int.Parse(x.Value)).ToList();
+        var durationsLine = reader.ReadLine()!;
+        var durations = NumberList().Matches(durationsLine.Substring("Distance:".Length)).Cast<Match>().Select(x => int.Parse(x.Value)).ToList();
+
+        if (times.Count != durations.Count)
+        {
+            throw new Exception("Expecting the same number of times and distances");
+        }
+
+        return times.Zip(durations).Select(item => new RaceStats(item.First, item.Second)).ToList();
+    }
+}

+ 2 - 0
Day6/inputs/input-lar.txt

@@ -0,0 +1,2 @@
+Time:        38     94     79     70
+Distance:   241   1549   1074   1091

+ 2 - 0
Day6/inputs/sample1.txt

@@ -0,0 +1,2 @@
+Time:      7  15   30
+Distance:  9  40  200