-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add boilerplate for day 4 puzzle * Added day 4 part 1 solution * Added part 2 boilerplate * Add missing day title * Added part 2 solution * Moved puzzle to solved solution folder * Formatted files
- Loading branch information
Showing
15 changed files
with
766 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch (my input)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
"program": "${workspaceFolder}/bin/Debug/net8.0/Day04.dll", | ||
"args": [ | ||
"input.txt" | ||
], | ||
"cwd": "${workspaceFolder}", | ||
"console": "internalConsole", | ||
"stopAtEntry": false | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
}, | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/Day04.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>AdventOfCode.Year2023.Day04</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="mMosiur.AdventOfCode.Abstractions" Version="4.4.0" /> | ||
<PackageReference Include="mMosiur.AdventOfCode.Common" Version="0.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="*.txt"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using AdventOfCode.Abstractions; | ||
using AdventOfCode.Common.EnumerableExtensions; | ||
using AdventOfCode.Year2023.Day04.Puzzle; | ||
|
||
namespace AdventOfCode.Year2023.Day04; | ||
|
||
public sealed class Day04Solver : DaySolver | ||
{ | ||
public override int Year => 2023; | ||
public override int Day => 4; | ||
public override string Title => "Scratchcards"; | ||
|
||
private readonly InputReader _inputReader; | ||
private IReadOnlyList<Scratchcard>? _scratchcards; | ||
|
||
private IReadOnlyList<Scratchcard> Scratchcards => _scratchcards ??= _inputReader.ReadInput(InputLines); | ||
|
||
public Day04Solver(Day04SolverOptions options) : base(options) | ||
{ | ||
_inputReader = new InputReader(); | ||
} | ||
|
||
public Day04Solver(Action<Day04SolverOptions> configure) | ||
: this(DaySolverOptions.FromConfigureAction(configure)) | ||
{ | ||
} | ||
|
||
public Day04Solver() : this(new Day04SolverOptions()) | ||
{ | ||
} | ||
|
||
private static int CountMyWinningNumbers(Scratchcard scratchcard) | ||
{ | ||
HashSet<int> winningNumbers = new(scratchcard.WinningNumbers); | ||
winningNumbers.IntersectWith(scratchcard.MyNumbers); | ||
return winningNumbers.Count; | ||
} | ||
|
||
private static int CalculateCardPoints(Scratchcard scratchcard) | ||
{ | ||
int count = CountMyWinningNumbers(scratchcard); | ||
if (count == 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
return 1 << count - 1; | ||
} | ||
|
||
public override string SolvePart1() | ||
{ | ||
int sum = Scratchcards.Sum(CalculateCardPoints); | ||
return sum.ToString(); | ||
} | ||
|
||
public override string SolvePart2() | ||
{ | ||
foreach ((Scratchcard scratchcard, int index) in Scratchcards.WithIndex()) | ||
{ | ||
int count = CountMyWinningNumbers(scratchcard); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
Scratchcards[index + i + 1].Copies += scratchcard.Copies; | ||
} | ||
} | ||
int sum = Scratchcards.Sum(sc => sc.Copies); | ||
return sum.ToString(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using AdventOfCode.Abstractions; | ||
|
||
namespace AdventOfCode.Year2023.Day04; | ||
|
||
public sealed class Day04SolverOptions : DaySolverOptions | ||
{ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Diagnostics; | ||
using AdventOfCode; | ||
using AdventOfCode.Year2023.Day04; | ||
|
||
try | ||
{ | ||
string? filepath = args.Length switch | ||
{ | ||
0 => null, | ||
1 => args[0], | ||
_ => throw new CommandLineException( | ||
$"Program was called with too many arguments. Proper usage: \"dotnet run [<input filepath>]\"." | ||
) | ||
}; | ||
|
||
Day04Solver solver = new(options => | ||
{ | ||
options.InputFilepath = filepath ?? options.InputFilepath; | ||
}); | ||
|
||
Console.WriteLine($"--- Day {solver.Day}: {solver.Title} ---"); | ||
|
||
Console.Write("Part one: "); | ||
string part1 = solver.SolvePart1(); | ||
Console.WriteLine(part1); | ||
|
||
Console.Write("Part two: "); | ||
string part2 = solver.SolvePart2(); | ||
Console.WriteLine(part2); | ||
} | ||
catch (AdventOfCodeException e) | ||
{ | ||
string errorPrefix = e switch | ||
{ | ||
CommandLineException => "Command line error", | ||
InputException => "Input error", | ||
DaySolverException => "Day solver error", | ||
_ => throw new UnreachableException($"Unknown exception type \"{e.GetType()}\".") | ||
}; | ||
|
||
Console.ForegroundColor = ConsoleColor.Red; | ||
Console.Error.WriteLine($"{errorPrefix}: {e.Message}"); | ||
Console.ResetColor(); | ||
Environment.Exit(1); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Text.RegularExpressions; | ||
using AdventOfCode.Common.SpanExtensions; | ||
|
||
namespace AdventOfCode.Year2023.Day04.Puzzle; | ||
|
||
internal sealed class InputReader | ||
{ | ||
private static readonly Regex ScratchcardRegex = new(@"^\s*Card +(\d+): ([\d ]+) \| ([\d ]+)\s*$", RegexOptions.Compiled); | ||
|
||
private static Scratchcard ReadFromRegexMatch(Match match) | ||
{ | ||
int cardNumber = int.Parse(match.Groups[1].Value); | ||
List<int> winningNumbers = new(10); | ||
foreach (var split in match.Groups[2].ValueSpan.Split(' ')) | ||
{ | ||
if (split.IsEmpty) continue; | ||
winningNumbers.Add(int.Parse(split)); | ||
} | ||
List<int> myNumbers = new(25); | ||
foreach (var split in match.Groups[3].ValueSpan.Split(' ')) | ||
{ | ||
if (split.IsWhiteSpace()) continue; | ||
myNumbers.Add(int.Parse(split)); | ||
} | ||
return new Scratchcard | ||
{ | ||
Id = cardNumber, | ||
WinningNumbers = winningNumbers, | ||
MyNumbers = myNumbers | ||
}; | ||
} | ||
|
||
public IReadOnlyList<Scratchcard> ReadInput(IEnumerable<string> inputLines) | ||
=> inputLines | ||
.Select(l => ScratchcardRegex.Match(l)) | ||
.Select(ReadFromRegexMatch) | ||
.ToArray(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace AdventOfCode.Year2023.Day04.Puzzle; | ||
|
||
internal sealed class Scratchcard | ||
{ | ||
public required int Id { get; init; } | ||
public required IReadOnlyList<int> WinningNumbers { get; init; } | ||
public required IReadOnlyList<int> MyNumbers { get; init; } | ||
|
||
public int Copies { get; set; } = 1; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# [Day 4: Scratchcards](https://adventofcode.com/2023/day/4) | ||
|
||
## Part One | ||
|
||
The gondola takes you up. Strangely, though, the ground doesn't seem to be coming with you; you're not climbing a mountain. As the circle of Snow Island recedes below you, an entire new landmass suddenly appears above you! The gondola carries you to the surface of the new island and lurches into the station. | ||
|
||
As you exit the gondola, the first thing you notice is that the air here is much **warmer** than it was on Snow Island. It's also quite **humid**. Is this where the water source is? | ||
|
||
The next thing you notice is an Elf sitting on the floor across the station in what seems to be a pile of colorful square cards. | ||
|
||
"Oh! Hello!" The Elf excitedly runs over to you. "How may I be of service?" You ask about water sources. | ||
|
||
"I'm not sure; I just operate the gondola lift. That does sound like something we'd have, though - this is **Island Island**, after all! I bet the **gardener** would know. He's on a different island, though - er, the small kind surrounded by water, not the floating kind. We really need to come up with a better naming scheme. Tell you what: if you can help me with something quick, I'll let you **borrow my boat** and you can go visit the gardener. I got all these [scratchcards](https://en.wikipedia.org/wiki/Scratchcard) as a gift, but I can't figure out what I've won." | ||
|
||
The Elf leads you over to the pile of colorful cards. There, you discover dozens of scratchcards, all with their opaque covering already scratched off. Picking one up, it looks like each card has two lists of numbers separated by a vertical bar (`|`): a list of **winning numbers** and then a list of **numbers you have**. You organize the information into a table (your puzzle input). | ||
|
||
As far as the Elf has been able to figure out, you have to figure out which of the **numbers you have** appear in the list of **winning numbers**. The first match makes the card worth **one point** and each match after the first **doubles** the point value of that card. | ||
|
||
For example: | ||
|
||
``` | ||
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 | ||
``` | ||
|
||
In the above example, card 1 has five winning numbers (`41`, `48`, `83`, `86`, and `17`) and eight numbers you have (`83`, `86`, `6`, `31`, `17`, `9`, `48`, and `53`). Of the numbers you have, four of them (`48`, `83`, `17`, and `86`) are winning numbers! That means card 1 is worth **`8`** points (1 for the first match, then doubled three times for each of the three matches after the first). | ||
|
||
- Card 2 has two winning numbers (`32` and `61`), so it is worth **`2`** points. | ||
- Card 3 has two winning numbers (`1` and `21`), so it is worth **`2`** points. | ||
- Card 4 has one winning number (`84`), so it is worth **`1`** point. | ||
- Card 5 has no winning numbers, so it is worth no points. | ||
- Card 6 has no winning numbers, so it is worth no points. | ||
|
||
So, in this example, the Elf's pile of scratchcards is worth **`13`** points. | ||
|
||
Take a seat in the large pile of colorful cards. **How many points are they worth in total?** | ||
|
||
## Part Two | ||
|
||
Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time. | ||
|
||
There's no such thing as "points". Instead, scratchcards only cause you to **win more scratchcards** equal to the number of winning numbers you have. | ||
|
||
Specifically, you win **copies** of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15. | ||
|
||
Copies of scratchcards are scored like normal scratchcards and have the **same card number** as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.) | ||
|
||
This time, the above example goes differently: | ||
|
||
``` | ||
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 | ||
``` | ||
|
||
- Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5. | ||
- Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4. | ||
- Your copy of card 2 also wins one copy each of cards 3 and 4. | ||
- Your four instances of card 3 (one original and three copies) have two matching numbers, so you win **four** copies each of cards 4 and 5. | ||
- Your eight instances of card 4 (one original and seven copies) have one matching number, so you win **eight** copies of card 5. | ||
- Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards. | ||
- Your one instance of card 6 (one original) has no matching numbers and wins no more cards. | ||
|
||
Once all of the originals and copies have been processed, you end up with **`1`** instance of card 1, **`2`** instances of card 2, **`4`** instances of card 3, **`8`** instances of card 4, **`14`** instances of card 5, and **`1`** instance of card 6. In total, this example pile of scratchcards causes you to ultimately have **`30`** scratchcards! | ||
|
||
Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, **how many total scratchcards do you end up with?** |
Oops, something went wrong.