2023-12-11 15:59:28 -06:00
|
|
|
using System.Collections;
|
|
|
|
using System.Net.Mail;
|
2023-12-05 14:14:41 -06:00
|
|
|
|
|
|
|
namespace AdventOfCode;
|
|
|
|
|
|
|
|
public class Day02 : BaseDay
|
|
|
|
{
|
|
|
|
private readonly string _input;
|
2023-12-11 15:59:28 -06:00
|
|
|
private int valGames;
|
|
|
|
private int powerSum;
|
2023-12-05 14:14:41 -06:00
|
|
|
|
|
|
|
public Day02()
|
|
|
|
{
|
|
|
|
_input = File.ReadAllText(InputFilePath);
|
|
|
|
StringReader reader = new(_input);
|
|
|
|
Part1(reader);
|
2023-12-11 15:59:28 -06:00
|
|
|
reader = new(_input);
|
|
|
|
Part2(reader);
|
2023-12-05 14:14:41 -06:00
|
|
|
}
|
|
|
|
public void Part1(StringReader r)
|
|
|
|
{
|
|
|
|
string line = r.ReadLine();
|
|
|
|
string[] colors = {"red", "green", "blue"}; //cube colors to search for
|
2023-12-11 15:59:28 -06:00
|
|
|
int[] validNum = {12, 13, 14}; //the valid number of cubes in the bag for each color
|
|
|
|
int games = 1;
|
|
|
|
while(line != null)
|
2023-12-05 14:14:41 -06:00
|
|
|
{
|
2023-12-11 15:59:28 -06:00
|
|
|
//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);
|
2023-12-05 14:14:41 -06:00
|
|
|
foreach(string color in colors)
|
|
|
|
{
|
|
|
|
int currInd = 0;
|
|
|
|
while(line.IndexOf(color, currInd) != -1)
|
|
|
|
{
|
2023-12-11 15:59:28 -06:00
|
|
|
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;
|
2023-12-05 14:14:41 -06:00
|
|
|
}
|
2023-12-11 15:59:28 -06:00
|
|
|
//Console.WriteLine(color + " minimum cubes: " + minCubes[Array.IndexOf(colors, color)]);
|
2023-12-05 14:14:41 -06:00
|
|
|
}
|
2023-12-11 15:59:28 -06:00
|
|
|
powerSum += minCubes[0] * minCubes[1] * minCubes[2];
|
2023-12-05 14:14:41 -06:00
|
|
|
line = r.ReadLine();
|
|
|
|
}
|
|
|
|
}
|
2023-12-11 15:59:28 -06:00
|
|
|
public override ValueTask<string> Solve_1() => new(valGames.ToString());
|
2023-12-05 14:14:41 -06:00
|
|
|
|
2023-12-11 15:59:28 -06:00
|
|
|
public override ValueTask<string> Solve_2() => new(powerSum.ToString());
|
|
|
|
}
|