Last active
December 4, 2025 09:11
-
-
Save dkaraush/67fd4ed7a47ac0ad1fe3717bda675ca6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string[] lines = File.ReadAllLines("input.txt"); | |
| int W = lines[0].Length, H = lines.Length; | |
| char[][] map = lines | |
| .Select(l => $".{l}.") | |
| .Prepend(new string('.', 2 + W)) | |
| .Append(new string('.', 2 + W)) | |
| .Select(l => l.ToCharArray()) | |
| .ToArray(); | |
| (int dx, int dy)[] NeighbourOffsets = { | |
| (-1, -1), (-1, 0), (-1, +1), (0, +1), | |
| (+1, +1), (+1, 0), (+1, -1), (0, -1) | |
| }; | |
| int CountNeighbours(int x, int y, char b) => | |
| NeighbourOffsets.Count(o => map[y + o.dy][x + o.dx] == b); | |
| int FindRolls(bool remove = false) { | |
| int removed = 0; | |
| for (int y = 1; y <= H; ++y) | |
| for (int x = 1; x <= W; ++x) { | |
| if (map[y][x] != '@') continue; | |
| if (CountNeighbours(x, y, '@') >= 4) continue; | |
| removed++; | |
| if (remove) | |
| map[y][x] = '.'; | |
| } | |
| return removed; | |
| } | |
| Console.WriteLine(FindRolls()); | |
| int result2 = 0, removed = 0; | |
| while ((removed = FindRolls(true)) > 0) | |
| result2 += removed; | |
| Console.WriteLine(result2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment