Skip to content

Commit

Permalink
Solved Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Strausbaugh committed Dec 8, 2023
1 parent 4994697 commit a478672
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Day_4/.idea/.idea.Day_4/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Day_4/.idea/.idea.Day_4/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Day_4/.idea/.idea.Day_4/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Day_4/.idea/.idea.Day_4/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Day_4/Day_4.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_4", "Day_4\Day_4.csproj", "{F3B089A1-1DCA-4655-99E0-32E1237FFF21}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F3B089A1-1DCA-4655-99E0-32E1237FFF21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3B089A1-1DCA-4655-99E0-32E1237FFF21}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3B089A1-1DCA-4655-99E0-32E1237FFF21}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3B089A1-1DCA-4655-99E0-32E1237FFF21}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions Day_4/Day_4/Day_4.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<None Update="test.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions Day_4/Day_4/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

const string DATA_FILE = "data.txt";

var lines = ReadData();
var card_copies = new int[lines.Count()];
Array.Fill(card_copies, 1);

var sum = 0;
var card_sum = 0;

for (var i = 0; i < lines.Count(); i++)
{
var score = 0;

var card = lines[i].Split(":", StringSplitOptions.RemoveEmptyEntries);
var numbers = card[1].Split("|", StringSplitOptions.RemoveEmptyEntries);
var win_numbers = numbers[0].Split(" ", StringSplitOptions.RemoveEmptyEntries);
var player_numbers = numbers[1].Split(" ", StringSplitOptions.RemoveEmptyEntries);

var matches = player_numbers.Where(p => win_numbers.Contains(p));

switch (matches.Count())
{
case 0:
score = 0;
break;
case 1:
score = 1;
break;
default:
score = (int)Math.Pow(2, matches.Count() - 1);
break;
}

sum += score;

for (int j = 0; j < card_copies[i]; j++)
{
for (int k = 1; k <= matches.Count(); k++)
{
card_copies[(i + k) >= card_copies.Length ? card_copies.Length - 1 : i + k] += 1;
}
}

card_sum += card_copies[i];
}

Console.WriteLine($"Total Score is {sum}");
Console.WriteLine($"Total Number of Card Copies is {card_sum}"); // 8063216

List<string> ReadData()
{
var data = File.ReadAllLines(DATA_FILE).ToList();

return data;
}
202 changes: 202 additions & 0 deletions Day_4/Day_4/data.txt

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Day_4/Day_4/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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

0 comments on commit a478672

Please sign in to comment.