Skip to content

Commit

Permalink
2023 Day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Dec 2, 2023
1 parent 4866b8c commit 7721887
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/2023/02/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
using AdventOfCode.Common;
using System.Text.RegularExpressions;
using AdventOfCode.Common;

var lines = Resources.GetInputFileLines();
// Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
var inputRegex = new Regex("Game (?<gameId>\\d+):");
var revealRegex = new Regex("(?<count>\\d+) (?<color>blue|red|green)");
var games = new List<Game>();

foreach (var line in Resources.GetInputFileLines())
{
var id = int.Parse(inputRegex.Match(line).Groups["gameId"].Value);
var reveals = line
.SplitBy(";")
.Select(reveals => revealRegex.Matches(reveals))
.Select(reveals => reveals.Select(reveal =>
(Count: int.Parse(reveal.Groups["count"].Value),
Color: reveal.Groups["color"].Value)))
.Select(reveals => new Reveal(
Red: reveals.FirstOrDefault(r => r.Color == "red").Count,
Blue: reveals.FirstOrDefault(r => r.Color == "blue").Count,
Green: reveals.FirstOrDefault(r => r.Color == "green").Count))
.ToList();

games.Add(new Game(id, reveals));
}

var part1Bag = new Reveal(Red: 12, Green: 13, Blue: 14);
var possibleGames = games
.Where(game => game.Reveals.All(reveal =>
reveal.Red <= part1Bag.Red &&
reveal.Green <= part1Bag.Green &&
reveal.Blue <= part1Bag.Blue));

var leastCubes = games
.Select(game => new Reveal(
Red: game.Reveals.Max(reveal => reveal.Red),
Green: game.Reveals.Max(reveal => reveal.Green),
Blue: game.Reveals.Max(reveal => reveal.Blue)))
.Select(leastCubes => leastCubes.Red * leastCubes.Blue * leastCubes.Green);

Console.WriteLine($"Part 1: {possibleGames.Select(game => game.Id).Sum()}");
Console.WriteLine($"Part 2: {leastCubes.Sum()}");

file record Reveal(int Blue, int Red, int Green);
file record Game(int Id, IReadOnlyCollection<Reveal> Reveals);

0 comments on commit 7721887

Please sign in to comment.