adventOfCode_2023/CSharp/AdventOfCode/DayTwo/Game.cs

67 lines
1.6 KiB
C#

namespace DayTwo;
public class Game
{
public int Id { get; set; }
public ICollection<string> Steps { get; set; }
private const int BLUE = 14;
private const int RED = 12;
private const int GREEN = 13;
public Game(string line)
{
var headers = line.Split(':');
var id = headers[0].Split(' ')[1];
Id = int.Parse(id);
Steps = headers[1].Split(';');
}
public int GetIdIfValid()
{
if (IsGameValid())
{
return Id;
}
return 0;
}
private bool IsGameValid()
{
foreach (var step in Steps)
{
var colorDictionary = new Dictionary<string, int>();
var colorValues = step.Split(",");
foreach (var colorNumber in colorValues)
{
var separatedValues = colorNumber.Split(" ");
colorDictionary.Add(separatedValues[2], int.Parse(separatedValues[1]));
}
foreach (var colors in colorDictionary)
{
switch (colors.Key)
{
case "red":
if (colors.Value > RED) return false;
break;
case "green":
if (colors.Value > GREEN) return false;
break;
case "blue":
if (colors.Value > BLUE) return false;
break;
default:
break;
}
}
}
return true;
}
}