Your program receives the level below(without line breaks) via standard input.
It then has to print out a commands which are needed to successfully complete the level.
All levels are 4 lines height, with each of the lines 63 chars wide. That makes a total of 252 characters per level.
> ______ ____ ________ ___ <
> C ______| | | | C __| | | | ____E<
>S______ __ ____| | | |_____| |__| |___| <
> xx xx xx <
Notes: The >< are just to illustrate the borders, they are NOT included in the input to your program. Also watch out for your text editor, as mine screwed up the whitespace a number of times
Successive commands are stacked on top of each other.
F indicates where you will fall(remember you can't do anything while falling),
MMMF MMMF
M MMMMMMJ MMMMF M MMMMMMMMJ MMMF
M J MMMFMMMF MMMMMMJ| | | |F J MMJ| | | |F MMMMME
SMMMJMJ MJ MMMJ| | | |MMJMJ| |__| |MMJ|
xx xx xx
Resulting command sequence, 75 characters long:
MMMMJJMMJMMMMJMMMMMMJMMMMMMJMMMMMMJMMMMMMMMMJJMMJMMJMMMMMMMMJMMMMMMMMJMMMMM
Hope this yields some interesting results... and not tons of flames :O
OK, there are way more possibilities than I initially thought of, I apologize for all the edits.
Javascript:
Short Version (334 280 256 240 238 236 233 223 207 205 196 184 182 characters)
a=prompt();j=i=0;while(a[++j*63]<(o="M"));while(++i<62){while(a[h=j*63+i]<"_")j++;if(a[h-63]>"B")o+="JJ";if(a[h+1]>"z")o+="J",j--;if(a[h+3]+a[h+1]=="_ ")o+="JMM",i+=2;o+="M"}alert(o)
Note: The Javascript method prompt tends to remove space on some browser (ex.: Google Chrome). It might not work as expected for that reason for those browsers. On others (ex.: Firefox), it will work fine.
Commented Version
a=prompt(); // Read the input //
j=i=0;
while(a[++j*63]<(o="M")); // Place the cursor at the "S" //
while(++i<62){ // While we are not at the end point //
while(a[h=j*63+i]<"_")j++; // If we are on a space, we fall //
if(a[h-63]>"B")o+="JJ";// We jump for coins //
if(a[h+1]>"z")o+="J",j--; // We jump when we reach a wall //
if(a[h+3]+a[h+1]=="_ ")o+="JMM",i+=2; // We jump on gap //
o+="M" // We add the movemment in the output
}
alert(o) // Output
Basically the same approach as HoLyVieR (I wonder if there can be lots of radically different directions solution-wise). Reads from stdin.
Update 1 (318 -> 302): don't check for E
but assume it is on position 63 (as asked in a comment).
Update 2 (302 -> 300): changed range(0,252,63)
to (0,63,126,189)
(two whole characters)
Update 3 (300 -> 284): seems raw_input
also fetches stdin
so import sys
etc can be dropped.
Update 4 (284 -> 277): [y][x+3]=="_"and p[y][x+1]==" "
to p[y][x:x+4]==list("_ _")
Update 5 (277 -> 206): went for string instead of 2-dimensional list processing, big save...
Update 6 (206 -> 203): implemented suggestions in a comment on HoLyVieR's answer (by Nabb)
Update 7 (203 -> 191): breaking the 200 char-limit by using boolean string building...
Update 8 (191 -> 184): minor tweaks
All comments or suggestions welcome!
Note: I added an (unneeded) \
and newline
in the code below (EOL 5->6) (to avoid scrollbars here)
l=raw_input()
x,y,o=0,l.index('S')//63,''
while x<62:
while l[y*63+x]==" ":y+=1
b=y*63+x;g=l[b+1]>"z";h=l[b:b+4]=="_ _";o+=(l[b-63]>"A")*"JJ"+g*"J"+h*"JMM"+\
"M";y-=g;x+=1+h*2
print o
Usage: python 2dplatform.py < level.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With