5 kyu

Snake Collision

93 of 136kodejuice
Description
Loading description...
Arrays
Games
Algorithms
Simulation
Puzzles
  • Please sign in or sign up to leave a comment.
  • ahmet_popaj Avatar

    Quite a classic, nice one.

  • dfhwze Avatar

    Even more seriously annoying issues:

    • sample tests show snake always size 3 in horizontal position at row 1
    • random tests have snakes of other sizes (against the spec)
    • random tests have snakes on other rows (this is surprising, given they are always on first row in sample tests)
    • random tests have snakes that are in vertical position, where the F is their head ??!
    • What are steps, is each move considered a step, are turns included, ..?
  • dfhwze Avatar

    "Initial size of snake would be always 3"

    Random test -> size = 5

    ooooo---$$-----------
    --$$-----------------
    -------$$--$--$---$--
    -$---$$---$$---$$-$--
    $---$$$-$-$---$------
    -------$--$--$----$$-
    $---$$$-------$$$$$--
    $-----$$$$----$----$$
    -$-$-$-----$---$----$
    --------$------------
    $--$$--$$$---$-$-$---
    -----$--$----$----$--
    ----$---$--$$----$-$-
    
  • EmilieCote Avatar

    The recently included test case that verifies food is only consumed once is causing a contradiction with other test cases.

    Some tests assume food remains in the field after being eaten so this Kata is no longer solvable.

    Example:

    moves = '7 D 7 R 3 U 1 L 7 U 6 L 1 U 5 L 6 D 3 L 8 U 6 L 2 U 3 R 1 D 5 R 5 D 4 R 4 U 7 R 2 D 4 R 7 U 8 R 4 D'

         field = ['ooo-$----$-$--$----$-',
                  '--$-------$-$$------$',
                  '$----$$-$--$-$-$--$--',
                  '$---$--$-----$-------',
                  '-$$$---$----$$$--$---',
                  '$--$$---$----$$-$-$--',
                  '-$--$$$--$$--$-$-----',
                  '$---$------$---------',
                  '-----$-------$-$$--$$',
                  '-$$-$---$-$-$$$--$-$-',
                  '-$$----$-$$----$-$$-$',
                  '$--$-$---$-$-----$-$$',
                  '-$$$---$-----$$$----$']
                  
                  
    

    The test says ((0, 4), 33) should equal ((6, 10), 21) but this could only be true if the snake ate the same food twice and was long enough to collide with itself.

    I've found a few other scenarios like this.

  • LaurentBouvier Avatar

    Hello, I think there is a anomaly on the test case

    ooo$$$$$$$$$$$$$$$$$$ --------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $-------------------$ $$$$$$$$$$$$$$$$$$$$$ 18 D 12 L 20 U 12 R 5

    Indeed, the snake can turn around and grow as we have one space on the border, the head is never on the tail. it should be OK. Why to expect ((0, 5), 67) and not ((0, 5), 0) as all moves were successful?

  • Sburly Avatar

    Are we just gonna accept this is a 5kyu kata?

  • monstaber Avatar

    Description asks for head position and N: Number of steps leading up to and including the collision

    There is a test case with these moves given: U R L D U R L D

    The first move leads to a collision against the top border of the board. However the test expects [[0, 2], 0] rather than [[0, 2], 1]

    Am I missing something or is this wrong?

  • Kees de Vreugd Avatar

    This looks like an awful lot of work for a 5kyu.

  • mauro-1 Avatar

    Description doesn't say if food can be used multiple times or only once.

    Both versions pass the tests (at least in python).

  • JohanWiltink Avatar

    You are to return the output in the format: ((row, col), N).

    What do we return if there is no collision when all moves have been executed?

    The description should specify this.

  • arhimedoffs Avatar

    Are width and height values mixed up in details? Or sample represented column wise?

  • RealKenshiro Avatar

    Nice Kata!

    May I suggest a small helper function for debugging purposes :

    def show_grid(arr, occ):
        a = [[arr[i][j] for j in range(len(arr[0]))] for i in range(len(arr))]
        for x,y in occ:
            a[x][y]='o'
        print('\n'.join(''.join(u) for u in a)+'\n')
    

    arr is the field array (only '$' and '-') and occ the positions occupied by the snake.

  • kodejuice Avatar

    I sincerely apologize for the very horrible description, this was 3years ago, and i had just started creating katas at that time. I didn't really put in much effort into the correctness of the problem description and test cases, i apologize for that. Thanks @XRFXLP for the fixes done.

  • Awesome A.D. Avatar

    Are there any tests that check whether food is consumed twice? Because my code does not even check for that and it passes.

  • FArekkusu Avatar

    No tests with no moves.

  • FArekkusu Avatar

    If no collision occurs, tests expect the one-before-last head position instead of the last one.

    The issue is back.

  • FArekkusu Avatar

    No tests with no moves.

  • FArekkusu Avatar

    If no collision occurs, tests expect the one-before-last head position instead of the last one.

  • FArekkusu Avatar

    the snake moves in sequential manner

    The explanations should be rewritten in terms of body segments instead of the snake as a whole where relevant. "Tail" could mean both the last segment of its body, and every segment excluding the first one.

  • FArekkusu Avatar

    X, Y = cartesian coordinates of the cell where a collision ocurred

    Incase of collision, return the coordinate of head before the collision

    Can we have a consistent description, please?

  • dolamroth Avatar

    Random tests should include grids of different sizes and various densities of food.

  • dolamroth Avatar

    I would suggest to make it more clear, that if snake moved into the cell, where the end of its tail was, it is considered collision. Because in some realizations of "snake" game it is considered a valid move.

    With each step the snake moves its head to the adjacent cell according to current direction, and then draws its tail from its last position (so after the 1-st step the cell (0,0) becomes empty etc.)

    change to

    With each step the snake firstly moves its head to the adjacent cell according to current direction, and after that draws its tail from its last position (so after the 1-st step the cell (0,0) becomes empty etc). If snake moves into the cell, where its tail just is, it is considered collision.

    P.S. It appears, that FArekkusu has already made issue about that. I will leave this suggestion with an improved description.

  • FArekkusu Avatar

    X, Y = cartesian coordinates of the cell where a collision ocurred

    Collision occurs on the boundary of 2 cells, not in one particular cell, so this could be interpreted as either "the last valid", or "the new invalid" position of the head.

    What's worse, the 2 different collision cases correspond to different interpretations - ideally, this should be made consistent too (at least if kazk decides to throw this kata back into beta).

  • FArekkusu Avatar

    The third function parameter should be removed from the initial solution in JavaScript.

  • FArekkusu Avatar

    Node 12 should be enabled.

  • FArekkusu Avatar

    New test framework should be used in Python.

  • FArekkusu Avatar

    It is not specified whether moving to the position of the last segment of the body results in a collision or not.

  • dolamroth Avatar
    1. Returning multiple output types is a bad practice. Instead, one could always return X, Y, N, with N == -1 if there is no collision. (X,Y) would be valid coordinates in either case.
    2. In Python the output with fixed number of elements should be a tuple, not a list.

    I'm not sure if I should make it a suggestion or an issue, but let it be issue for starters.

  • FArekkusu Avatar

    if the snake comes in contact with this, it increases in size

    How?

  • B1ts Avatar

    JS random tests: ReferenceError: field is not defined at Test.it._

  • Blind4Basics Avatar

    Careful with the expressions you use:

    if you are in cartesian coordinates, y is ascending, and since you talk about the celle(0,0) being the top left corner, the origin of your axes is there. But your "y"s are descending, currently. So just remove the "cartesian" and keep the reamining of the sentence.

  • ZED.CWT Avatar

    This comment has been hidden.

  • Blind4Basics Avatar

    i used Test.expect(false) on purpose, its meant to break the tests and out that error message "Value is not what ...", i printed the field after the moves have been done, isnt that enough for you ?

    no it's not because, since the snake makes a loop, the display is fully useless and one would have to follow each step by hand to MAYBE get the expected solution, which is unlikely in some edge cases because you do not provide the full/correct set of rules. So, yes, it is an issue.

    And even provding the full set of rules, what is the point of not giving the expected result? Except the will to annoy the user???

  • Blind4Basics Avatar

    not enough fixed tests: you should have at least one of each collision case, one with and one without turn before hitting a border, one with a flip of direction (U 2 D 3) and things like that.

  • Blind4Basics Avatar

    @ZED: your ranking is nuts...

  • Blind4Basics Avatar
    Your result ( [15, -1, 20] ) is wrong
    Value is not what was expected
    

    LOL!

    Seriously!? How many times did we already told you to NOT use test.expect unless you provide a useful assertion message...? X-/

    And that leads to another problem: You tell in the description: X, Y = coordinates of the cell where a collision ocurred. But isn't it actually the position of the head right BEFORE the collision, then? If not, there will be an inconsistency between collision on borders and collisions with the snake itself.

  • Blind4Basics Avatar

    Looks a very interesting one! Though the description is lacking some information:

    • when the sanke is growing, is it growing from the head or from the tail? (meaing, do we have to put the extra piece in front of the head or behind the tail?)
    • is the number of turns 0 indexed or 1 indexed?