using System.Collections; using System.Net.Mail; namespace AdventOfCode; public class Day02 : BaseDay { private readonly string _input; private int valGames; private int powerSum; public Day02() { _input = File.ReadAllText(InputFilePath); StringReader reader = new(_input); Part1(reader); reader = new(_input); Part2(reader); } public void Part1(StringReader r) { string line = r.ReadLine(); string[] colors = {"red", "green", "blue"}; //cube colors to search for int[] validNum = {12, 13, 14}; //the valid number of cubes in the bag for each color int games = 1; while(line != null) { //Console.WriteLine("Game #" + games); bool validColors = true; foreach(string color in colors) { int currInd = 0; while(line.IndexOf(color, currInd) != -1 && validColors) { int cubeCount = Char.GetNumericValue(line.ElementAt(line.IndexOf(color, currInd)-3)) > 0 ? int.Parse(line.Substring(line.IndexOf(color, currInd)-3, 2)) : (int)Char.GetNumericValue(line.ElementAt(line.IndexOf(color, currInd)-2)); //store the number of cubes in an extremely convuluted and probably overly complicated variable //(it checks if the number is 2 digits) //Console.WriteLine(cubeCount + " " + color); if(cubeCount > validNum[Array.IndexOf(colors, color)]) //if there are more cubes of the color shown than there should be { validColors = false; //game is invalid //Console.WriteLine("Invalid game"); } currInd = line.IndexOf(color, currInd) + color.Length; } } if(validColors) valGames += games; //Console.WriteLine("Current valid game ID total: " + validGamesTotal); games++; line = r.ReadLine(); } } public void Part2(StringReader r) { string line = r.ReadLine(); string[] colors = {"red", "green", "blue"}; //cube colors to search for //int i = 0; while(line != null) { int[] minCubes = new int[3]; //minimum cubes required for game to be possible //Console.WriteLine("Game #" + games); foreach(string color in colors) { int currInd = 0; while(line.IndexOf(color, currInd) != -1) { int cubeCount = Char.GetNumericValue(line.ElementAt(line.IndexOf(color, currInd)-3)) > 0 ? int.Parse(line.Substring(line.IndexOf(color, currInd)-3, 2)) : (int)Char.GetNumericValue(line.ElementAt(line.IndexOf(color, currInd)-2)); //store the number of cubes in an extremely convuluted and probably overly complicated variable //(it checks if the number is 2 digits) //Console.WriteLine(cubeCount + " " + color); if(cubeCount > minCubes[Array.IndexOf(colors, color)]) minCubes[Array.IndexOf(colors, color)] = cubeCount; currInd = line.IndexOf(color, currInd) + color.Length; } //Console.WriteLine(color + " minimum cubes: " + minCubes[Array.IndexOf(colors, color)]); } powerSum += minCubes[0] * minCubes[1] * minCubes[2]; line = r.ReadLine(); } } public override ValueTask Solve_1() => new(valGames.ToString()); public override ValueTask Solve_2() => new(powerSum.ToString()); }