The task description is copyrighted, but it's available here.
Ok, now we are on track. The hardest part of this problem is the parsing, but I introduced a helper that can extract a number in the context of some regular expression which works like a breeze. What's more, we only need to keep track of the maximum of the red, green and blue boxes, so our Game
struct becomes just four integers.
The actual algorithm for Part 1 and Part 2 is very simple, and linq makes it quite readable as well.
namespace AdventOfCode.Y2023.Day02;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
record Game(int id, int red, int green, int blue);
[ProblemName("Cube Conundrum")]
class Solution : Solver {
public object PartOne(string input) => (
from line in input.Split("\n")
let game = ParseGame(line)
where game.red <= 12 && game.green <= 13 && game.blue <= 14
select game.id
).Sum();
public object PartTwo(string input) => (
from line in input.Split("\n")
let game = ParseGame(line)
select game.red * game.green * game.blue
).Sum();
// no need to keep track of the individual rounds in a game, just return
// the maximum of the red, green, blue boxes
Game ParseGame(string line) =>
new Game(
ParseInts(line, @"Game (\d+)").First(),
ParseInts(line, @"(\d+) red").Max(),
ParseInts(line, @"(\d+) green").Max(),
ParseInts(line, @"(\d+) blue").Max()
);
// extracts integers from a string identified by a single regex group.
IEnumerable<int> ParseInts(string st, string rx) =>
from m in Regex.Matches(st, rx)
select int.Parse(m.Groups[1].Value);
}
Please ☆ my repo if you like it!