55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AdventOfCode2023.Day2
|
|
{
|
|
public class Day2_2 : DayBase
|
|
{
|
|
string input = string.Empty;
|
|
|
|
Regex gameIdRegex = new Regex(@"Game (?<ID>\d+):");
|
|
|
|
public Day2_2()
|
|
{
|
|
input = GetInput("Day2.txt");
|
|
}
|
|
|
|
public Day2_2(string testInput)
|
|
{
|
|
input = testInput;
|
|
}
|
|
|
|
public override string Execute()
|
|
{
|
|
var inputLines = input.Split("\r\n");
|
|
List<Game> games = inputLines.Select(LineToGame).ToList();
|
|
|
|
foreach (var game in games)
|
|
{
|
|
var maxima = game.GetMaxima();
|
|
game.Power = maxima.red * maxima.green * maxima.blue;
|
|
}
|
|
|
|
return games.Select(x => x.Power).Sum().ToString();
|
|
}
|
|
|
|
private Game LineToGame(string line)
|
|
{
|
|
if(!gameIdRegex.IsMatch(line))
|
|
{
|
|
throw new Exception($"Line: {line} does not contain game id!");
|
|
}
|
|
|
|
string gameId = gameIdRegex.Match(line).Groups["ID"].Value;
|
|
string setLine = gameIdRegex.Replace(line, "");
|
|
List<string> set = setLine.Split(';').ToList();
|
|
|
|
return new Game(gameId, set);
|
|
}
|
|
}
|
|
}
|