ソースを参照

Day 2 sum of powers

Lukas Angerer 2 年 前
コミット
d495aa49c6
3 ファイル変更34 行追加5 行削除
  1. 0 1
      Day2/GameSamples.cs
  2. 29 4
      Day2/Program.cs
  3. 5 0
      Day2/SampleSet.cs

+ 0 - 1
Day2/GameSamples.cs

@@ -9,6 +9,5 @@ public record GameSamples(int Id, IList<SampleSet> Samples)
         {
             sample.Print();
         }
-        Console.WriteLine();
     }
 }

+ 29 - 4
Day2/Program.cs

@@ -20,7 +20,7 @@ var sum = 0;
 
 foreach (var game in parser.Parse(inputFile))
 {
-    game.Print();
+    //game.Print();
     var pass = true;
     foreach (var set in game.Samples)
     {
@@ -33,16 +33,41 @@ foreach (var game in parser.Parse(inputFile))
 
     if (pass)
     {
-        Console.WriteLine("PASS");
+        //Console.WriteLine("PASS");
         sum += game.Id;
     }
     else
     {
-        Console.WriteLine("FAIL");
+        //Console.WriteLine("FAIL");
     }
+
+    Console.WriteLine();
+}
+
+Console.WriteLine();
+Console.WriteLine($"Part 1 sum: {sum}");
+
+Console.WriteLine();
+
+sum = 0;
+foreach (var game in parser.Parse(inputFile))
+{
+    game.Print();
+
+    var max = game.Samples.Aggregate(new SampleSet(0, 0, 0), (prev, current) => new SampleSet(
+        Math.Max(prev.Red, current.Red),
+        Math.Max(prev.Green, current.Green),
+        Math.Max(prev.Blue, current.Blue)
+    ));
+    
+    Console.WriteLine($"Max: {max.Power()}");
+    max.Print();
+    sum += max.Power();
+    
+    Console.WriteLine();
 }
 
 Console.WriteLine();
-Console.WriteLine(sum);
+Console.WriteLine($"Part 2 sum of powers: {sum}");
 
 return 0;

+ 5 - 0
Day2/SampleSet.cs

@@ -6,4 +6,9 @@ public record SampleSet(int Red, int Green, int Blue)
     {
         Console.WriteLine($"R {Red}, G {Green}, B {Blue}");
     }
+
+    public int Power()
+    {
+        return Red * Green * Blue;
+    }
 }