From a78c95af678447b1ee14597122a4f2a6e46b8c3c Mon Sep 17 00:00:00 2001 From: Rob Stoffelen Date: Mon, 16 Dec 2024 08:48:15 +0100 Subject: [PATCH] Partial day 11 --- AdvendOfCode.Runner/Program.cs | 3 +- AdventOfCode.Solutions/2024/Day 11/Day11.cs | 93 +++++++++++++++++++ .../2024/Day 11/day-11-input.txt | 1 + .../AdventOfCode.Solutions.csproj | 3 + AdventOfCode.Solutions/day-00-input.txt | 11 +-- 5 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 AdventOfCode.Solutions/2024/Day 11/Day11.cs create mode 100644 AdventOfCode.Solutions/2024/Day 11/day-11-input.txt diff --git a/AdvendOfCode.Runner/Program.cs b/AdvendOfCode.Runner/Program.cs index 3fa7eb9..8253946 100644 --- a/AdvendOfCode.Runner/Program.cs +++ b/AdvendOfCode.Runner/Program.cs @@ -4,13 +4,12 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.DependencyInjection; using AdventOfCode.Core.Shared.IO; - IChallange challange = Host.CreateDefaultBuilder() .ConfigureServices(ConfigureServices) .Build() .Services .GetService() - .GetChallange(2024, 6); + .GetChallange(2024, 11); Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}"); diff --git a/AdventOfCode.Solutions/2024/Day 11/Day11.cs b/AdventOfCode.Solutions/2024/Day 11/Day11.cs new file mode 100644 index 0000000..19adfe9 --- /dev/null +++ b/AdventOfCode.Solutions/2024/Day 11/Day11.cs @@ -0,0 +1,93 @@ +using AdventOfCode.Core.Shared.IO; + +namespace AdventOfCode.Solutions._2024 +{ + public class Day11 : IChallange + { + public int Year => 2024; + + public int Day => 11; + + private readonly IInputReader _inputReader; + + public Day11(IInputReader inputReader) + { + _inputReader = inputReader; + _inputReader.SetInput(this); + //_inputReader.SetSampleInput(true); + } + + // 55312 + // 220999 + public async Task GetSolutionPart1() + { + string input = await _inputReader.ReadAsString(); + List stones = input.Split(' ').Select(long.Parse).ToList(); + //Print(stones); + + for (int i = 0; i < 25; i++) + { + stones = ApplyRules(stones); + Console.WriteLine($"{i}: {stones.Count}"); + //Print(stones); + } + + return stones.Count.ToString(); + } + + // 6 + // + public async Task GetSolutionPart2() + { + string input = await _inputReader.ReadAsString(); + List stones = input.Split(' ').Select(long.Parse).ToList(); + long total = 0; + //Print(stones); + for (int i = 0; i < stones.Count; i++) + { + List stone = [stones[i]]; + for (int y = 0; y < 75; y++) + { + stone = ApplyRules(stone); + //Print(stones); + } + total += stone.Count; + } + + return total.ToString(); + } + + private List ApplyRules(List stones) + { + List stonesResult = []; + + for (int i = 0; i < stones.Count; i++) + { + var stone = stones[i]; + string stringStone = stone.ToString(); + + // check length + if (stone == 0) + { + stonesResult.Add(1); + } + else if (stringStone.Length % 2 == 0) + { + stonesResult.Add(long.Parse(stringStone[..(stringStone.Length / 2)])); + stonesResult.Add(long.Parse(stringStone.Substring(stringStone.Length / 2, stringStone.Length / 2))); + } + else + { + stonesResult.Add(stone * 2024); + } + } + + return stonesResult; + } + + private static void Print(List stones) + { + Console.WriteLine($"Stones: {string.Join(", ", stones)}"); + } + } +} \ No newline at end of file diff --git a/AdventOfCode.Solutions/2024/Day 11/day-11-input.txt b/AdventOfCode.Solutions/2024/Day 11/day-11-input.txt new file mode 100644 index 0000000..f89b9fd --- /dev/null +++ b/AdventOfCode.Solutions/2024/Day 11/day-11-input.txt @@ -0,0 +1 @@ +6 11 33023 4134 564 0 8922422 688775 \ No newline at end of file diff --git a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj index 9af0705..f560b26 100644 --- a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj +++ b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj @@ -34,6 +34,9 @@ Always + + Always + Always diff --git a/AdventOfCode.Solutions/day-00-input.txt b/AdventOfCode.Solutions/day-00-input.txt index b60e466..528f9d5 100644 --- a/AdventOfCode.Solutions/day-00-input.txt +++ b/AdventOfCode.Solutions/day-00-input.txt @@ -1,10 +1 @@ -....#..... -.........# -.......... -..#....... -.......#.. -.......... -.#..^..... -........#. -#......... -......#... \ No newline at end of file +125 17 \ No newline at end of file