From c3eb1c24a5fe3560cd3857792d78598200ffaf48 Mon Sep 17 00:00:00 2001 From: Rob Date: Fri, 15 Dec 2023 00:13:33 +0100 Subject: [PATCH] almost got it --- AdventOfCode.Solutions/2023/Day 14/Day14.cs | 85 ++++++++++++++++--- .../AdventOfCode.Solutions.csproj | 3 + 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/AdventOfCode.Solutions/2023/Day 14/Day14.cs b/AdventOfCode.Solutions/2023/Day 14/Day14.cs index 78f220e..3fc8218 100644 --- a/AdventOfCode.Solutions/2023/Day 14/Day14.cs +++ b/AdventOfCode.Solutions/2023/Day 14/Day14.cs @@ -24,7 +24,7 @@ namespace AdventOfCode.Solutions._2023 public async Task GetSolutionPart2() { - long maxCycles = 1_000_000_000; + int maxCycles = 1_000_000_000; Dictionary mapStates = []; bool foundLoop = false; @@ -32,25 +32,84 @@ namespace AdventOfCode.Solutions._2023 HashSet bolders = grid.FindWithValue('O').Select(p => new Point(p.X, p.Y)).ToHashSet(); HashSet cubes = grid.FindWithValue('#').Select(p => new Point(p.X, p.Y)).ToHashSet(); + int totalBolders = bolders.Count; + for (int currentCycle = 0; currentCycle < maxCycles; currentCycle++) { + // ensure the state is saved mapStates.TryAdd(CreateStringMap(bolders, cubes, grid.Rows, grid.Columns), currentCycle); + // add a test to see when we hit the right cycle for the test data + // move the boulders bolders = MoveUp(bolders, cubes); + if (bolders.Count != totalBolders) + { + Console.WriteLine($"lost boulders MoveUp at {currentCycle}, from {totalBolders} to {bolders.Count}"); + return string.Empty; + } + bolders = MoveLeft(bolders, cubes); + if (bolders.Count != totalBolders) + { + Console.WriteLine($"lost boulders MoveLeft at {currentCycle}, from {totalBolders} to {bolders.Count}"); + return string.Empty; + } + bolders = MoveDown(bolders, cubes, grid.Rows); + if (bolders.Count != totalBolders) + { + Console.WriteLine($"lost boulders MoveDown at {currentCycle}, from {totalBolders} to {bolders.Count}"); + return string.Empty; + } + bolders = MoveRight(bolders, cubes, grid.Columns); + if (bolders.Count != totalBolders) + { + Console.WriteLine($"lost boulders MoveRight at {currentCycle}, from {totalBolders} to {bolders.Count}"); + return string.Empty; + } + + // see if we found the loop, if so we continue untill the end if (foundLoop) continue; - int hash = bolders.GetHashCode(); + // see if the current layout of the platform already exists + // if so we are repeating our selfs so wel can calculate the remaining steps to get to the end. + // we might even yoink the map from the dic and use that if (mapStates.TryGetValue(CreateStringMap(bolders, cubes, grid.Rows, grid.Columns), out int prevCycle)) { // calculate remaining cycles to the first exit point - long remainingCycles = (maxCycles - prevCycle) % (currentCycle + 1 - prevCycle) + 1; + int remainingCycles = maxCycles % (currentCycle + 1); // set the total the first exit point - maxCycles = remainingCycles + currentCycle; - foundLoop = true; + int exitMap = remainingCycles + currentCycle; + + string[] lines = mapStates.First(kvp => kvp.Value == prevCycle - 1).Key.Split(':', StringSplitOptions.RemoveEmptyEntries); + int totalWeight = 0; + for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++) + { + int weight = lines[lineIndex].Length, lineWeight = 0; + for (int charIndex = 0; charIndex < lines[lineIndex].Length; charIndex++) + { + if (lines[lineIndex][charIndex] is '.') + continue; + + if (lines[lineIndex][charIndex] is '#') + { + weight = lines[lineIndex].Length - charIndex - 1; + continue; + } + + if (lines[lineIndex][charIndex] is 'O') + { + lineWeight += weight; + weight--; + } + } + + totalWeight += lineWeight; + } + + return totalWeight.ToString(); } } @@ -82,7 +141,7 @@ namespace AdventOfCode.Solutions._2023 private HashSet MoveDown(HashSet bolders, HashSet cubes, long ySize) { HashSet boldersMoved = []; - foreach (Point bolder in bolders.OrderBy(bolder => bolder.Y)) + foreach (Point bolder in bolders.OrderByDescending(bolder => bolder.Y)) { // current location long ySearch = bolder.Y; @@ -108,8 +167,8 @@ namespace AdventOfCode.Solutions._2023 // current location long xSearch = bolder.X; while (xSearch - 1 >= 0 // ensure in map - && !boldersMoved.Contains(new(xSearch, bolder.Y)) // check that the next tile does not have a bolder - && !cubes.Contains(new(xSearch, bolder.Y))) // check that the next tile has not cube + && !boldersMoved.Contains(new(xSearch - 1, bolder.Y)) // check that the next tile does not have a bolder + && !cubes.Contains(new(xSearch - 1, bolder.Y))) // check that the next tile has not cube { xSearch--; // move to next } @@ -124,13 +183,13 @@ namespace AdventOfCode.Solutions._2023 private HashSet MoveRight(HashSet bolders, HashSet cubes, long xSize) { HashSet boldersMoved = []; - foreach (Point bolder in bolders.OrderBy(bolder => bolder.Y)) + foreach (Point bolder in bolders.OrderByDescending(bolder => bolder.X)) { // current location - long xSearch = bolder.Y; + long xSearch = bolder.X; while (xSearch + 1 < xSize // ensure in map - && !boldersMoved.Contains(new(xSearch, bolder.Y)) // check that the next tile does not have a bolder - && !cubes.Contains(new(xSearch, bolder.Y))) // check that the next tile has not cube + && !boldersMoved.Contains(new(xSearch + 1, bolder.Y)) // check that the next tile does not have a bolder + && !cubes.Contains(new(xSearch + 1, bolder.Y))) // check that the next tile has not cube { xSearch++; // move to next } @@ -157,7 +216,7 @@ namespace AdventOfCode.Solutions._2023 stringBuilder.Append('.'); } - stringBuilder.AppendLine(); + stringBuilder.Append(':'); } return stringBuilder.ToString(); diff --git a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj index 63f719b..7ecd75f 100644 --- a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj +++ b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj @@ -12,6 +12,9 @@ + + Always + Always