diff --git a/AdventOfCode2024.sln b/AdventOfCode2024.sln index a1e878e..1917065 100644 --- a/AdventOfCode2024.sln +++ b/AdventOfCode2024.sln @@ -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 diff --git a/Day11/Day11.csproj b/Day11/Day11.csproj new file mode 100644 index 0000000..e0c4c2e --- /dev/null +++ b/Day11/Day11.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + enable + enable + + + + + Always + + + + diff --git a/Day11/Program.cs b/Day11/Program.cs new file mode 100644 index 0000000..87d0011 --- /dev/null +++ b/Day11/Program.cs @@ -0,0 +1,49 @@ +var input =File.ReadAllText("input.txt").Split(' ').Select(long.Parse).ToList(); +var part1 = GetStonesAfterIteration(25, new List(input)).Values.Sum().ToString(); +var part2 = GetStonesAfterIteration(75, new List(input)).Values.Sum().ToString(); + +Console.WriteLine($"Part1: {part1}\nPart2: {part2}"); + +Dictionary GetStonesAfterIteration(int iteration, List 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 { { 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 modifications) +{ + if (!modifications.TryAdd(key, value)) modifications[key] += value; +} \ No newline at end of file diff --git a/Day11/input.txt b/Day11/input.txt new file mode 100644 index 0000000..f2caa36 --- /dev/null +++ b/Day11/input.txt @@ -0,0 +1 @@ +572556 22 0 528 4679021 1 10725 2790 \ No newline at end of file