35
6
Background
You are the apprentice of a powerful wizard, and your master is currently developing a spell for creating an inter-dimensional labyrinth to trap his enemies in. He wants you to program his steam-powered computer to analyze the possible layouts. Programming this diabolical machine is highly dangerous, so you'll want to keep the code as short as possible.
Input
Your input is a two-dimensional grid of periods .
and hashes #
, signifying empty space and walls, given as a newline-delimited string.
There will always be at least one .
and one #
, and you can decide whether there is a trailing newline or not.
This grid is the blueprint of an infinite labyrinth, which is made by aligning infinitely many copies of the grid next to each other. The labyrinth is divided into cavities, which are connected components of empty spaces (diagonally adjacent spaces are not connected). For example, the grid
##.####
...##..
#..#..#
####..#
##...##
results in the following labyrinth (continued infinitely in all directions):
##.######.######.####
...##.....##.....##..
#..#..##..#..##..#..#
####..#####..#####..#
##...####...####...##
##.######.######.####
...##.....##.....##..
#..#..##..#..##..#..#
####..#####..#####..#
##...####...####...##
##.######.######.####
...##.....##.....##..
#..#..##..#..##..#..#
####..#####..#####..#
##...####...####...##
This particular labyrinth contains a cavity of infinite area. On the other hand, this blueprint results in a labyrinth with only finite cavities:
##.####
##..###
####...
..####.
#..####
Output
Your output shall be a truthy value if the labyrinth contains an infinite cavity, and a falsy value if not. Note that the labyrinth may contain both finite and infinite cavities; in that case, the output shall be truthy.
Rules
You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.
Additional Test Cases
Infinite cavities:
.#
#.#
...
#.#
#.###.#.###.#
#.#...#...#.#
#.#.#####.#.#
..#.#...#.#..
###.#.#.#.###
#...#.#.#...#
#.###.#.###.#
##.###
#..###
..##..
###..#
##..##
..#..#..#..#..#..#
.#..#..#..#..#..#.
#..#..#..#..#..#..
#.####.###.###.####
#...#..#...###..###
###.#..#.######..##
....####.#######...
###..###...########
##########.##....##
..###......##.##...
#.........##..#####
###########..###..#
#...........####..#
#.###########.##..#
#.##....##.....####
#.####.###.###.####
Finite cavities:
###
#.#
###
.#
#.
####
.#..
####
#.#.#
..#..
#####
..#..
#.#.#
#.#.#.#.#.#
..#...#.#..
###.###.###
..#.#......
#.#.#######
#.#.......#
#.#######.#
#.#.....#.#
#.#.#.#.#.#
##....#####
.#..#...##.
.##.#..#...
..###.###..
#..##.#####
#...##....#
#.#.#####.#
###..####.#
....####...
###...#####
###....##.#########
####...##....#...##
..####.#######.###.
....##..........##.
###..#####.#..##...
####..#..#....#..##
..###.####.#.#..##.
..###...#....#.#...
..####..##.###...##
#.####.##..#####.##
####...##.#####..##
###########
........#..
#########.#
..........#
.##########
.#.........
##.########
...#.......
Is there a trailing newline character? – FUZxxl – 2015-03-30T16:06:13.840
@FUZxxl That's up to you. – Zgarb – 2015-03-30T16:07:41.067
Can the infinite labyrinth be a straight line that goes to infinity. – None – 2015-03-31T02:23:44.590
1@Neil I'm not sure what you mean. The first and second infinite examples have infinite lines, but there is at least one
.
and one#
in the input. – Zgarb – 2015-03-31T07:12:22.8071Nice challenge, more difficult than it seems – edc65 – 2015-03-31T23:34:00.730
Can we receive input as a matrix of characters instead of a string with newlines? – FUZxxl – 2015-03-31T23:51:56.530
I have an interesting solution that runs in O(n² m²) where n and m are the dimensions of the array. Let's see if it works. – FUZxxl – 2015-04-01T00:30:32.093
@FUZxxl In this challenge, it must be a newline-delimited string. Looking forward to your solution! – Zgarb – 2015-04-01T07:41:26.823