The -r flag puts a list of the lines of input into g. The output after the last column is a non-empty list if the player can win, or an empty list (which prints as just a trailing newline) if the player loses.īuilt-in variables used in this program: i = 0, l =, s = " ". We transpose the input and loop over the lines (columns), keeping a list of the y-positions the player could reach in that column. The strategy is essentially a breadth-first search. Uses the -r flag to read all lines of stdin. These 2 bytes sneakilyĬome from the -p option for the and the -e option $_=m w\n Check if the last column contains a w: He made it! &redo Keep looping as long as w's keep getting added This is the 3 regexes shown above combined. Will be the regex that goes one step down a column \n/ Match the first newline (needed to measure the $n effectively becomes rowlength-1 times ".". With that out of the way the program is easy: #!/usr/bin/perl -0p Slurp all of STDIN into $_, at the end print $_ Starting at the earliest possible character, so Is not needed for gravity up because regex always matches So for gravityĭown we must handle w as if it is still a space. W but the gravity up regex has already put a w directlyĪfter the w gravity guy takes off from. Position x is currently a space and must be replaced by a There as long as it is over spaces (or w) you finallyĮnd up on a space directly above a #. w($n)*\K $n#/ A w followed by a space (or w) and if you go down from Position gravity guy ends up on if starting from that The only way to get a word boundary is if Over spaces) you get to a space that follows a wordīoundary. Go down further from there (as as the descent is #$n\K( $n)*\b / A space immediately below a # and if you (optionally) This puts gravity guy on his starting platform Then the relevant regexes are: /^( $n)*\K $n#/ From the first position drop down as long as youĮncounter spaces until you encounter a #. Which will demand that the space is preceded by prefix and followed by postfix but replace only the space by w without needing lots of advanced grouping.Īssume $n contains a regex that will progress just enough that the left and right sides are exactly below each other. The string will be constructed by replacing one reachable space by w in each round.Įach replacement will be of the form s/prefix \K space postfix/ w | $& /e So gravity guy can make it if and only if there is a w in the last column. So for the second to last truthy example it will construct: # This will construct a string with a w in each positions gravity guy can reach. But the commandline based version doesn't need that extra newline so this counts as 74 bytes: perl -0pe $n)*\b |(^|w)($n)*\K $n#/w|$&/es&redo $_=m w' < gravity.txt This file based version needs the final newline, so that is really 75 bytes. Run with the input pattern (with all lines space padded to the same length) on STDIN: < gravity.txt
![gravity guy 1 player gravity guy 1 player](https://games.macrojuegos.com/11000/11705/thumb_230x144.jpg)
(each case separated by -, any blank lines should also be padded with spaces) True #
#Gravity guy 1 player code
This is code-golf, may the shortest code in bytes win!.The width and height of the grid will be 50 characters at most.If it is possible for the player to win the level, output a truthy value.Input grid can be in any appropriate "grid" format (multiline string padded with spaces, array of line strings, array of array of characters, etc).# | # | # | # | # | #Īs you can see, by switching gravity at these times Gravity Guy reaches the end, so this input would return a truthy value. ^ = switch gravity to up and v = switch gravity to down. Gravity Guy would start at the x and the be at these positions after each iteration. If Gravity Guy moves off the right side of the input grid, the player wins.If there is no # to fall on to and he falls off the grid, the player loses.
![gravity guy 1 player gravity guy 1 player](https://bestwindows8apps.s3.amazonaws.com/cache/00/13/00137faad353a5932847487c715b4034.png)
![gravity guy 1 player gravity guy 1 player](https://images.sftcdn.net/images/t_app-cover-l,f_auto/p/cfcad730-68b0-11e8-be9f-21532cebd7b7/3236884210/gravity-guy-screenshot.png)