Challenge
You are given an ASCII-art representation of characters on a plane as input by any reasonable method. This will only contain:
[a-z]
representing moveable characters. Every letter will appear on the board at most once.#
representing immovable walls.
representing empty space
For example:
abcdef.gh#..
.......ij.#.
#..#.......#
...#.#...###
.#.......#q#
.........###
You are also given a string representing the changes in gravity. This will only contain:
>
representing a change to rightward gravity<
representing a change to leftward gravity^
representing a change to upward gravityv
representing a change to downward gravity
For example:
v>^
Your program must simulate the each change in gravity sequentially until all characters stop moving (they hit a wall, or another character). Characters that "fall off the edge of the map" are permanently removed, and characters can "stack" on top of each other.
In this example, at the start there is downward gravity (v
), so c
, e
, g
, h
, i
, and j
fall off the bottom of the map. All other characters slide downwards until hitting a wall, leaving the map like this:
.........#..
a..d......#.
#..#.f.....#
.b.#.#...###
.#.......#q#
.........###
Then, we move on to rightward gravity (>
), which leaves us with this:
Note how the a
stacks next to the d
.
.........#..
........ad#.
#..#......f#
..b#.#...###
.#.......#q#
.........###
Finally, we simulate upward gravity (^
), during which the a
and the b
fall off the map.
.........#..
.........d#.
#..#......f#
...#.#...###
.#.......#q#
.........###
Your task is to output the remaining characters after the gravitational shifts. They can be given in any order. For this example, you could output any permutation of dfq
.
Testcases
For the following map:
abcde
.....
##.##
v = abde
v> = <nothing>
For the following map:
######
#....#
abcdef
#.gh..
######
> = <nothing>
< = gh
^> = bcde
v< = bghef