50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
namespace AdventOfCodeLibrary._2023
|
|
{
|
|
using AdventOfCodeLibrary.Shared;
|
|
using System.Text.RegularExpressions;
|
|
|
|
public class Day02Part1 : Answerable
|
|
{
|
|
public override int Year { get; set; } = 2023;
|
|
public override int Day { get; set; } = 2;
|
|
public override int Part { get; set; } = 1;
|
|
|
|
public override string GetAnswer(byte[] data)
|
|
{
|
|
return GetAsStringArray(data)
|
|
.Select(ParseGames)
|
|
.Sum()
|
|
.ToString();
|
|
}
|
|
|
|
private int ParseGames(string line)
|
|
{
|
|
// get all the numers
|
|
string gameId = line.Split(':')[0].Split(' ')[1];
|
|
string[] dicePulls = line.Split(':')[1].Split(';');
|
|
|
|
// parse the pulls
|
|
foreach (string dicePull in dicePulls)
|
|
{
|
|
// regex the pull
|
|
MatchCollection dice = Regex.Matches(dicePull, "(\\d+) (g|r|b)");
|
|
foreach (Match match in dice)
|
|
{
|
|
string[] splitted = match.Value.Split(' ');
|
|
int ammount = int.Parse(splitted[0]);
|
|
|
|
if (ammount <= 12) // pass for all dice
|
|
continue;
|
|
|
|
if (ammount > 14 // fail for any dice set
|
|
|| (splitted[1] == "r" && ammount > 12)
|
|
|| (splitted[1] == "g" && ammount > 13))
|
|
return 0; // not possible
|
|
}
|
|
|
|
}
|
|
|
|
return int.Parse(gameId);
|
|
}
|
|
}
|
|
} |