-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |