Skip to content

Instantly share code, notes, and snippets.

@dkaraush
Last active December 4, 2025 09:11
Show Gist options
  • Select an option

  • Save dkaraush/67fd4ed7a47ac0ad1fe3717bda675ca6 to your computer and use it in GitHub Desktop.

Select an option

Save dkaraush/67fd4ed7a47ac0ad1fe3717bda675ca6 to your computer and use it in GitHub Desktop.
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