Given an input of an ASCII art "road," output the road with all dead ends labelled.
This is a road:
########.....######..#..###
#......#######....#..#..#.#
#.##......#...#####..#..###
#..#####..#....#..#######.#
#......#...#####.....##...#
#..###.#...#...###...#..###
##########.#..#..##..#.##.#
..#......#.######.#..#.#.#.
..#......#.#..#.#.#..#.#.#.
..######.###..##..#########
This is the road with dead ends labelled with the letter X
:
########.....######..X..###
#......#######....#..X..#.#
#.XX......X...X####..X..###
#..XXXXX..X....#..#######.#
#......X...#####.....##...#
#..###.X...#...###...#..###
##########.#..X..##..#.##.X
..X......#.#XXXXX.#..#.#.X.
..X......#.#..X.X.#..#.#.X.
..XXXXXX.###..XX..######XXX
A dead end is defined as any road tile that borders n other road tiles, at least n-1 of which are considered dead ends already by this rule. "Bordering" is in the four cardinal directions, so tiles bordering diagonally don't count.
This rule is applied repeatedly, as newly created dead ends can, themselves, create more dead ends. Also note that any road tile that borders only one other road tile is considered a dead end the first time the rule is applied.
Input and output may be either a single string (with lines separated by any
character that is not #
or .
) or an array/list/etc. If your language
supports it, you may also take input with each line being a function argument.
You may assume the following about the input:
There will always be at least one "loop"—that is, a group of
#
characters that can be followed infinitely. (Otherwise every single tile would become a dead end.)This implies that the input will always be 2×2 or larger, since the smallest loop is:
## ##
(Which, incidentally, should be output with no change.)
All
#
characters will be connected. That is, if you were to perform a flood fill on any#
, all of them would be affected.
Since this is code-golf, the shortest code in bytes will win.
The example above and the tiny 2×2 grid can be used as test cases (there aren't a lot of edge cases to cover in this challenge).