diff --git a/src/AdventOfCode/AdventOfCode/Day2.cs b/src/AdventOfCode/AdventOfCode/Day2.cs index 5e50019..64a8af9 100644 --- a/src/AdventOfCode/AdventOfCode/Day2.cs +++ b/src/AdventOfCode/AdventOfCode/Day2.cs @@ -137,7 +137,7 @@ public static class Day2 while (TryReadLine(ref buffer, out var line)) { - sum += ProcessLine(line, gameSettings); + sum += ProcessLine2(line, gameSettings); } reader.AdvanceTo(buffer.Start, buffer.End); @@ -199,6 +199,39 @@ public static class Day2 } return sum; } + + private static int ProcessLine2(ReadOnlySequence line, GameSettings gameSettings) + { + var str = Encoding.UTF8.GetString(line); + if (string.IsNullOrWhiteSpace(str)) + { + return 0; + } + var game = ParseString(str); + GameSet minGameSet = default; + foreach (var gameSets in game.Games.Values) + { + foreach (var gameSet in gameSets) + { + if (gameSet.Red > minGameSet.Red) + { + minGameSet.Red = gameSet.Red; + } + + if (gameSet.Blue > minGameSet.Blue) + { + minGameSet.Blue = gameSet.Blue; + } + + if (gameSet.Green > minGameSet.Green) + { + minGameSet.Green = gameSet.Green; + } + } + } + + return minGameSet.Red * minGameSet.Green * minGameSet.Blue; + } public static GameData ParseString(string str) {