From 23efa3276ea7ca878f3de1495e65fd9d88942d80 Mon Sep 17 00:00:00 2001 From: smabuk <2011834+smabuk@users.noreply.github.com> Date: Tue, 14 Nov 2023 12:33:54 +0000 Subject: [PATCH] 2018 Day12 refactored --- Solutions/2018/Day12.cs | 46 +++++++++++------------------------------ Tests/2018/Tests_12.cs | 26 ----------------------- 2 files changed, 12 insertions(+), 60 deletions(-) diff --git a/Solutions/2018/Day12.cs b/Solutions/2018/Day12.cs index 7e2a0b5..6b4dd2d 100644 --- a/Solutions/2018/Day12.cs +++ b/Solutions/2018/Day12.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace AdventOfCode.Solutions._2018; +namespace AdventOfCode.Solutions._2018; /// /// Day 12: Subterranean Sustainability @@ -15,39 +13,19 @@ public sealed partial class Day12 { private static readonly char NO_PLANT = '.'; private static readonly char PLANT = '#'; - private static int Solution1(string[] input) { - const int noOfGenerations = 20; - string currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{input[0][15..]}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}"; - int offset = 4; - - HashSet plantPatterns = input[2..] - .Where(i => i.EndsWith(PLANT)) - .Select(i => i[..5]) - .ToHashSet(); - - for (int gen = 1; gen <= noOfGenerations; gen++) { - char[] newState = new char[currentState.Length - 4]; - for (int pot = 0; pot < currentState.Length - 4; pot++) { - char plant = plantPatterns.Contains(currentState[pot..(pot + 5)]) ? PLANT : NO_PLANT; - newState[pot] = plant; - } - string state = new(newState); - offset = offset + 2 - state.IndexOf(PLANT); - currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{state.Trim(NO_PLANT)}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}"; - } - - int potPlantSum = 0; - for (int pot = 0; pot < currentState.Length - 4; pot++) { - if (currentState[pot] == PLANT) { - potPlantSum += pot - offset; - } - } - - return potPlantSum; + private static long Solution1(string[] input) { + const long noOfGenerations = 20; + return GrowPlantsAndReturnSum(input, noOfGenerations); } - private static long Solution2(string[] input) { + private static long Solution2(string[] input) + { const long noOfGenerations = 50_000_000_000; + return GrowPlantsAndReturnSum(input, noOfGenerations); + } + + private static long GrowPlantsAndReturnSum(string[] input, long noOfGenerations) + { string currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{input[0][15..]}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}"; long offset = 4; @@ -68,7 +46,7 @@ private static long Solution2(string[] input) { string newState = new(newStateArray); offset = offset + 2 - newState.IndexOf(PLANT); currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{newState.Trim(NO_PLANT)}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}"; - if (state.TryGetValue(currentState, out long stateOffset)) { + if (state.TryGetValue(currentState, out long stateOffset)) { // Turns out this is the previous state that repeats so we have a simple calculation for the remaining iterations offset += (noOfGenerations - gen) * (offset - stateOffset); break; diff --git a/Tests/2018/Tests_12.cs b/Tests/2018/Tests_12.cs index 030bba8..b04119f 100644 --- a/Tests/2018/Tests_12.cs +++ b/Tests/2018/Tests_12.cs @@ -29,30 +29,4 @@ public void Part1(string input, int expected) _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART1, input), out int actual); Assert.Equal(expected, actual); } - - [Theory] - [InlineData(""" - initial state: #..#.#..##......###...### - - ...## => # - ..#.. => # - .#... => # - .#.#. => # - .#.## => # - .##.. => # - .#### => # - #.#.# => # - #.### => # - ##.#. => # - ##.## => # - ###.. => # - ###.# => # - ####. => # - """ - , 325)] - public void Part2(string input, int expected) - { - _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, 32), out int actual); - Assert.Equal(expected, actual); - } }