AdventOfCode/AdventOfCode.Solutions/2023/Day 02/Day02Part1.cs
Rob 3846b42b7e Massive code base change
partial completion of day 3
2023-12-03 19:09:26 +01:00

49 lines
1.5 KiB
C#

using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
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);
}
}
}