43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
namespace AdventOfCodeLibrary._2023
|
|
{
|
|
using AdventOfCodeLibrary.Shared;
|
|
using System.Text.RegularExpressions;
|
|
|
|
public class Day02Part2 : Answerable
|
|
{
|
|
public override int Year { get; set; } = 2023;
|
|
public override int Day { get; set; } = 2;
|
|
public override int Part { get; set; } = 2;
|
|
|
|
public override string GetAnswer(byte[] data)
|
|
{
|
|
return GetAsStringArray(data)
|
|
.Select(GetValueFromLine)
|
|
.Sum()
|
|
.ToString();
|
|
}
|
|
|
|
private int GetValueFromLine(string line)
|
|
{
|
|
// get all the numers
|
|
string dicePulls = line.Split(':')[1];
|
|
|
|
// parse the pulls
|
|
MatchCollection dice = Regex.Matches(dicePulls, "(\\d+) (g|r|b)");
|
|
int red = 0, green = 0, blue = 0;
|
|
foreach (Match match in dice)
|
|
{
|
|
string[] splitted = match.Value.Split(' ');
|
|
int ammount = int.Parse(splitted[0]);
|
|
|
|
if (splitted[1] == "r" && ammount > red) { red = ammount; }
|
|
else if (splitted[1] == "g" && ammount > green) { green = ammount; }
|
|
else if (splitted[1] == "b" && ammount > blue) { blue = ammount; }
|
|
}
|
|
|
|
|
|
return red * green * blue;
|
|
}
|
|
}
|
|
}
|