|
| 1 | +using System.Collections.Immutable; |
| 2 | + |
| 3 | +namespace Day_17; |
| 4 | + |
| 5 | +internal class Program |
| 6 | +{ |
| 7 | + private static void Main(string[] args) |
| 8 | + { |
| 9 | + var line = File.ReadAllLines("input.txt")[0]; |
| 10 | + var directions = line.ToCharArray().Select(x => x switch |
| 11 | + { |
| 12 | + '<' => Direction.Left, |
| 13 | + '>' => Direction.Right, |
| 14 | + _ => throw new() |
| 15 | + }).ToImmutableList(); |
| 16 | + |
| 17 | + var shape1 = new[,] |
| 18 | + { |
| 19 | + { true, true, true, true } |
| 20 | + }; |
| 21 | + |
| 22 | + var shape2 = new[,] |
| 23 | + { |
| 24 | + { false, true, false }, |
| 25 | + { true, true, true }, |
| 26 | + { false, true, false} |
| 27 | + }; |
| 28 | + |
| 29 | + var shape3 = new[,] |
| 30 | + { |
| 31 | + { false, false, true }, |
| 32 | + { false, false, true }, |
| 33 | + { true, true, true} |
| 34 | + }; |
| 35 | + |
| 36 | + var shape4 = new[,] |
| 37 | + { |
| 38 | + { true }, |
| 39 | + { true }, |
| 40 | + { true }, |
| 41 | + { true } |
| 42 | + }; |
| 43 | + |
| 44 | + var shape5 = new[,] |
| 45 | + { |
| 46 | + { true, true }, |
| 47 | + { true, true } |
| 48 | + }; |
| 49 | + |
| 50 | + var shapeList = new List<bool[,]> { shape1, shape2, shape3, shape4, shape5 }.ToImmutableList(); |
| 51 | + var map = new Map(7); |
| 52 | + |
| 53 | + var commandIndex = 0; |
| 54 | + var shapeIndex = 0; |
| 55 | + var currentShape = shapeList[shapeIndex]; |
| 56 | + var shapePosX = 2; |
| 57 | + var shapePosY = 4; |
| 58 | + |
| 59 | + var numShapesSpawned = 1; |
| 60 | + |
| 61 | + while (true) |
| 62 | + { |
| 63 | + // Apply Gas |
| 64 | + var command = directions[commandIndex]; |
| 65 | + commandIndex = (commandIndex + 1) % directions.Count; |
| 66 | + |
| 67 | + switch (command) |
| 68 | + { |
| 69 | + case Direction.Left when map.CanMoveTo(currentShape, shapePosX - 1, shapePosY): |
| 70 | + Console.WriteLine($"Move left to {shapePosX}"); |
| 71 | + shapePosX -= 1; |
| 72 | + break; |
| 73 | + case Direction.Right when map.CanMoveTo(currentShape, shapePosX + 1, shapePosY): |
| 74 | + Console.WriteLine($"Move right to {shapePosX}"); |
| 75 | + shapePosX += 1; |
| 76 | + break; |
| 77 | + case Direction.Left: |
| 78 | + Console.Write("cannot move left"); |
| 79 | + break; |
| 80 | + case Direction.Right: |
| 81 | + Console.Write("cannot move right"); |
| 82 | + break; |
| 83 | + default: |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + if (map.CheckCollision(currentShape, shapePosX, shapePosY - 1)) |
| 88 | + { |
| 89 | + Console.WriteLine("Collision ! Materialize:"); |
| 90 | + map.Materialize(currentShape, shapePosX, shapePosY); |
| 91 | + |
| 92 | + //Render(map); |
| 93 | + |
| 94 | + shapeIndex = (shapeIndex + 1) % shapeList.Count; |
| 95 | + currentShape = shapeList[shapeIndex]; |
| 96 | + shapePosX = 2; |
| 97 | + shapePosY = map.MaxHeight() + 4; |
| 98 | + |
| 99 | + if (numShapesSpawned == 2022) |
| 100 | + { |
| 101 | + // 3195 too low |
| 102 | + Console.WriteLine($"Solution Part 1 {map.MaxHeight()}"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + numShapesSpawned += 1; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + shapePosY -= 1; |
| 111 | + Console.WriteLine($"fall to height {shapePosY}"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void Render(Map map) |
| 117 | + { |
| 118 | + Console.WriteLine("===================="); |
| 119 | + for (int y = map.MaxHeight() - 1; y >= 0; y--) |
| 120 | + { |
| 121 | + for (int x = 0; x < map.Width; x++) |
| 122 | + { |
| 123 | + if (map._heightMap[x][y]) |
| 124 | + { |
| 125 | + Console.Write("#"); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + Console.Write("."); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + Console.WriteLine(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +class Map |
| 140 | +{ |
| 141 | + public readonly List<bool>[] _heightMap; |
| 142 | + |
| 143 | + public Map(int width) |
| 144 | + { |
| 145 | + Width = width; |
| 146 | + _heightMap = new List<bool>[Width]; |
| 147 | + for (int i = 0; i < Width; i++) |
| 148 | + { |
| 149 | + _heightMap[i] = new(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public int Width { get; } |
| 154 | + |
| 155 | + public void Materialize(bool[,] shape, int x, int y) |
| 156 | + { |
| 157 | + var shapeHeight = shape.GetLength(0); |
| 158 | + var shapeWidth = shape.GetLength(1); |
| 159 | + var currentMapMaxHeight = MaxHeight(); |
| 160 | + var missingRowCount = (y + shapeHeight - 1) - currentMapMaxHeight; |
| 161 | + |
| 162 | + ExtendMap(missingRowCount); |
| 163 | + |
| 164 | + for (int sx = 0; sx < shapeWidth; sx++) |
| 165 | + { |
| 166 | + for (int sy = 0; sy < shapeHeight; sy++) |
| 167 | + { |
| 168 | + if (shape[shapeHeight - sy - 1, sx]) |
| 169 | + { |
| 170 | + _heightMap[sx + x][y + sy - 1] = true; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private void ExtendMap(int count) |
| 177 | + { |
| 178 | + if (count <= 0) |
| 179 | + { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + for (int i = 0; i < Width; i++) |
| 184 | + { |
| 185 | + for (int j = 0; j < count; j++) |
| 186 | + { |
| 187 | + _heightMap[i].Add(false); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public bool CheckCollision(bool[,] shape, int x, int y) |
| 193 | + { |
| 194 | + if (y == 0) |
| 195 | + { |
| 196 | + return true; |
| 197 | + } |
| 198 | + |
| 199 | + var shapeHeight = shape.GetLength(0); |
| 200 | + var shapeWidth = shape.GetLength(1); |
| 201 | + |
| 202 | + for (int sy = 0; sy < shapeHeight; sy++) |
| 203 | + { |
| 204 | + if (y + sy > MaxHeight()) |
| 205 | + { |
| 206 | + return false; |
| 207 | + } |
| 208 | + |
| 209 | + for (int sx = 0; sx < shapeWidth; sx++) |
| 210 | + { |
| 211 | + if (_heightMap [sx + x][y + sy - 1] && shape[(shapeHeight - sy) - 1, sx]) |
| 212 | + { |
| 213 | + return true; |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return false; |
| 219 | + } |
| 220 | + |
| 221 | + public int MaxHeight() |
| 222 | + { |
| 223 | + return this._heightMap[0].Count; |
| 224 | + } |
| 225 | + |
| 226 | + public bool CanMoveTo(bool[,] shape, int x, int y) |
| 227 | + { |
| 228 | + var shapeWidth = shape.GetLength(1); |
| 229 | + if (x < 0 || x + shapeWidth > _heightMap.Length) |
| 230 | + { |
| 231 | + return false; |
| 232 | + } |
| 233 | + |
| 234 | + var collision = CheckCollision(shape, x, y); |
| 235 | + |
| 236 | + if (collision) |
| 237 | + { |
| 238 | + Console.WriteLine("Can not move, since collision"); |
| 239 | + return false; |
| 240 | + } |
| 241 | + |
| 242 | + return true; |
| 243 | + |
| 244 | + //if (y > _heightMap.Length) |
| 245 | + //{ |
| 246 | + // return true; |
| 247 | + //} |
| 248 | + |
| 249 | + //for (int i = 0; i < shape.Slices.Count; i++) |
| 250 | + //{ |
| 251 | + // var currentHeight = _heightMap[i + x]; |
| 252 | + // if (currentHeight >= y + shape.Slices[i].ProbeOffset) |
| 253 | + // { |
| 254 | + // return false; |
| 255 | + // } |
| 256 | + //} |
| 257 | + |
| 258 | + //return true; |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +internal enum Direction |
| 263 | +{ |
| 264 | + Left, |
| 265 | + Right |
| 266 | +} |
0 commit comments