Untitled

mail@pastecode.io avatar
unknown
csharp
6 months ago
1.4 kB
6
Indexable
Never
public abstract class Day2
{
    private static int GetPower(string line)
         {
             var minimums = new Dictionary<string, int>
             {
                 ["red"] = int.MinValue,
                 ["green"] = int.MinValue,
                 ["blue"] = int.MinValue 
             };
             
             var newStr = line.Split(":")[1];
             var newLines = newStr.Split(";");
             foreach (var newLine in newLines)
             {
                 foreach (var val in newLine.Split(","))
                 {
                     var splitStr = val[1..].Split(" ");
                     var currColor = splitStr[1];
                     var currCount = int.Parse(splitStr[0]);
                     // Console.WriteLine(currColor + " " + currCount);
                     minimums[currColor] = int.Max(minimums[currColor], currCount);
                 }
             }
             
             // foreach (var kvp in minimums)
             // {
             //     Console.Write(kvp.Key + " = " + kvp.Value + " ");
             // }
             // Console.WriteLine();
             
             var list = minimums.Values.ToList();
             return list.Aggregate(1, (current, power) => current * power);
         }
        
        public static int Part2()
        {
            return GetLines(InputFile).Sum(GetPower);
        }
}
Leave a Comment