From 6e433fcf3d25b0d01282e41216704a42f3ad4ee8 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Fri, 24 Jan 2025 10:19:12 +0100 Subject: [PATCH] Add day 11 --- Days/Day11.cs | 127 +++++++++++++++++++++++++++++++++++++++++++++++ Inputs/Day11.txt | 1 + 2 files changed, 128 insertions(+) create mode 100644 Days/Day11.cs create mode 100644 Inputs/Day11.txt diff --git a/Days/Day11.cs b/Days/Day11.cs new file mode 100644 index 0000000..1fc0930 --- /dev/null +++ b/Days/Day11.cs @@ -0,0 +1,127 @@ +using System.Collections.Frozen; +using System.Numerics; +using Spectre.Console; + +namespace AdventOfCode.Days; + +public class Day11 : Day +{ + public override int Number => 11; + public override string Name => "Plutonian Pebbles"; + + private const int BlinkIterations = 25; + + public override void RunPart1(bool display = true) + { + var stones = ParseStones(); + + for (var iteration = 0; iteration < BlinkIterations; iteration++) + { + var index = 0; + + while (index < stones.Count) + { + var stone = stones[index]; + var stoneString = stone.ToString(); + + if (stone is 0) + { + stones[index] = 1; + } + else if (stoneString.Length % 2 == 0) + { + var splitIndex = stoneString.Length / 2; + + var leftStone = long.Parse(stoneString[..splitIndex]); + var rightStone = long.Parse(stoneString[splitIndex..]); + + stones[index] = leftStone; + stones.Insert(index + 1, rightStone); + + index++; + } + else + { + stones[index] = stone * 2024; + } + + index++; + } + } + + if (display) + { + AnsiConsole.MarkupLine($"[green]Number of stones after blinking {BlinkIterations} times: [yellow]{stones.Count}[/][/]"); + } + } + + public override void RunPart2(bool display = true) + { + var stones = ParseStones().ToDictionary(s => s, _ => 1L); + + for (var iteration = 0; iteration < (BlinkIterations * 3); iteration++) + { + var stonesToIterate = stones.ToList(); + + foreach (var (stone, count) in stonesToIterate) + { + var stoneString = stone.ToString(); + + if (stone is 0) + { + SafeAdd(stones, 1, count); + } + else if (stoneString.Length % 2 == 0) + { + var splitIndex = stoneString.Length / 2; + + var leftStone = long.Parse(stoneString[..splitIndex]); + var rightStone = long.Parse(stoneString[splitIndex..]); + + SafeAdd(stones, leftStone, count); + SafeAdd(stones, rightStone, count); + } + else + { + SafeAdd(stones, stone * 2024, count); + + } + + SafeAdd(stones, stone, -count); + } + } + + var stonesCount = stones.Sum(p => p.Value); + + if (display) + { + AnsiConsole.MarkupLine($"[green]Number of stones after blinking {BlinkIterations} times: [yellow]{stonesCount}[/][/]"); + } + } + + private List ParseStones() + { + var stones = new List(); + + var span = Input.AsSpan(); + + foreach (var stoneRange in span.Split(' ')) + { + stones.Add(long.Parse(span[stoneRange])); + } + + return stones; + } + + private void SafeAdd(Dictionary dictionary, long key, long valueToAdd) + { + if (dictionary.TryGetValue(key, out var currentValue)) + { + dictionary[key] = currentValue + valueToAdd; + } + else + { + dictionary[key] = valueToAdd; + } + } +} \ No newline at end of file diff --git a/Inputs/Day11.txt b/Inputs/Day11.txt new file mode 100644 index 0000000..4959fe9 --- /dev/null +++ b/Inputs/Day11.txt @@ -0,0 +1 @@ +0 7 6618216 26481 885 42 202642 8791 \ No newline at end of file