AdventOfCode/Advent Of Code Library/2023/Day 02/Day02Part1.cs
2023-12-02 13:07:39 +01:00

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);
}
}
}