15
2
You have been hired as a research assistant, and asked to create a small program that will build rat mazes. The rat box is always 62x22 and has an entrance (a) and exit (A) for the rat, like this (input 1):
#######a######################################################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#################################################A############
Your program must fill the box with blocks (#) leaving a path for the rat, like this (output 1):
#######a######################################################
####### ######################################################
####### ######################################################
####### ######################################################
####### ######################################################
####### ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
################################################# ############
#################################################A############
This is easy you think! You start writing a small program, full of confidence. However, the Principle Scientist has had a new idea -- he wants two rats to navigate the maze at the same time. Dr Rattanshnorter explains that they have different doors and different exits (input 2):
#b#####a######################################################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# B
# #
#################################################A############
The rats have been trained to move straight through cross intersections but T-intersections leave them hopelessly confused and will invalidate the experiment. You start on your new more complex task when the good Doctor explains one final requirement: the rats are savage to each other so if they see each other at any point, a rat fight will break out and you will both be before the ethics board. You now realise your program should output a maze something like this (output 2):
#b#####a######################################################
# ##### ######################################################
# ##### ######################################################
# ##### ####################################### ####
# ##### ####################################### ######### ####
# ##### ####### ####
# ############################################# # ####### ####
# ############################################# # ####### ####
# ############################################# # ####### ####
# ############################################# # ####### ####
# # ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### ####
################################################# ####### B
################################################# ############
#################################################A############
By the time rat B reaches the intersection, rat A will be travelling down the corridor to exit A and the rat fight will be avoided.
Rules:
Your program should read in (STDIN or file) an input like those above, and output (STDOUT or file) the same data except many spaces will now be hashes (#). You may substitute any single character (such as
;
) instead of\n
in the input string but the output string still requires\n
characters. UPDATEDA rat pathway must be one character width wide, except for cross intersections (every space must have zero or two orthogonally adjacent
#
characters). Each rat must have a clear single path, except for cross intersections. No T-intersections are allowed.Rats are released simultaneously and move at a constant rate. At no time should two or more rats see each other (be in the same column or row without one of more
#
characters in between).If no solution is possible (such as adjacent entrance points), print
Impossible\n
and exit.Entrances and exits can be on any sides, however they will never be on the corners.
If a matched entrance and exit are adjacent (eg:
##aA##
), the rat cannot go directly froma
toA
. There must be a small 2 space corridor section inside the maze area.On the turn where a rat reaches its exit point (or any time after that), it is no longer visible to other rats.
Your program may be designed to compute mazes for 1, 2, up to 26 rats.
Standard loopholes are disallowed.
Score:
With your solution, nominate how many rats per maze (N) your program can solve. Your score is your code length in bytes divided by this number N.
Please include a sample output in your answer so we can see what your program produces.
Is the only difference in the possible inputs the locations of a,A,b,B? – xnor – 2015-04-03T07:09:27.783
For the 2 rat version, yes. If your program is designed for up to 3 rats, you would need to cope with all possible locations of a,b,c,A,B,C. – Logic Knight – 2015-04-03T07:21:59.300
Are T intersections allowed if the rat will only walk alongside the horizontal part of the T? – orlp – 2015-04-03T07:38:44.317
No, these rats are easily confused. Only straight paths, elbow bends, and cross roads are allowed. – Logic Knight – 2015-04-03T07:49:03.507
@CarpetPython Can an entrance/exit be anywhere along the edge of the maze? Can they be adjacent? – orlp – 2015-04-03T07:50:07.943
As mentioned in the rules above, they may be adjacent and therefore that input is not solvable. Entrance/exits will never be on the corners though. – Logic Knight – 2015-04-03T08:11:21.187
I meant adjacent entrance and exit, like this: https://gist.github.com/orlp/f986056d28eb0a4a3a9e
– orlp – 2015-04-03T08:16:47.393Oh, I get it. It is a valid input to have 'aA' adjacent like that, and is trivial to solve. An input with 'ab' together is valid but impossible to solve (so output
Impossible
). – Logic Knight – 2015-04-03T08:24:39.247@CarpetPython Can open space be seen a series of cross intersections? Example: https://gist.github.com/orlp/f986056d28eb0a4a3a9e
– orlp – 2015-04-03T08:43:15.787Please note the updated rule 2 above. Rat paths must be single character width. – Logic Knight – 2015-04-03T08:46:07.183
@CarpetPython It doesn't say that. It says there must be zero or 2 orthogonal characters - a cross intersection for example is an invalidation of 'single character width'. – orlp – 2015-04-03T08:47:46.747
Mmmm. I will try to make that rule more clear. – Logic Knight – 2015-04-03T08:51:02.873
@CarpetPython Can rats move along other rats' exits and ignore them? Example. Or are those unsolvable? What about cases where there are 3 adjacent exits?
– orlp – 2015-04-03T13:03:38.370Yes, rats will only use their own exit, and ignore others. Your example solution looks ok. Three adjacent exits should be ok in principle, but I can't see how it could still follow the 2 or 0
#
per space rule. – Logic Knight – 2015-04-03T13:45:54.057Can I output a maze with sparse walls? http://pastebin.com/hRxud3cX for example, no rat ever encounters a three-way junction; its just that most of the spaces they pass through are four-way junctions.
– AJMansfield – 2015-04-03T15:01:47.060@AJMansfield, please rule 2 above: "A rat pathway must be one character width wide, except for cross intersections (every space must have zero or two orthogonally adjacent # characters). Each rat must have a clear single path, except for cross intersections.". This would mean that sparse layouts are not valid solutions. – Logic Knight – 2015-04-03T16:25:02.910
@CarpetPython I would interpret that wording to mean that they are valid, because every rat's path is one character wide, except at cross intersections, because most of the rat's path is through cross intersections. If that is your intent, you need to reword it to express that idea. Something like "any space that is not used by a rat as a path must have a wall in it", although that is a but cumbersome. – AJMansfield – 2015-04-03T16:33:25.350
@CarpetPython But I strongly disagree that such solutions should be invalid anyway. I think that sparse solutions should be allowed. Deriving a non-sparse solution from a sparse one would be a trivial operation, but would mean that programs that arrive at their solutions by placing things in a sparse arrangement would need to include additional code to thicken the solution. – AJMansfield – 2015-04-03T16:40:30.697
I can see your point AJ, but I decided on the corridor requirement early when I designed this challenge. There are several good reasons to limit paths to corridors, not the least of which is visual validation. May I suggest a statement at the end like
for xy in grid: cell[xy]='#' if xy not in paths
to fill in non-path cells? I hope this limitation will not put you off this challenge. I look forward to seeing your solution. – Logic Knight – 2015-04-03T17:15:35.227@CarpetPython If the algorithm I'm playing with pans out the way I think it will, although it might not work at all... – AJMansfield – 2015-04-04T11:57:29.490
@CarpetPython If the entrance is adjacent to the exit, do you need any path at all, or can the rat go straight from the entrance to the exit?
##aA##
– Hjulle – 2015-04-05T10:49:44.710@Hjulle, good question. The rat will need some corridor. They cannot go from a to A directly. – Logic Knight – 2015-04-05T12:03:41.297
@CarpetPython Would
##aA##\n## ##\n######
be a valid solution then, or is it unsolveable? – Hjulle – 2015-04-05T12:05:38.100The only valid solution would be
##aA##\n##..##\n######
(note the 2 space corridor shown as..
here) as the "zero or 2 # per space" rule would block any other option. – Logic Knight – 2015-04-05T12:14:49.647@CarpetPython Can rats see other rats that have already exited from the maze? – Hjulle – 2015-04-06T15:14:38.693
No. Once a rat has reached its exit place, no other rats can see it (maybe it drops down a chute into its own cage). – Logic Knight – 2015-04-06T17:12:43.330
For javascript in a browser, stdin equivalent is usually 'prompt()', but it does not work for multiline strings. May I write a function with a string (multiline) parameter? – edc65 – 2015-04-07T08:53:05.697
If I add "you may substitute any single character (such as
;
) instead of\n
in the input string.", would this solve the problem? – Logic Knight – 2015-04-07T09:03:56.417Are you sure that this can be solved in polynomial time in general? My strategy is probably not the best, but just going 2 steps with 6 rats leads to 46656 possibilities. – Hjulle – 2015-04-08T00:58:28.990
I have made no promises or predictions on the algorithmic complexity of this puzzle. I tend to make my challenges complex enough (see questions in my profile) that brute force search is not practical. You usually need clever optimisations to make a good answer. – Logic Knight – 2015-04-08T05:09:09.157