24
2
It is Halloween and Jimmy (/o\
) has gone into a mysterious neighborhood for trick-or-treating (ask himself why). Now some evil ghosts are chasing him. Can Jimmy escape the ghosts?
Challenge:
Input:
A board showing position of Jimmy, ghosts, solid objects and empty spaces.
An example 10x5 board, o
is Jimmy (we needed a single character Jimmy), g
s are ghosts, #
s are solid objects and .
s are empty spaces:
##########
......g...
#.o.......
#.....g...
##########
Output:
A truthy value if Jimmy can escape at least in one scenario, otherwise a falsy value.
How things work:
Ghosts and Jimmy can move one space each turn. There are 4 possible movements: left, right, up, down (no diagonal movements, no skipping).
On each turn, first ghosts move one space towards Jimmy. Ghosts always select the closest path to Jimmy, if two possible moves towards Jimmy have the same cost, the move going to left or right is chosen. Ghosts can go through solid objects, meaning at the end of a turn a ghost can be on same space as a solid object. Multiple ghosts can stay on same space too. This basically means all the ghosts on the same space will move exactly like each other after that point (they are all like a single ghost now).
After all ghosts have moved, if any of the ghosts ends up on the same space as Jimmy, Jimmy is caught and you have to try another scenario.
If Jimmy is not caught, he moves next. You have to decide on Jimmy's moves. Jimmy cannot go through solid objects. If Jimmy moves to a space with a ghost, he is caught. Jimmy cannot move into a space he has already visited. If Jimmy has no valid moves, consider him caught.
After Jimmy has moved, if he ends up on an empty border space (i.e. a space on the edge of the grid without a ghost on it), he has escaped.
If you can find at least one scenario where Jimmy can escape, output a truthy value. Also if the input position before doing any moves is already an escape position, it is valid and you should return truthy. If Jimmy is caught or out of valid moves in all possible scenarios, then return a falsy value.
Challenge rules:
- You can use any consistent characters that you like for Jimmy, ghosts, solid objects and empty spaces in the input. You cannot use same character for different types of items though.
- The input board is always a rectangle or square with a minimum size of 2x2 and a maximum size of 10x10.
- Input is flexible as long as you don't include anything more than initial board information (for example, you cannot include if the initial board is already an escape position or not). It can be a multi-line string, a list of strings, a 2d array or matrix of characters or numbers, etc. It can also be position of items, for example a
(x, y)
for Jimmy and two lists of(x, y)
s for ghosts and solid objects. You can also pass board size as a separate input if you like. Basically any format and combinations of inputs that don't include more than the initial board information are allowed. - Input is guaranteed to always have a single Jimmy.
- Input always has at least one ghost.
- Input always has at least one solid object.
- Input can never start in a position where more than one item is on same space.
- Input can start in a position which already is an escaped position for Jimmy (any input with Jimmy on a border space), you should return truthy for cases like this.
General rules:
- This is code-golf, so shortest answer in bytes for every language wins. Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
- Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Truthy:
Jimmy can move left, up, left to escape:
##########
......g...
#.o.......
#.....g...
##########
Jimmy is already on a border space, this is an escape:
#.
og
If Jimmy moves only left or only right, he escapes:
#########
.........
....g....
...g.g...
....o....
...g.g...
....g....
.........
Jimmy has only two ways, one of them is an escape:
##########
#g.o.....#
########.#
#........#
#.########
#........#
########.#
#........#
#.########
Falsy:
Jimmy is caught by first move of any of the ghosts:
.....
..g..
.go..
.....
.....
Jimmy has no valid moves:
###.......
#o#......g
###.......
Jimmy will run out of valid moves before ghost can catch him:
#####.....
#o..#....g
#####.....
Remember, ghosts can go through solid objects:
g#.#
.#.#
.#.#
.#o#
.#.#
.#.#
g#.#
Oh poor Jimmy! It is a dead end...
##########
#...o....g
##########
Jimmy!!? How did you get there in the first place?
#.########
#g.......#
#....g...#
#......g.#
#..g.....#
#....g...#
#........#
#....g...#
#..g....o#
##########
3Jimmy will run out of valid moves before ghost can caught him. – None – 2019-10-28T08:59:36.300
2@A_ fixed, sorry for my poor English. – Night2 – 2019-10-28T09:02:05.393
2Is the rule "Jimmy cannot move into a space he has already visited" necessarily? Could anyone find an input which may yield different result when this rule is removed? – tsh – 2019-10-28T09:58:19.893
3@tsh I believe it makes a difference. Without that rule, Jimmy can go left and right infinitely against a single ghost without escaping or getting caught. I put that rule in place to prevent infinite loops or very long scenarios that would make TIO not work for them. – Night2 – 2019-10-28T10:06:23.997
2
@tsh example of infinite loop: https://i.imgur.com/66ZlURr.gif
– Night2 – 2019-10-28T10:27:04.297Second truthy case looks wrong. You say the ghost moves first, so the ghost will catch Jimmy before he can even move. Otherwise, please update the rules. – Level River St – 2019-10-30T20:51:56.827
How flexible is the input? Ideally, I'd like to take a integer position
(x,y)
for jimmy, a list of initial ghost integer positions[(g1x,g1y), ...]
and then a 2-D matrix of integers0/1
representation of the maze and its walls. – Chas Brown – 2019-10-31T00:08:30.8101@LevelRiverSt: It is already specified twice: "Also if the input position before doing any moves is already an escape position, it is valid and you should return truthy." and "Input can start in a position which already is an escaped position for Jimmy (any input with Jimmy on a border space), you should return truthy for cases like this." – Night2 – 2019-10-31T01:17:52.820
@ChasBrown input is flexible, you can use what you mentioned. Added an item under "Challenge rules:" section to clarify it more. – Night2 – 2019-10-31T01:38:57.040