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

42 lines
1.3 KiB
C#

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