Skip to content

Commit

Permalink
Day 4 (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Dec 4, 2023
1 parent 9d91270 commit 4e8dfc5
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 10 deletions.
24 changes: 24 additions & 0 deletions Day04_2/Day04_2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="input.txt" />
</ItemGroup>

<ItemGroup>
<Content Include="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Tools\Tools.csproj" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions Day04_2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Tools;

var input = InputTools.ReadAllLines();
int[] cardCount = new int[input.Length];
for (int i = 0; i < cardCount.Length; i++)
{
cardCount[i] = 1;
}

for (int cardId = 0; cardId < input.Length; cardId++)
{
string? line = input[cardId];
var parts = line.Split(':');
var numbers = parts[1].Split('|');
var pickedNumbers = numbers[0]
.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(int.Parse)
.ToArray();
var ourNumbers = numbers[1]
.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(int.Parse)
.ToArray();

var matchCount = pickedNumbers.Intersect(ourNumbers).Count();

for (int i = 0; i < matchCount; i++)
{
cardCount[cardId + 1 + i] += cardCount[cardId];
}
}

Console.WriteLine(cardCount.Sum());
7 changes: 7 additions & 0 deletions Day04_2/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"Run": {
"commandName": "Project"
}
}
}
Loading

0 comments on commit 4e8dfc5

Please sign in to comment.