17
\$\begingroup\$

Introduction

Jonny wants to play Frogger. However, he's not very good. In fact, he will only try to move forward, and only after the platforms have moved.

Find out if Jonny's frog manages to reach the end of the path or if it dies on its way.

Challenge

The program will be receiving as input a Frogger grid composed by 0s and 1s, with the following format:

  • The grid will be of a random width and length, and at least 3x3
  • 1 represents platforms
  • 0 represents water
  • F represents the starting position of the frog
  • Every first and last line of the grid will be composed by only 1s, and will not move, and the frog F will be placed at random on the last line
  • Every intermediate layer will always be moving, and will have a < or > at the end of every line indicating if it moves left or right

Replacing these symbols with your own is allowed, as long as they're all distinct and you specify the replacements in your answer.

The input may be in any compatible format (string with line breaks, array of strings, array of characters, ...).

Challenge Rules

  • Every turn, all platforms will move one square, based on the direction indicated by the < or > sign
  • Platforms reappear on the other side of the grid if they get pushed off "screen"
  • If the frog is on a moving platform, it will move along with it
  • After that, the frog will jump one square towards the top row. The frog will move every turn.
  • The frog dies if it jumps in water (0) or it touches the side of the grid along with a moving platform

Your program has to output a truthy value if the frog survives and a falsy value otherwise.

This is , so the shortest answer in bytes win. Standard loopholes apply.

Examples

Example 1

Input

11111
00111>
00101<
1F111

Output

1

Execution

Turn 1:

11111
10011
01010
1F111

11111
10011
0F010
11111

Turn 2:

11111
11001
F0100
11111

11111
F1001
10100
11111

Turn 3:

11111
1F100
01001
11111

1F111
11100
01001
11111

Example 2

Input

11111
00100<
00100<
1F111

Output

0

Execution

Turn 1:

11111
01000
01000
1F111

11111
01000
0F000
11111

Turn 2:

11111
10000
F0000
11111

11111
F0000
10000
11111

Turn 3:

11111
00001
00001
11111
\$\endgroup\$
  • \$\begingroup\$ Will intermediate lines always be moving? Can we take a list of lines as input? If a line is not moving can we assume it ends with a character other than < or > so we can take rectangular arrays as input? By the way, nice challenge! \$\endgroup\$ – dylnan Jun 18 '18 at 15:20
  • \$\begingroup\$ @dylnan I clarified it in the challenge text. Intermediate layers will always be moving and will always have a < or > at the end. \$\endgroup\$ – BgrWorker Jun 18 '18 at 15:23
  • \$\begingroup\$ Does the frog move forward every turn even when there is a 0 in front of it, or will it wait for the next 1? If it can wait, will it go forward on every 1, or can it wait smartly? I.e. with the test case 11111 00001< 00011< 11F11, will it be falsey because it jumps in the water (pastebin of steps); will it be falsey because it moves out of frame (pastebin of steps); or will it be truthy because it waits smartly for the second platform before jumping forward (pastebin of steps)? \$\endgroup\$ – Kevin Cruijssen Jun 19 '18 at 7:11
  • \$\begingroup\$ @KevinCruijssen it moves every turn, and will happily suicide (as I said, Jonny is not a very good player) \$\endgroup\$ – BgrWorker Jun 19 '18 at 7:15
  • \$\begingroup\$ @BgrWorker Ok, that makes the challenge indeed more doable. :) Maybe edit it in the challenge description, that the frog will move forward every turn, even if it would jump into a 0. \$\endgroup\$ – Kevin Cruijssen Jun 19 '18 at 7:19
4
\$\begingroup\$

Python 2, 168 165 152 145 137 129 bytes

s=input();x=s[-1].find('F');L=len(s[0]);i=k=1
for l in s[-2:0:-1]:d=('<'in l)%-2|1;k*=l[(x-d*i)%L]>'0'>-1<x+d<L;x+=d;i+=1
print k

Try it online!

Input format is a list of strings; characters having meanings as given in the problem statement.

Explanation:

i is the turn number (starting with Turn 1); x is the position of the frogger at the start of that turn.

The row the frogger is about to step onto is the string l (note that via the slicing, these come in bottom to top order). d=('<'in l)%-2|1 yields -1 or 1 depending on the direction the row is moving.

Since this is the ith turn, that row will have shifted from it's original position by i bytes; and so the character the frogger is about to jump onto is l[(x-d*i)%L] where L is the width of the row, so we want that character to be equal to '1'; i.e., >'0'.

