diff --git a/03/GearRatios.Tests/GearRatios.Tests.csproj b/03/GearRatios.Tests/GearRatios.Tests.csproj index d3b6c47..e0d4aab 100644 --- a/03/GearRatios.Tests/GearRatios.Tests.csproj +++ b/03/GearRatios.Tests/GearRatios.Tests.csproj @@ -27,4 +27,10 @@ + + + PreserveNewest + + + diff --git a/03/GearRatios.Tests/GlobalUsings.cs b/03/GearRatios.Tests/GlobalUsings.cs index 2d5b473..d6f0167 100644 --- a/03/GearRatios.Tests/GlobalUsings.cs +++ b/03/GearRatios.Tests/GlobalUsings.cs @@ -1,3 +1,3 @@ global using FluentAssertions; -global using Xunit; +global using Xunit; \ No newline at end of file diff --git a/03/GearRatios.Tests/PuzzleSolverTests.cs b/03/GearRatios.Tests/PuzzleSolverTests.cs index 74c000e..5ad3dfd 100644 --- a/03/GearRatios.Tests/PuzzleSolverTests.cs +++ b/03/GearRatios.Tests/PuzzleSolverTests.cs @@ -36,6 +36,8 @@ public class PuzzleSolverTests ".664.598..", ]; + private static readonly string[] Input = File.ReadAllLines("INPUT.txt"); + public static IEnumerable SumGearRatios => new List { @@ -44,6 +46,11 @@ public class PuzzleSolverTests TestSchematic, 467835, }, + new object[] + { + Input, + 81997870, + } }; public static IEnumerable SumPartNumbersData => @@ -84,6 +91,11 @@ public class PuzzleSolverTests "........375...%.........*......450.456.$.........714........851.327..#...+......*...+.......179.630....854.................................*", }, 11612 + }, + new object[] + { + Input, + 550934, } }; } diff --git a/04/Scratchcards.Tests/CardTests.cs b/04/Scratchcards.Tests/CardTests.cs new file mode 100644 index 0000000..2430574 --- /dev/null +++ b/04/Scratchcards.Tests/CardTests.cs @@ -0,0 +1,105 @@ +namespace Scratchcards.Tests; + +public class CardTests +{ + [Theory] + [MemberData(nameof(TestData.ParseCardData), MemberType = typeof(TestData))] + public void Parse_GivenInput_ItShouldReturnExpectedCard(string input, Card expected) + { + var result = Card.Parse(input); + result.Should().BeEquivalentTo(expected); + } + + [Theory] + [MemberData(nameof(TestData.GetCardValueData), MemberType = typeof(TestData))] + public void GetCardValue_GivenCard_ItShouldReturnExpectedValue(Card card, int expected) + { + var result = card.GetCardValue(); + result.Should().Be(expected); + } + + public static class TestData + { + private static readonly Dictionary TestCards = + new() + { + { + "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", + new Card( + 1, + [41, 48, 83, 86, 17], + [83, 86, 6, 31, 17, 9, 48, 53] + ) + }, + { + "Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", + new Card( + 2, + [13, 32, 20, 16, 61], + [61, 30, 68, 82, 17, 32, 24, 19] + ) + }, + { + "Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", + new Card( + 3, + [1, 21, 53, 59, 44], + [69, 82, 63, 72, 16, 21, 14, 1] + ) + }, + { + "Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", + new Card( + 4, + [41, 92, 73, 84, 69], + [59, 84, 76, 51, 58, 5, 54, 83] + ) + }, + { + "Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", + new Card( + 5, + [87, 83, 26, 28, 32], + [88, 30, 70, 12, 93, 22, 82, 36] + ) + }, + { + "Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", + new Card( + 6, + [31, 18, 13, 56, 72], + [74, 77, 10, 23, 35, 67, 36, 11] + ) + }, + }; + + public static IEnumerable ParseCardData => + TestCards + .Select(kvp => new object[] { kvp.Key, kvp.Value }) + .ToList(); + + public static IEnumerable GetCardValueData() + { + for (var i = 0; i < TestCards.Count; i++) + { + var currentCard = TestCards.ElementAt(i); + var expectedValue = i switch + { + 0 => 8, + 1 => 2, + 2 => 2, + 3 => 1, + 4 => 0, + 5 => 0, + _ => throw new Exception("Unexpected card index"), + }; + + yield return new object[] + { + currentCard.Value, + expectedValue + }; + } + } + } +} \ No newline at end of file diff --git a/04/Scratchcards.Tests/GlobalUsings.cs b/04/Scratchcards.Tests/GlobalUsings.cs new file mode 100644 index 0000000..2d5b473 --- /dev/null +++ b/04/Scratchcards.Tests/GlobalUsings.cs @@ -0,0 +1,3 @@ +global using FluentAssertions; + +global using Xunit; diff --git a/04/Scratchcards.Tests/PuzzleSolverTests.cs b/04/Scratchcards.Tests/PuzzleSolverTests.cs new file mode 100644 index 0000000..b243570 --- /dev/null +++ b/04/Scratchcards.Tests/PuzzleSolverTests.cs @@ -0,0 +1,67 @@ +namespace Scratchcards.Tests; + +public class PuzzleSolverTests +{ + private readonly PuzzleSolver _sut = new(); + + [Theory] + [MemberData(nameof(TestData.SumCardValuesData), MemberType = typeof(TestData))] + public void SumCardValues_GivenCards_ItShouldReturnExpectedValue(string[] cards, int expected) + { + var result = _sut.SumCardValues(cards); + result.Should().Be(expected); + } + + [Theory] + [MemberData(nameof(TestData.GetTotalCardCountData), MemberType = typeof(TestData))] + public void GetTotalCardCount_GivenCards_ItShouldReturnExpectedValue(string[] cards, int expected) + { + var result = _sut.GetTotalCardCount(cards); + result.Should().Be(expected); + } + + public static class TestData + { + private static readonly string[] ExampleInput = + [ + "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", + "Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", + "Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", + "Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", + "Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", + "Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", + ]; + + private static string[] Input => File.ReadAllLines("INPUT.txt"); + + public static IEnumerable GetTotalCardCountData => + new List + { + new object[] + { + ExampleInput, + 30, + }, + new object[] + { + Input, + 5920640, + }, + }; + + public static IEnumerable SumCardValuesData => + new List + { + new object[] + { + ExampleInput, + 13, + }, + new object[] + { + Input, + 23235, + }, + }; + } +} \ No newline at end of file diff --git a/04/Scratchcards.Tests/Scratchcards.Tests.csproj b/04/Scratchcards.Tests/Scratchcards.Tests.csproj new file mode 100644 index 0000000..530763c --- /dev/null +++ b/04/Scratchcards.Tests/Scratchcards.Tests.csproj @@ -0,0 +1,36 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + PreserveNewest + + + + diff --git a/04/Scratchcards/Program.cs b/04/Scratchcards/Program.cs new file mode 100644 index 0000000..c838a87 --- /dev/null +++ b/04/Scratchcards/Program.cs @@ -0,0 +1,171 @@ +namespace Scratchcards; + +public class Program +{ + public static async Task Main(string[] args) + { + if (args.Length is 0) + { + Console.WriteLine("Please provide a path to the input file."); + return -1; + } + + if (File.Exists(args[0]) is false) + { + Console.WriteLine("The provided file does not exist."); + return -2; + } + + var puzzleSolver = new PuzzleSolver(); + + var input = await File.ReadAllLinesAsync(args[0]); + var result = args.Length is 2 && args[1] == "part2" + ? puzzleSolver.GetTotalCardCount(input) + : puzzleSolver.SumCardValues(input); + + if (args.Length is 2 && args[1] == "part2") + { + Console.WriteLine($"The total number of cards is {result}."); + } + else + { + Console.WriteLine($"The total value of the cards is {result}."); + } + + return result; + } +} + +/// +/// Solves the puzzle. +/// +public class PuzzleSolver +{ + /// + /// Sums the value of the cards. + /// + /// The cards. + /// The sum of the cards. + public int SumCardValues(string[] cards) => cards + .Select(Card.Parse) + .Sum(c => c.GetCardValue()); + + /// + /// Gets the total number of cards. + /// + /// The cards. + /// The total number of cards. + public int GetTotalCardCount(string[] cards) + { + var cardStringsToProcess = new List(cards); + + for (var i = 0; i < cardStringsToProcess.Count; i++) + { + var currentCardString = cardStringsToProcess[i]; + var currentCardStringIndex = Array.IndexOf(cards, currentCardString); + var currentCard = Card.Parse(currentCardString); + var numOfMatchingNumbersForCurrentCard = currentCard.MatchingNumbers.Count; + var cardIndexesToCopy = Enumerable.Range( + currentCardStringIndex + 1, + Math.Min( + numOfMatchingNumbersForCurrentCard, + cards.Length - currentCardStringIndex - 1 + ) + ); + + foreach (var index in cardIndexesToCopy) + { + cardStringsToProcess.Add(cards[index]); + } + } + + return cardStringsToProcess.Count; + } +} + +/// +/// Represents a card. +/// +/// The identifier. +/// The winning numbers. +/// The numbers. +/// An instance of . +public class Card( + int id, + List winningNumbers, + List numbers +) +{ + /// + /// Gets the identifier. + /// + public int Id { get; init; } = id; + + /// + /// Gets the winning numbers. + /// + public List WinningNumbers { get; init; } = winningNumbers; + + /// + /// Gets the numbers. + /// + public List Numbers { get; init; } = numbers; + + /// + /// Gets the matching numbers. + /// + public List MatchingNumbers => Numbers.Intersect(WinningNumbers).ToList(); + + /// + /// Gets the value of the card. + /// + /// The value of the card. + public int GetCardValue() => MatchingNumbers.Count is 1 + ? 1 + : (int)Math.Pow(2, MatchingNumbers.Count - 1); + + /// + /// Parses the specified input. + /// + /// The input. + /// An instance of . + public static Card Parse(string input) + { + var parts = input.Split(':'); + var cardId = int.Parse( + parts[0].Split( + ' ', + StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries + )[1] + ); + + var numberStrings = parts[1].Trim(); + var numberParts = numberStrings + .Split('|') + .Select(s => s.Trim()) + .ToArray(); + + var winningNumbersString = numberParts[0]; + var numbersString = numberParts[1]; + + var winningNumbers = winningNumbersString + .Split( + ' ', + StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries + ) + .Select(s => s.Trim()) + .Select(int.Parse) + .ToList(); + + var numbers = numbersString + .Split( + ' ', + StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries + ) + .Select(s => s.Trim()) + .Select(int.Parse) + .ToList(); + + return new Card(cardId, winningNumbers, numbers); + } +} \ No newline at end of file diff --git a/04/Scratchcards/Scratchcards.csproj b/04/Scratchcards/Scratchcards.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/04/Scratchcards/Scratchcards.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/AdventOfCode2023.sln b/AdventOfCode2023.sln index 0d08297..3e793ae 100644 --- a/AdventOfCode2023.sln +++ b/AdventOfCode2023.sln @@ -21,6 +21,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GearRatios", "03\GearRatios EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GearRatios.Tests", "03\GearRatios.Tests\GearRatios.Tests.csproj", "{FEE9253C-A56B-4735-BDB3-C62220C5B1B1}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04", "04", "{522D6D1E-54AD-4479-BB78-0AB5D7493EFC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scratchcards", "04\Scratchcards\Scratchcards.csproj", "{B58CF14B-97A1-45F6-967F-29FA82DAFD0F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scratchcards.Tests", "04\Scratchcards.Tests\Scratchcards.Tests.csproj", "{FF8A5512-85FD-45E7-9F97-EC528465949C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -54,6 +60,14 @@ Global {FEE9253C-A56B-4735-BDB3-C62220C5B1B1}.Debug|Any CPU.Build.0 = Debug|Any CPU {FEE9253C-A56B-4735-BDB3-C62220C5B1B1}.Release|Any CPU.ActiveCfg = Release|Any CPU {FEE9253C-A56B-4735-BDB3-C62220C5B1B1}.Release|Any CPU.Build.0 = Release|Any CPU + {B58CF14B-97A1-45F6-967F-29FA82DAFD0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B58CF14B-97A1-45F6-967F-29FA82DAFD0F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B58CF14B-97A1-45F6-967F-29FA82DAFD0F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B58CF14B-97A1-45F6-967F-29FA82DAFD0F}.Release|Any CPU.Build.0 = Release|Any CPU + {FF8A5512-85FD-45E7-9F97-EC528465949C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF8A5512-85FD-45E7-9F97-EC528465949C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF8A5512-85FD-45E7-9F97-EC528465949C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF8A5512-85FD-45E7-9F97-EC528465949C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {3F8EAC09-4BC7-43AA-B72B-48DDD2710F6D} = {8C29858C-623A-461A-BF0B-254E151CD9C2} @@ -62,5 +76,7 @@ Global {4825CE42-4717-4DA6-944F-9BD9A825932B} = {4FD81B09-302F-4C76-BECB-B4559DF3984F} {F697AC19-C92D-4E2A-975A-1A16A1961133} = {EA04152D-316A-4AA4-9BC1-57F6A9F3CD4E} {FEE9253C-A56B-4735-BDB3-C62220C5B1B1} = {EA04152D-316A-4AA4-9BC1-57F6A9F3CD4E} + {B58CF14B-97A1-45F6-967F-29FA82DAFD0F} = {522D6D1E-54AD-4479-BB78-0AB5D7493EFC} + {FF8A5512-85FD-45E7-9F97-EC528465949C} = {522D6D1E-54AD-4479-BB78-0AB5D7493EFC} EndGlobalSection EndGlobal diff --git a/README.md b/README.md index f44df71..ce7b631 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ dotnet run -- part2 | 01 | [Problem](./01/PROBLEM.md) | [Solution](./01/Trebuchet/) | ✅ | The trickiest part here was accounting for overlapping digit words. | | 02 | [Problem](./02/PROBLEM.md) | [Solution](./02/CubeConundrum/) | ✅ | The key to me here was to parse the input into a useful model. | | 03 | [Problem](./03/PROBLEM.md) | [Solution](./03/GearRatios/) | ✅ | The edge case that got me here was lines ending with a part number. | -| 04 | [Problem](./04/PROBLEM.md) | [Solution](./04/) | ⌛ | +| 04 | [Problem](./04/PROBLEM.md) | [Solution](./04/Scratchcards/) | ✅ | Part 2 gets out of hand quickly with just 200 cards. | | 05 | [Problem](./05/PROBLEM.md) | [Solution](./05/) | ⌛ | | 06 | [Problem](./06/PROBLEM.md) | [Solution](./06/) | ⌛ | | 07 | [Problem](./07/PROBLEM.md) | [Solution](./07/) | ⌛ |