diff --git a/AdventOfCode/Day02.cs b/AdventOfCode/Day02.cs index 0a407c7..5f62246 100644 --- a/AdventOfCode/Day02.cs +++ b/AdventOfCode/Day02.cs @@ -25,10 +25,13 @@ public override ValueTask Solve_1() public override ValueTask Solve_2() { - return new ValueTask("Not implemented."); + return new ValueTask( + _input + .Select(game => game.GetMinimumSetOfCubes().Power()) + .Sum().ToString()); } - public Game ParseGame(string line) + public static Game ParseGame(string line) { var separatedBySemicolon = line.Split(":"); var gameId = separatedBySemicolon[0].Split(" ")[1]; @@ -38,7 +41,7 @@ public Game ParseGame(string line) return new Game(int.Parse(gameId), subsets); } - public Subset ParseSubset(string line) + public static Subset ParseSubset(string line) { var numberOfBlue = ParseColor(line, "blue"); var numberOfRed = ParseColor(line, "red"); @@ -71,6 +74,26 @@ public Game(int id, IEnumerable subsets) : this() public int Id { get; } public IEnumerable Subsets { get; } + public Subset GetMinimumSetOfCubes() + { + return new Subset(GetFewestRequiredOfBlue(), GetFewestRequiredOfRed(), GetFewestRequiredOfGreen()); + } + + private int GetFewestRequiredOfRed() + { + return Subsets.Select(subset => subset.NumberOfRed).Max(); + } + + private int GetFewestRequiredOfBlue() + { + return Subsets.Select(subset => subset.NumberOfBlue).Max(); + } + + private int GetFewestRequiredOfGreen() + { + return Subsets.Select(subset => subset.NumberOfGreen).Max(); + } + public bool IsPossible() { return Subsets.All(subset => subset.NumberOfBlue <= BlueLimit @@ -79,4 +102,21 @@ public bool IsPossible() } } -public record Subset(int NumberOfBlue, int NumberOfRed, int NumberOfGreen); \ No newline at end of file +public class Subset() +{ + public Subset(int numberOfBlue, int numberOfRed, int numberOfGreen) : this() + { + NumberOfBlue = numberOfBlue; + NumberOfRed = numberOfRed; + NumberOfGreen = numberOfGreen; + } + + public int NumberOfBlue { get; } + public int NumberOfRed { get; } + public int NumberOfGreen { get; } + + public int Power() + { + return NumberOfBlue * NumberOfRed * NumberOfGreen; + } +} \ No newline at end of file diff --git a/AdventOfCodeTests/Day02.cs b/AdventOfCodeTests/Day02.cs index 204b19b..123b7a1 100644 --- a/AdventOfCodeTests/Day02.cs +++ b/AdventOfCodeTests/Day02.cs @@ -13,7 +13,7 @@ public void IdentifiesPossibleGames() Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green """; - var game = new Day02().ParseGame(input); + var game = Day02.ParseGame(input); Assert.True(game.IsPossible()); } @@ -26,7 +26,7 @@ public void IdentifiesImpossibleGames() Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red """; - var game = new Day02().ParseGame(input); + var game = Day02.ParseGame(input); Assert.False(game.IsPossible()); } @@ -35,7 +35,7 @@ public void IdentifiesImpossibleGames() public void CorrectlyParsesGames() { const string line = "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red"; - var game = new Day02().ParseGame(line); + var game = Day02.ParseGame(line); Assert.Equal(3, game.Id); Assert.Equal(3, game.Subsets.Count()); @@ -45,10 +45,35 @@ public void CorrectlyParsesGames() public void CorrectlyParsesSubsets() { const string line = "8 green, 6 blue, 20 red"; - var subset = new Day02().ParseSubset(line); + var subset = Day02.ParseSubset(line); Assert.Equal(8, subset.NumberOfGreen); Assert.Equal(6, subset.NumberOfBlue); Assert.Equal(20, subset.NumberOfRed); } + + [Fact] + public void GetsMinimumSubset() + { + var subset1 = new Subset(1, 4, 5); + var subset2 = new Subset(3, 1, 4); + var subset3 = new Subset(4, 3, 1); + var game = new Game(1, new List { subset1, subset2, subset3 }); + + var result = game.GetMinimumSetOfCubes(); + + Assert.Equal(4, result.NumberOfBlue); + Assert.Equal(4, result.NumberOfRed); + Assert.Equal(5, result.NumberOfGreen); + } + + [Fact] + public void CalculatesPower() + { + var subset = new Subset(6, 4, 2); + + var result = subset.Power(); + + Assert.Equal(48, result); + } } \ No newline at end of file