From df41b9faab9877917d66d2e34f022d699e574a60 Mon Sep 17 00:00:00 2001 From: deckelmouck Date: Sat, 2 Dec 2023 15:48:33 +0100 Subject: [PATCH] soved day 02 part 2 --- 2023/Day02/solution.cs | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/2023/Day02/solution.cs b/2023/Day02/solution.cs index cc61a99..5a257d1 100644 --- a/2023/Day02/solution.cs +++ b/2023/Day02/solution.cs @@ -93,6 +93,58 @@ public void SolvePart1() public void SolvePart2() { Console.WriteLine("part2"); + + solutionBase sb = new(); + var input = sb.getInputLines(@"2023/Day02/input"); + //var input = sb.getInputLines(@"2023/Day02/testpartone"); + + List games = new(); + + foreach (var item in input) + { + string gamenumber = item.Split(':')[0].Replace("Game ", ""); + Console.WriteLine(gamenumber); + + int gno = Convert.ToInt32(gamenumber); + + string plays = item.Split(':')[1]; + + List playlist = new(); + + foreach (var set in plays.Split(';')) + { + int red = 0; + int green = 0; + int blue = 0; + + foreach (var c in set.Split(',')) + { + if(c.Contains("red")) + { + red = GetIntOutOfString(c); + } + else if(c.Contains("green")) + { + green = GetIntOutOfString(c); + } + else if(c.Contains("blue")) + { + blue = GetIntOutOfString(c); + } + } + + Cubes cubesSet = new Cubes(red, green, blue); + playlist.Add(cubesSet); + } + + Game game = new Game(gno, playlist); + games.Add(game); + } + + Console.WriteLine($"there are {games.Count} games in input"); + + int sum = games.Sum(x => x.Power()); + Console.WriteLine($"the sum of all powers in: {sum} "); } int GetIntOutOfString(string input) @@ -117,6 +169,20 @@ internal Game (int g, List lc) game = g; listCubes = lc; } + + internal int Power() + { + int maxRed = 0; + int maxGreen = 0; + int maxBlue = 0; + + maxRed = listCubes.Max(x => x.red); + maxGreen = listCubes.Max(x => x.green); + maxBlue = listCubes.Max(x => x.blue); + + + return maxBlue * maxGreen * maxRed; + } } class Cubes