In addition, we want to check that the frogger will not be shifted off the edge at the beginning of the next turn; that is the function of expression -1<x+d<L.

These conditions are chained (since '0'>-1 is always True); and if at any time the resulting expression is false, k will become (and then stay) 0.

In any case, we update the frogger's position x+=d and bump the row number; then lather, rinse, repeat.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 246 245 244 242 bytes

-3 byte thanks to Mr. Xcoder
-1 byte thanks to Jonathan Frech

m=input()
exec"""for i,r in enumerate(m):
 d=-int(min('1',r[-1]));q=r[d*2]
 if m[i+1:]:r=sum([r[d+1:d-1],[[q,' '][q<'L']]][::d-~d],[])+r[-1:]
 if'F'in r:j=r.index('F');r[j]='L';m[i-1][j]=min('F',m[i-1][j])
 m[i]=r
"""*~-len(m)
print'F'in m[0]

Try it online!

Explanation

  • d is the direction each layer will move
  • q is the character that will be wrapped around
    • [q,' '][q<'L'] will drop the frog off-screen
  • sum([r[d+1:d-1],[[q,' '][q<'L']]][::d-~d],[])+r[-1:] will remove the last character (the direction), then remove the first character and append it or remove second last and prepend it (based on d), and append the direction back, effectively moving the entire row to the left/right.
  • if'F'in r:j=r.index('F');r[j]='L';m[i-1][j]=min('F',m[i-1][j]) will make the frog jump forward
  • min('F',m[i-1][j]) will make the frog fall in the water
  • The characters comparison (min and <) follow the order ' ' < '0' < '1' < 'F' < 'L'

The input will be a list of list of characters:
' ' - water
'F' - frog
'L' - plataform
'0' - move the layer to the left
'1' - move the layer to the right

\$\endgroup\$
  • \$\begingroup\$ if i<len(m)-1 could possibly be if~-len(m)>i. \$\endgroup\$ – Jonathan Frech Jun 18 '18 at 19:50
0
\$\begingroup\$

Java 8, 293 277 bytes

a->{for(int l=a.length-1,x=a[l].indexOf('F'),y=l,i,t=a[0].length()-1,b;y>0;y--){for(i=l;i-->1;a[i]=a[i].endsWith("<")?a[i].substring(1,t+1)+a[i].charAt(0*(x-=b))+"<":a[i].charAt(t)+a[i].substring(0*(x+=b),t)+">")b=i==y?1:0;if(x<0|x>t||a[y].charAt(x)<49)return 0>1;}return 1>0;}

Uses the default characters as specified in the challenge description (01F<>).

Try it online.

Explanation:

a->{                         // Method with String-array parameter & boolean return-type
  for(int l=a.length-1,      //  Amount of rows minus 1
       x=a[l].indexOf('F'),  //  Start x-position of the frog
       y=l,                  //  Start y-position of the frog
       i,                    //  Index-integer
       t=a[0].length()-1,    //  Length of the rows minus 1
       b;                    //  Temp integer
       y>0;                  //  Loop as long as the frog hasn't reached the other side
       y--){                 //    Jump forward once after every turn
    for(i=l;l-->1;           //   Inner loop over all the moving rows
        ;a[i]=               //     After every iteration: Change the moving row to:
         a[i].endsWith("<")? //      If the current platform moves to the left:
          a[i].substring(1,t+1)
                             //       Last part of the platform
                             //        i.e. "00101<" → "0101"
          +a[i].charAt(0     //       Appended with the first character
                             //        i.e. "00101<" → '0'
            *(x-=b))         //       We are moving left, so subtract `b` from `x`      
          +"<"               //       And append the direction "<" again
                             //        so "00101<" becomes "01010<"
         :                   //      Else (the platform moves to the right):
          a[i].charAt(t)     //       Take the last character
                             //        i.e. "00111>" → '1'
          +a[i].substring(0  //       And append the first part of the platform
                             //        i.e. "00111>" → "0011"
            *(x+=b),t)       //       We are moving right, so add `b` to `x`
          +">")              //       And append the direction "<" again
                             //        so "00111>" becomes "10011>"
      b=i==y?                //    If the frog is on the current row:
         1                   //     Set `b` to 1
        :                    //    Else:
         0;                  //     Set `b` to 0
    if(x<0|x>t               //   If the Frog is out of bounds
       ||a[y].charAt(x)<49)  //   Or jumped into the water
      return 0>1;}           //    Return false
  return 1>0;}               //  If the loop ended the frog made it to the other side,
                             //  so return true
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.