From b73e58879ad852fc90ce5b2a7e621cefbcd0f57a Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:14:53 -0600 Subject: [PATCH 1/6] chore: scaffold for day 4 puzzle --- 04/Scratchcards.Tests/GlobalUsings.cs | 1 + .../Scratchcards.Tests.csproj | 30 +++++++++++++++++++ 04/Scratchcards.Tests/UnitTest1.cs | 10 +++++++ 04/Scratchcards/Program.cs | 2 ++ 04/Scratchcards/Scratchcards.csproj | 10 +++++++ AdventOfCode2023.sln | 16 ++++++++++ 6 files changed, 69 insertions(+) create mode 100644 04/Scratchcards.Tests/GlobalUsings.cs create mode 100644 04/Scratchcards.Tests/Scratchcards.Tests.csproj create mode 100644 04/Scratchcards.Tests/UnitTest1.cs create mode 100644 04/Scratchcards/Program.cs create mode 100644 04/Scratchcards/Scratchcards.csproj diff --git a/04/Scratchcards.Tests/GlobalUsings.cs b/04/Scratchcards.Tests/GlobalUsings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/04/Scratchcards.Tests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ 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..6793136 --- /dev/null +++ b/04/Scratchcards.Tests/Scratchcards.Tests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/04/Scratchcards.Tests/UnitTest1.cs b/04/Scratchcards.Tests/UnitTest1.cs new file mode 100644 index 0000000..822f43f --- /dev/null +++ b/04/Scratchcards.Tests/UnitTest1.cs @@ -0,0 +1,10 @@ +namespace Scratchcards.Tests; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + + } +} \ No newline at end of file diff --git a/04/Scratchcards/Program.cs b/04/Scratchcards/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/04/Scratchcards/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); 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 From e3f5aeaa97b6af55a33a3e6e700d57041748b5e4 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:18:05 -0600 Subject: [PATCH 2/6] docs: update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f44df71..c62875b 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/) | ⌛ | | 05 | [Problem](./05/PROBLEM.md) | [Solution](./05/) | ⌛ | | 06 | [Problem](./06/PROBLEM.md) | [Solution](./06/) | ⌛ | | 07 | [Problem](./07/PROBLEM.md) | [Solution](./07/) | ⌛ | From d50b7c75cae9aac4c9263ce7f2927981b59eca9e Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:22:44 -0600 Subject: [PATCH 3/6] tests: add tests for GearRatios challenge for actual puzzle input --- 03/GearRatios.Tests/GearRatios.Tests.csproj | 6 ++++++ 03/GearRatios.Tests/PuzzleSolverTests.cs | 12 ++++++++++++ 2 files changed, 18 insertions(+) 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/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, } }; } From 9f2ccdf17fc6647894afbe6fb70ba905c1c8eeee Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:32:14 -0600 Subject: [PATCH 4/6] feat: solve part 1 of puzzle 3 --- 04/Scratchcards.Tests/CardTests.cs | 105 ++++++++++++++++++ 04/Scratchcards.Tests/GlobalUsings.cs | 3 +- 04/Scratchcards.Tests/PuzzleSolverTests.cs | 42 +++++++ .../Scratchcards.Tests.csproj | 6 + 04/Scratchcards.Tests/UnitTest1.cs | 10 -- 04/Scratchcards/Program.cs | 97 +++++++++++++++- 6 files changed, 250 insertions(+), 13 deletions(-) create mode 100644 04/Scratchcards.Tests/CardTests.cs create mode 100644 04/Scratchcards.Tests/PuzzleSolverTests.cs delete mode 100644 04/Scratchcards.Tests/UnitTest1.cs 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 index 8c927eb..7fef4b0 100644 --- a/04/Scratchcards.Tests/GlobalUsings.cs +++ b/04/Scratchcards.Tests/GlobalUsings.cs @@ -1 +1,2 @@ -global using Xunit; \ No newline at end of file +global using Xunit; +global using FluentAssertions; \ No newline at end of file diff --git a/04/Scratchcards.Tests/PuzzleSolverTests.cs b/04/Scratchcards.Tests/PuzzleSolverTests.cs new file mode 100644 index 0000000..2a14baa --- /dev/null +++ b/04/Scratchcards.Tests/PuzzleSolverTests.cs @@ -0,0 +1,42 @@ +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); + } + + public static class TestData + { + private static string[] Input => File.ReadAllLines("INPUT.txt"); + + public static IEnumerable SumCardValuesData => + new List + { + new object[] + { + 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", + "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", + }, + 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 index 6793136..530763c 100644 --- a/04/Scratchcards.Tests/Scratchcards.Tests.csproj +++ b/04/Scratchcards.Tests/Scratchcards.Tests.csproj @@ -27,4 +27,10 @@ + + + PreserveNewest + + + diff --git a/04/Scratchcards.Tests/UnitTest1.cs b/04/Scratchcards.Tests/UnitTest1.cs deleted file mode 100644 index 822f43f..0000000 --- a/04/Scratchcards.Tests/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Scratchcards.Tests; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -} \ No newline at end of file diff --git a/04/Scratchcards/Program.cs b/04/Scratchcards/Program.cs index 3751555..e0acc3b 100644 --- a/04/Scratchcards/Program.cs +++ b/04/Scratchcards/Program.cs @@ -1,2 +1,95 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +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 = puzzleSolver.SumCardValues(input); + + Console.WriteLine($"The sum of all card values is {result}."); + + return result; + } +} + +public class PuzzleSolver +{ + public int SumCardValues(string[] cards) => cards + .Select(Card.Parse) + .Sum(c => c.GetCardValue()); +} + +public class Card( + int id, + List winningNumbers, + List numbers +) +{ + public int Id { get; init; } = id; + public List WinningNumbers { get; init; } = winningNumbers; + public List Numbers { get; init; } = numbers; + + public int GetCardValue() + { + var commonNumbers = WinningNumbers.Intersect(Numbers).ToList(); + + return commonNumbers.Count is 1 + ? 1 + : (int)Math.Pow(2, commonNumbers.Count - 1); + } + + 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 From 475a9def07bc657fc05f71daeb9e6014543deef6 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:44:37 -0600 Subject: [PATCH 5/6] chore: dotnet format --- 03/GearRatios.Tests/GlobalUsings.cs | 2 +- 04/Scratchcards.Tests/GlobalUsings.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) 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/04/Scratchcards.Tests/GlobalUsings.cs b/04/Scratchcards.Tests/GlobalUsings.cs index 7fef4b0..2d5b473 100644 --- a/04/Scratchcards.Tests/GlobalUsings.cs +++ b/04/Scratchcards.Tests/GlobalUsings.cs @@ -1,2 +1,3 @@ +global using FluentAssertions; + global using Xunit; -global using FluentAssertions; \ No newline at end of file From 8b4b0dd8fe4c7c5d729cb30d9d7eb4446029c346 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 4 Dec 2023 13:29:25 -0600 Subject: [PATCH 6/6] docs: add xml comments --- 04/Scratchcards.Tests/PuzzleSolverTests.cs | 43 +++++++--- 04/Scratchcards/Program.cs | 94 +++++++++++++++++++--- README.md | 2 +- 3 files changed, 120 insertions(+), 19 deletions(-) diff --git a/04/Scratchcards.Tests/PuzzleSolverTests.cs b/04/Scratchcards.Tests/PuzzleSolverTests.cs index 2a14baa..b243570 100644 --- a/04/Scratchcards.Tests/PuzzleSolverTests.cs +++ b/04/Scratchcards.Tests/PuzzleSolverTests.cs @@ -12,24 +12,49 @@ public class PuzzleSolverTests 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[] { - 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", - "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", - }, + ExampleInput, 13, }, new object[] diff --git a/04/Scratchcards/Program.cs b/04/Scratchcards/Program.cs index e0acc3b..c838a87 100644 --- a/04/Scratchcards/Program.cs +++ b/04/Scratchcards/Program.cs @@ -19,40 +19,116 @@ public class Program var puzzleSolver = new PuzzleSolver(); var input = await File.ReadAllLinesAsync(args[0]); - var result = puzzleSolver.SumCardValues(input); + var result = args.Length is 2 && args[1] == "part2" + ? puzzleSolver.GetTotalCardCount(input) + : puzzleSolver.SumCardValues(input); - Console.WriteLine($"The sum of all card values is {result}."); + 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; - public int GetCardValue() - { - var commonNumbers = WinningNumbers.Intersect(Numbers).ToList(); + /// + /// Gets the matching numbers. + /// + public List MatchingNumbers => Numbers.Intersect(WinningNumbers).ToList(); - return commonNumbers.Count is 1 - ? 1 - : (int)Math.Pow(2, commonNumbers.Count - 1); - } + /// + /// 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(':'); diff --git a/README.md b/README.md index c62875b..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/Scratchcards/) | ⌛ | +| 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/) | ⌛ |