Add day 11
This commit is contained in:
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day9", "Day9\Day9.csproj",
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day10", "Day10\Day10.csproj", "{9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day11", "Day11\Day11.csproj", "{FE187D99-D153-4909-959C-FA9C81444D11}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -69,6 +71,10 @@ Global
|
||||
{9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FE187D99-D153-4909-959C-FA9C81444D11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FE187D99-D153-4909-959C-FA9C81444D11}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FE187D99-D153-4909-959C-FA9C81444D11}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FE187D99-D153-4909-959C-FA9C81444D11}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
16
Day11/Day11.csproj
Normal file
16
Day11/Day11.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="input.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
49
Day11/Program.cs
Normal file
49
Day11/Program.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
var input =File.ReadAllText("input.txt").Split(' ').Select(long.Parse).ToList();
|
||||
var part1 = GetStonesAfterIteration(25, new List<long>(input)).Values.Sum().ToString();
|
||||
var part2 = GetStonesAfterIteration(75, new List<long>(input)).Values.Sum().ToString();
|
||||
|
||||
Console.WriteLine($"Part1: {part1}\nPart2: {part2}");
|
||||
|
||||
Dictionary<long, long> GetStonesAfterIteration(int iteration, List<long> input)
|
||||
{
|
||||
var stones = input.ToDictionary(x => x, x => input.LongCount(y => y == x));
|
||||
stones.TryAdd(1, 0);
|
||||
|
||||
for (var i = 0; i < iteration; i++)
|
||||
{
|
||||
var modifications = new Dictionary<long, long> { { 1, 0 } };
|
||||
foreach (var stone in stones)
|
||||
{
|
||||
if (stone.Key == 0)
|
||||
{
|
||||
AddStone(1, stone.Value, modifications);
|
||||
}
|
||||
else if (stone.Key.ToString().Length % 2 == 0)
|
||||
{
|
||||
var stoneString = stone.Key.ToString();
|
||||
var leftStone = int.Parse(stoneString[..(stoneString.Length / 2)]);
|
||||
var rightStone = int.Parse(stoneString[(stoneString.Length / 2)..]);
|
||||
|
||||
AddStone(leftStone, stone.Value, modifications);
|
||||
AddStone(rightStone, stone.Value, modifications);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddStone(stone.Key * 2024, stone.Value, modifications);
|
||||
}
|
||||
|
||||
stones.Remove(stone.Key);
|
||||
}
|
||||
|
||||
foreach (var modification in modifications) stones[modification.Key] = modification.Value;
|
||||
|
||||
modifications.Clear();
|
||||
}
|
||||
|
||||
return stones.Where(x => x.Value > 0).ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
|
||||
void AddStone(long key, long value, Dictionary<long, long> modifications)
|
||||
{
|
||||
if (!modifications.TryAdd(key, value)) modifications[key] += value;
|
||||
}
|
||||
1
Day11/input.txt
Normal file
1
Day11/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
572556 22 0 528 4679021 1 10725 2790
|
||||
Reference in New Issue
Block a user