From 09f866c9a3366418b515674544da900d92bd669b Mon Sep 17 00:00:00 2001 From: Parnic Date: Sat, 17 Dec 2022 01:55:17 -0600 Subject: [PATCH] Day 17 solution This is very slow. There are lots of ways to optimize it, but I hate this problem so I probably won't come back to it. But maybe. --- advent-of-code-2022.csproj | 1 + inputs/17.txt | 2 +- inputs/17a.txt | 1 + src/17.cs | 236 +++++++++++++++++++++++++++++++++++++ src/Util/Vec2.cs | 4 +- 5 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 inputs/17a.txt create mode 100644 src/17.cs diff --git a/advent-of-code-2022.csproj b/advent-of-code-2022.csproj index f6b1bc8..dc831a9 100644 --- a/advent-of-code-2022.csproj +++ b/advent-of-code-2022.csproj @@ -71,6 +71,7 @@ + diff --git a/inputs/17.txt b/inputs/17.txt index 65e5855..a1bc4e0 100644 --- a/inputs/17.txt +++ b/inputs/17.txt @@ -1 +1 @@ -target area: x=32..65, y=-225..-177 \ No newline at end of file +>><<<<>>><>><<>>>><<<>>><<>>><<>><>><<<>>>><<<<>>>><<<<>><<<<><<>><<<><<<<>>>><<>>>><<<<>>>><<>>>><><<<>>>><<>>>><<<<><>>>><<<>>>><<><<<>><<<<>>><<>>>><<><<>>><<>>><<<>>><<>><<><<><<>>><<<>><<<>>><>>><<<>>>><<<<><<<>>><>>>><<<>><<><<>>>><<>>><<<>>><<<><<>>>><<<<><<>>>><<>><<<<>><<<>>><><<<>>>><<<><>>><<<>>><<<>>>><<<<>>><<<<>>>><<<>><<<>><>><<<>>>><>>>><<<>><<<<><<<>>><<<<>>><<><<>>><>><<<>>>><<<>>>><>>><<<>>><<<<>>>><<<<>>>><<<<>><<<><<<<>>>><><<<>><<<<><><>>><<<<>>>><<>>><<>>><><><<<>>>><>>><<>><<><<<<><<<>>>><<<>>><>><<>>>><>>><<<>>><><<>>>><>><<>>>><<<>>><<<<>>>><<>>>><<>><<<>>>><<<>>><<<<>><<<<>>><<><<>>>><<>><>><<>>>><><>><<>>>><<>>><<>>>><<<>>>><<<>>><<<>>><<<<>>>><<<><<<>><<<><<<>><<<<>><<<>>><<<<>><<<>>>><<<<>><>><<<>>>><<>>><<<<>>>><<<<>>><<<<><<<<>>><<<<>>><<<<>>><<<<>><<>>><<<<><<<><<<<>>><<<>><<<>>><<<>>><<<<><<<<><<<<>><<>>><<<><<<><<<>>>><<<<>><<<<>>><<>><>>>><<<>><><<<><<<<>>>><<>>><<<><>><<>><>><<>>>><<>>><>>><<<>>>><<>>>><<>>><<<><<>><<<<>><>>><<<>>>><<<><<><<>><<<<><<<><>>>><<>>>><<>><<>>><><<><>>><<><>>><<>>><>><<<>>><<>>>><<<>><<<>>>><<>>><<<><<<<><<<<>>><<>>><<><<>>><<>>><<<><<<><<>>>><>><<<<>><<>>>><<>>><>>><<<>>><<>><<>><<<<>>>><<>>>><<<<>><<<<>><<<<><<<<>>>><<><<<<>>>><<<<>>><<<<>><<<>>>><<>>>><<<<>><<<>>><>>><<<<><><>><<<>>>><>>>><><<<>><<>>>><<<<>><<<<>><>>><<>>><<<<>><<<<>><<<<><<>>>><<><><<<>>>><>>><>>>><<<<><>>><<<>>><>>>><<<<>><><<<<><>>><<<>>>><<<<>>>><>>>><<<>>>><>><><<<<>>>><<<>><><<<><>>><<><<<<>><<><<<<>><<<<>>>><><<><<>>><<<<>><<<<>>><<>>><<>><>>><>>><<<>><><<><<<><<><<>>>><><<<<>><<<>>>><>><>><<<<><>><<<<><>>><<><<<<>>>><<<><<><>><><<<>>><>>>><><>>>><<<<>>><<>>><<<<>>><<>>>><<<<>>>><>>><<<<>>><>><<>>><<<>><<>><<<<>>>><><<><<<<>>><<<>>><<<>><><<<><<>>>><<<<>>><<>><<<>><<>>>><>>><<>>>><<>>><<>>><<<<>>><<<<>>><<<>><<>><<>>>><<<>><>><<<>>>><<>>>><<<><<><<>><<<<><>>><<>><<<<>>>><<<<><>><<<>><<<><><>><>>>><>><<>>><<>><<<>>>><<<<>>><<<<>><<<>><><<<<>>>><<<>>>><<<<>>><<<><<<<>><<<<>>>><<<>>><<>>><<<<><<<<>>><<<<>><<<>><<<<>>>><<<<>>>><><><<<<>>>><<>>>><<<>><<>>>><<><>>><<<>><>>>><>>>><<<>><><<<>>>><>>><<<>><<<<>><><<><><<<<>><<<>>>><<<>><><<><<<<>><<<>>><>>><>>>><<<>>>><<<<>>><<<<>><>>><<<>><<><><>><<<<>>>><>><>>>><<>>>><<>><<>>><<>><<<>><<<>>><<>>><<<<>>><<><<<<>>>><<<>><<<<><<>><<<>>>><><<<<>><<<>><>>>><<>><<<<><>>>><>>>><<>><>>>><<<<>>>><<>>><<<<><<<>>><<>><<<><<<>><<<<>>>><><<<<>>><>><>>>><>>>><<>>><<>>>><<<<><>><>>>><>>><<<<>>><<>><<<<>>>><>><>>>><<<<>>><<<<>>><<<>>><<<<>>><<<<><<<<>><<<><<>><><<<>>><<<>>><<<<>>><<<<>><<<><<<<>>><<>><<<<>><<>><>><<<<>>>><<>>>><<<<><>><><<<<>>><>>><><<>>>><>>>><>>><<<>>>><<<><<><>><<<<>><<>>><<>>><<>>>><>><<><<<>>><>>>><<<><<><<>><<<>>><<><>>>><<<<><>><<<<>>><<>>>><<>>><<>><<<>>><>>><<<<><><<<<>><<<<>><<<<>>><<<>>><<<<>>><<<><<>><>><<<<><>>>><<<>>><>><<>>><<>><<>>>><<<>>><<<<>><<<>>>><<<<>>>><>><<<<><<>><<<<><<>>>><<<><<<>><>>><<>>><<>>><<>>>><<>><<>><<<>>><<><<<><<<>>><<<><<><<<><<<><>>><<>>>><<<<>>>><<<<>>><<<><<<><<<<><<<<>>><<<>>><<><>>><<<<>><>><<><<<>>><<<>><>>><<<<>><<><<>><<<<>>><<<<>><><<<<><<<>>>><<<>><>>><<<<><>>><<<<>>><<<<>>><<<<>>><>>>><<<<><<<>>>><>>>><<<>>>><<<>>><<<<>>><<<<>><>>>><<<<>>><<<<>><>><<><><<<>>><<>><<<<><<<<>>>><<<<>>>><<<>><<<<><<<<>><<<>><<<<><<><<>><<<>><<><<<<>><>><<<<><<<>>><>>>><<<<><<>>>><<<<>>><>>>><<<<><<<<>><>><<>>><<<>><><>><<<<>>>><<>><<<><>><>>>><>><<<<>>><<>>>><>>>><<<>><<<>>>><>>><<<<><<<<>>><<<>>>><>><<>>><<<>><<<>>><<<<>>><<<>>><<>>><<<><<<<>>>><>>><<>>><>>>><<<>>>><<<<>>><<<>>>><<<<>><>>><>>>><<<>>><>>><<<<>>>><<<<>>>><<><<<>><<<>><>><>><<<<>>>><>>><>><<<>><<<><<<><<><><<<>>>><<>><<<>><><>><>>>><<>>><<<>>>><><><<><<<>>><<<><<<<>>>><<<<><><<<<>><>>><<>>><>>><>><<<>><<<<>><<>>><<<>><<>><<<<>>><>><<<>>>><<<<><<>>><<<<><<<<><<<>>><<<><>>><<>><<<>><>><>>>><<>>>><<<>>>><<<>><<<<><<<><>>>><<>>><<>>>><<<<>><<<>><>>>><<<>>>><<>>><<>><<<><<>><>>><<<><<>>><><<<<>>>><<>><<>>><<<><<<>><<<<><<<<>>><<>>><>><>>>><>><>><<>>><<>>><><<<<>><<<>><<<<>>><<>><>>><<<>>>><<>><<<><<<<>><<>><<<>>><<<>><<<>>><<<<>>><>><<>>>><<><<>><<<>>>><<<>>><>><>>><>>><<>>>><<<>>><<<<><>>>><<<<>><<>><><><<<>>><<<<><<<<>>>><<<<>><<>>><<<<>><>>><<<<>>>><<<>><<>><<>>>><>><<<<>>><><<<>>><<<>><<<><<<<>><<>>>><<<<>>><<<<>>><>><>>>><<<>><<<>>><<>>><<<<>>>><>><>><<<>>>><><<<<>><<<>><<>><><>><>>>><<>>>><<>>><<>><<<<>>><>>>><<<<><<><<<<>><<<<>>><<<<><<>>>><<>><<<<>>>><<<>>><<<>><>>>><<<<><<>>>><<>>>><<<<>>>><<>>>><<<<>>><<<>>><<<<><<>><<>>><<<>><<><<>>>><<<>><<<>>><<<>>><>>>><><>>>><<>>>><>>>><<<>>><<>><<<<><<>><<<<>>><<<<>><<<<>><<<<>><<>>><<<>>>><>><<>>>><>>>><<<<><<<>>><>>><<>>>><<<><>>><<>>><<<>><<<<><<<<>>>><<<<>>>><<<<><<><<>><<><>><<><<<<><><<<<>>><<<<>><<>>><<<>>>><<<<>>>><<<><<<<><>>><<<>>><<><><<<>><<>>>><<<<><<<><>>>><<<>><<<<>>>><<<>>><>>><<<<>>>><<<<>>><<<<>><<><<<<>>><<<>>><<<><<><<><<>>>><<><<<>>><<<<><<<><<<<>><>>><<><<><<><<><<<<>>>><<><<<>><<<<><<>>><><>>><><<><<>><<<<>><<<>><><<<>>><<<<>>><>>>><<>><<<<>><<>><<<<>>>><<<<>>><><>><<<<>><<>>>><<>>>><<<>><<><>>>><<<<>><>>>><<<>><<<<>>>><><<<>>>><<><<><>><<<<><<<<>><<>>><>>>><<<<>>><<<>>><<>><<><<>>>><>><>><<<>>><>><<><<<<><<<>>>><<<<>>>><>>>><<<>>>><>>>><<>><<<><<<<>>><>>>><<<<><<<>><<><>>>><<<><>><>>>><>>>><<>>><>>>><<>>><<<<><<>><>><>>><<><<>>><<<><<<<><<><<<<>>><<<>>><<<>><<<>>>><<<<>>><<<<><<<>>><>><>>><>><<<<>><<>>><>>><<<<>>><<<>><>>><<<><<<<>>>><<<<><>>><<<<>><<<>>><>>>><<<>>><<>>>><<>>><><><>>><<<>>><<<>><><<>>><>><<<<>><>>>><<>><<<<>>><<<><<><<>>>><<><<>>>><<<<>>>><<><<>><<>>>><>>>><>>>><<<<>>><><<<<><<<<>><<<<><<>>><>>>><<<<>><<<<>>><<<>>><<<><<<>>>><<>>><<<<>>><<>>><<<>>><<<><<<<><<>>>><<<<>>><<><<<<><<<<><><<<>><<<<><<><<<<><<<<><<><<<>><><>>><>>><<>><<<><<><>><<<<>>><<<><<<>>><<><>>>><<<<>>><<>>><><<<<>>>><<>><<><>><>><>>>><<<>>>><<><<>>>><><<<><>>><<<<>>><<<>>><<<<>>>><<>>><>><><<<><>>>><<>><>>>><<<>>>><>>><<<<>>><<<<>>>><<>><>>>><>><<<>>><<<>><>>><<>>><<><<<>>><<>>>><>><<<>>><<<<>><><<<>><>><<>>><<<<>>>><<<>>>><<<<>>><<<<><<<<>>><<<>><<<<>>>><<>>><<<><>>>><><<>><<<>>>><<>><<<>>><<><>>>><<<>><<>>>><><<<<>><<<<>>><>>>><<<<>><<<<>>>><<><<<>>>><<<>>>><<<<>>><<<>><><<><<<<>>>><<<<>><<>><<<><<<>>><><>>><<>><<<>>><><<<<>>><>><>><><<><<<><<<<>>><<<<><>>><<<>><<>><<><<<><<><>>>><<>><<>>>><<<>>>><>>>><<>>><<><<>>><<>><<<<><>>>><<<><<<>>><<<>>><<>>><<<<>>><<<<><<<<><>><<<>>><>>><>>><<<<>>><<>>>><>><<<<>>><<<<>><<<>>><<<>>><>>>><>>>><<>>><><<><<><>>>><>><<>>>><<<>><<<><<<<>>>><<>><>>><<>><>>>><<<>>><>><<><>>><<<<><<<<>>><<>><<>>>><<<><<><<<<><>><<<<>>><<<><><<<><<<><>><<>>>><<>>>><<><<>>><<>>>><>>>><<><<<>>>><<<>><<<>><<<<>>><<>>>><<<<>>>><>>><<<><<<<><<<<><><<>>>><<<<>>><>>>><<><<<>><<<>>>><<<<>>><<<<>>>><>>>><<>>>><<<<><>>>><<<<>>>><<>>>><<<<>>><<>>><<<>>>><>>><<<<>>><><<<>>><<><<<<><<<<>>>><<><>>><<><<<<>>>><<<<>>>><<<>><<<><<<<><<<><>>>><<<>>><<>>><>>><<<<><<<<><>>>><<<>><<<<>>>><<<<>><<<<>>>><<<<>>><<<>>>><<<>>><<<<><>><<<<><>>>><<<>>><<<>>><<<<><<><>>><>><<<<>>><<>>><<<<>><<><>>>><<><<>>>><<<<>><<<>>>><<>><<<><<<<>>>><>><>><<<>>>><>>><<>><<<<>>><<<><<>>>><<<<><<>>>><>><>><<<><><><<<><<><<>><<><<<<><<<<>><<<><<>>>><<><<>>>><>><<><<<<>>>><>>><<>>>><<><>>>><<<><<<<><<><>><<><<<>>><<><<<<>>><<><<<<>>><<<>>><><<<<>>>><><>>><<>>><><<<<>>><<><<>>>><<<<>><<>>>><>>>><<>>><<><<<<>><<>>>><<><<<<>><<<><>>>><<<<>>><<<<>>><<<>><<<><<<<>>><<<<>><<>>>><>>><>>>><>>><<><<<>><<>>><<>>>><<><<<>>>><>>><><<<>>>><><>>><<<<>>>><<<>>><<>><<>>><<<>>><<>>><>>>><<<>><<<<><<>><<<>>>><<<<><<>><<>>>><<<<><<<<>>>><><<>><<<>>>><<>>>><<><<<><<>><<><<><<<<><>>><<<>>><>><<<>><<<<><><>><<<<><<><<<><<<<>>>><>>>><<<<>>>><>>>><<<>>>><><<<<>>>><<>>>><<>>>><<<<><<<>>>><<<><>>><>><<<<><<<<>>><<<<>>><<>>>><><<<>><<<<>>><<>><<>>>><<<>><<<<>>>><<<<><<<<>>><<<<>><<>>><<<<><<>><<<<><>><<<><<>><<>><<><<<>><<<<>><<>>>><<>>>><>>><<<<>>><<<>><<<>>>><<<<>>>><<<<><<<<>><<<<>>>><<<>>><<<<><>><<>>>><>>>><<><<<>>>><<>>>><>>>><<>><>><<<<><<<>>><<><<<<>>><<>><<<>>><<<<>><<<>><<><><<<>>><<<>>><<>><<<<>>>><<<>>><<><<<>>>><<><>>>><>>>><>>><<<>>><>>>><<>>>><<><<<>>>><<>><<<<>>><<<<>><<<>>><<>>><<<>><<<><<<>>>><>>>><>>>><<<>>><<<><<><<<<>><<<<>>>><<<<>>><<<<><<>>>><><<<<><<<><<<<><<<<>>><>>>><<>><<<><<<<><<<<>>><<>>>><<>>>><<<>><<>>><<>>><<<<>>><<>>>><<<<>>><>>>><<<<>><<><<>>><<>>>><<>>><<<<><<><<<<>>>><<<<><<<>>><<><<<<>>>><<>>><<>>>><>><>><<<<>><<<<>>>><<<>><>>><<<<>><>>><<<>>>><<>><<<>>>><<<>>>><>><>>>><<<><<<<><<<>><>>>><>>><<>><<><<>>>><<>><<<>>><<>><<>>>><<<>>><<>>>><<><<<>>>><<>>><<<<>>>><<<>>>><<<<>>><>>>><<>><<<<>>><><<>>>><<<>><<>><<<>><>>><>>>><<<>><><>><<<>>>><<>>>><<<>>><<><<<<>><<<<>><<<><<>><>>>><<<<><<>>><<<<>>>><<<<>>>><<>>>><<<<><<<<><<<>>><>><>>>><<<<><<<>>>><>><<>><<<<>><<<>><<>><<<<>><><<>><<<>>><>>><<<<>>>><<<>><<>>><<>><<<>>><<<>>>><<<><<>><<<>>><<<>><<<><<>><<<>>>><>><<<<><<<<>><<<<>><><<<<>>>><>>><><<>>>><<>>><><>>>><<>>>><><<>><><<>>><<<<>><>>><<>>>><>><<<>>><<<><<><<<<><<><>><<>><<<><<<>><><<><<<>><<<<>>><<<<>>>><<<<>>><<><<><>><<<>><<<<>>>><>>>><<>><>>><>><><<<><<<<>>><<<>>>><<>>><<<>><<<>><<<>>>><>>>><>>>><<<<>>>><><<<>><<<<><<<<>>>><<<<>>><>><<<<>><<<<>>><<>>><>><<>>>><<<>><<>><<<>>><<<<><<><<<>><<<<><<<<>>>><>><<<>>>><<>><>><<<>>>><<<>><>>><<<<>>>><>>>><>>>><<<>>><<<>>>><<<<>>>><<>>><<<<>><<<<><<<<>>>><<<>><<<><<<><<>>><>>><<><<<<><<<<><<<<>><><>>>><<<><<<>>>><<>>><>>>><<<<>><<<<>>>><<<<>>><<<<>>>><<>><>>><<<>>>><<<>>><><<>>>><<<<>>>><<<<>>><<<>>><<<>><>>><<><><<<>><<><>>><<<>>>><><>><<>><<><<<<>>>><>>>><>>><<<><<<><<<<>>><>><<<>>><<><<<>><<<>><><><>>>><<<<><<>>><>><<<>><>>>><<<>>>><<<<>>><<>>><<<>>><<<><<<>><<<<>>>><>>>><<<<>>>><>>>><<<<>><><<>>><<<<>>><<<>>><<><<<>>><<>>><<>>><<>>><>>>><>>><><<<><<>>>><<><<>>>><<<>><<<>>><<>>><>>>><<<>>>><<<<>><><<>>>><<>>>><><>>>><<<>>>><>>><<><><>><<<<>>>><<><>><<<>>><<>>><<<>><<<>>>><>>><<><>>>><<><>>><<>><<><<<<>>>><<><<>>><>>>><>>><<<<><<<<>><><<<<>><<<>>><<<<><<>>><<<<>>>><>><<<<>>>><<>>><>>><<>>><<>>>><<<><>>><><>>><<><<>><<<<>>><<<>>><<<><<<><<<<>>><<<>>>><>><><<<<>>><<><<<<>><<>>>><<<<>>>><<>>>><<>><>>><<>>>><>>><<>>>><<>><<<>><>>>><<<<><<<<>><<<>>><<<<>>><<<>>>><><>><<<>>>><<<<>>><<<>>>><<<><<<<><<<>>>><>><<<>>><<<<>><<><<<<>><<<<>>><<<<>><<<<>><<><<<<>><>><<>><<<<>>><<<>>><<<>>>><<<>><>>><<<<><<<>>><<<>>><<>>>><<<>><<<>>><<>><><>>><<><<><<<>>><<>>><< \ No newline at end of file diff --git a/inputs/17a.txt b/inputs/17a.txt new file mode 100644 index 0000000..b22d4e8 --- /dev/null +++ b/inputs/17a.txt @@ -0,0 +1 @@ +>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> \ No newline at end of file diff --git a/src/17.cs b/src/17.cs new file mode 100644 index 0000000..42252b8 --- /dev/null +++ b/src/17.cs @@ -0,0 +1,236 @@ +using aoc2022.Util; + +namespace aoc2022; + +internal class Day17 : Day +{ + private List jetPatterns = new(); + + private ivec2[][] rocks = + { + new[]{ + new ivec2(0, 0), + new ivec2(1, 0), + new ivec2(2, 0), + new ivec2(3, 0), + }, + new[]{ + new ivec2(0, 1), + new ivec2(1, 0), + new ivec2(1, 1), + new ivec2(1, 2), + new ivec2(2, 1), + }, + new[]{ + new ivec2(0, 2), + new ivec2(1, 2), + new ivec2(2, 0), + new ivec2(2, 1), + new ivec2(2, 2), + }, + new[]{ + new ivec2(0, 0), + new ivec2(0, 1), + new ivec2(0, 2), + new ivec2(0, 3), + }, + new[]{ + new ivec2(0, 0), + new ivec2(0, 1), + new ivec2(1, 0), + new ivec2(1, 1), + }, + }; + + internal override void Parse() + { + var content = Parsing.ReadAllText("17"); + foreach (var ch in content) + { + jetPatterns.Add(ch == '>'); + } + } + + private const int maxWidth = 7; + private bool canMove(int rockIdx, ivec2 rockPos, ICollection grid, ivec2 direction) + { + var rock = rocks[rockIdx]; + var rockWidth = rock.MaxBy(v => v.x).x + 1; + var rockHeight = rock.MaxBy(v => v.y).y; + var result = rockPos + direction; + if (result.x < 0) + { + return false; + } + if (result.x + rockWidth > maxWidth) + { + return false; + } + + if (result.y - rockHeight == -1) + { + return false; + } + + foreach (var pt in rock) + { + if (grid.Contains(result + new ivec2(pt.x, -pt.y))) + { + return false; + } + } + + return true; + } + + internal override string Part1() + { + int settledRocks = 0; + HashSet grid = new(); + int jetSteps = 0; + for (int idx = 0; settledRocks < 2022; idx++) + { + var rockIdx = idx % rocks.Length; + var maxHeight = grid.Count > 0 ? grid.MaxBy(v => v.y).y + 1 : 0; + var rockHeight = rocks[rockIdx].MaxBy(v => v.y).y; + var rockPos = new ivec2(2, maxHeight + rockHeight + 3); + bool settled = false; + while (!settled) + { + var jetIdx = jetSteps % jetPatterns.Count; + jetSteps++; + if (jetPatterns[jetIdx]) + { + if (canMove(rockIdx, rockPos, grid, ivec2.RIGHT)) + { + rockPos.x++; + } + } + else + { + if (canMove(rockIdx, rockPos, grid, ivec2.LEFT)) + { + rockPos.x--; + } + } + + // inverted up/down for this... + if (canMove(rockIdx, rockPos, grid, ivec2.UP)) + { + rockPos.y--; + } + else + { + settled = true; + } + + if (settled) + { + foreach (var pt in rocks[rockIdx]) + { + grid.Add(rockPos + new ivec2(pt.x, -pt.y)); + } + + settledRocks++; + } + } + } + + var finalMaxHeight = grid.MaxBy(v => v.y).y + 1; + return $"After 2022<+black> rocks, tower height is: <+white>{finalMaxHeight}"; + } + + internal override string Part2() + { + long settledRocks = 0; + HashSet grid = new(); + List heightDeltasPerStep = new(); + long jetSteps = 0; + for (long idx = 0; settledRocks < 10000; idx++) + { + var rockIdx = (int)(idx % rocks.Length); + var maxHeight = grid.Count > 0 ? grid.MaxBy(v => v.y).y + 1 : 0; + var rockHeight = rocks[rockIdx].MaxBy(v => v.y).y; + var rockPos = new ivec2(2, maxHeight + rockHeight + 3); + bool settled = false; + while (!settled) + { + var jetIdx = (int)(jetSteps % jetPatterns.Count); + jetSteps++; + if (jetPatterns[jetIdx]) + { + if (canMove(rockIdx, rockPos, grid, ivec2.RIGHT)) + { + rockPos.x++; + } + } + else + { + if (canMove(rockIdx, rockPos, grid, ivec2.LEFT)) + { + rockPos.x--; + } + } + + // inverted up/down for this... + if (canMove(rockIdx, rockPos, grid, ivec2.UP)) + { + rockPos.y--; + } + else + { + settled = true; + } + + if (settled) + { + foreach (var pt in rocks[rockIdx]) + { + grid.Add(rockPos + new ivec2(pt.x, -pt.y)); + } + + settledRocks++; + + var newMaxHeight = grid.MaxBy(v => v.y).y + 1; + heightDeltasPerStep.Add(newMaxHeight - maxHeight); + } + } + } + + int cycleSize = 0; + int skip = 0; + for (int jumpAhead = 250; jumpAhead < 500 && skip == 0; jumpAhead++) + { + var skipped = heightDeltasPerStep.Skip(jumpAhead); + for (int cycleLen = 100; cycleLen < heightDeltasPerStep.Count / 4; cycleLen++) + { + var chunked = skipped.Chunk(cycleLen); + if (chunked.All(c => c.Length < cycleLen || c.SequenceEqual(chunked.First()))) + { + skip = jumpAhead; + cycleSize = cycleLen; + break; + } + } + } + + if (skip == 0) + { + throw new Exception("cycle not found"); + } + + var baseHeight = heightDeltasPerStep.Take(skip).Sum(); + var chunkHeight = heightDeltasPerStep.Skip(skip).Take(cycleSize).Sum(); + var cumulativeHeight = baseHeight; + long simmed; + for (simmed = skip; simmed + cycleSize < 1000000000000; simmed += cycleSize) + { + cumulativeHeight += chunkHeight; + } + + var finalChunkHeight = heightDeltasPerStep.Skip(skip).Take((int)(1000000000000 - simmed)).Sum(); + cumulativeHeight += finalChunkHeight; + + return $"After skipping {skip}<+black> initial drops, a cycle of size {cycleSize}<+black> produced a final height of <+white>{cumulativeHeight}"; + } +} diff --git a/src/Util/Vec2.cs b/src/Util/Vec2.cs index 38efc61..b8641d2 100644 --- a/src/Util/Vec2.cs +++ b/src/Util/Vec2.cs @@ -2,8 +2,8 @@ public struct ivec2 : IEquatable, IComparable, IComparable { - public readonly long x = 0; - public readonly long y = 0; + public long x = 0; + public long y = 0; public static readonly ivec2 ZERO = new ivec2(0, 0); public static readonly ivec2 ONE = new ivec2(1, 1);