5 kyu

Snake Collision

93 of 136kodejuice

Description:

In this kata, you're required to simulate the movement of a snake in a given 2D field following a given set of moves. The simulation ends when either of the following conditions is satisfied:

  • All moves have been executed
  • Snake collides with the boundary or with itself.

Concepts:

  • Growth

    When the snake encounters a $ it will grow a segment at its tail, and $ will disappear from there.
    For example

     --------               --------
     --ooo$--       ===>    --oooo--
     --------               --------
    
  • Collision

    (Here tail is the last segment of the body) It should be considered that the snake moves successively, that is its head moves to the next cell, then the cell which followed head will move to the cell which was occupied by the head, and so on.

    Keeping this in mind, a collision can occur when:

    • Snake collides with the wall of the field
    • Snake collides with itself
    • Snake's head moves into a place which is occupied by its tail after the last move was made.

    Example:

    Here's a demonstrative example: H : head of the snake, T: the tail of the snake

    --------------
    ---Toooo------   If the snake is directed to move up, it would be considered
    ---H---o------   a collision
    ---o---o------
    ---ooooo------
    
  • Movement

    The snake will be directed by commands, which can either direct the snake to turns its head or to perform translational motion in the direction of its head.

    For controlling the direction of the head, there are 4 commands: U, R, L and D which stand for up, right, left, and down respectively, and translation motion which is quantized by steps, which is given as a whole number.

    Initially, the snake would be pointing in the right direction R.

    For example: 12 D 2 R 2 would translate to

    move 12 steps right
    turn head in the downward direction
    move 2 steps downward
    turn head in the rightward direction
    move 2 steps rightward
    

Task

Write a function snake_collision having

Input:

  • field: A list/array of strings, in which each string consists of

    • o: snake's cell
    • -: empty cell
    • $: snake's food

    Provided fields will abide by the following rules:

    • Height : 13, Width: 21
    • Initial size of snake would be always 3
    • Initially, snake would occupy: (0, 0), (0, 1), (0, 2), snake's head would be at (0, 2) and it would be facing in rightward direction R
      (Here the first element of tuple is the row index and the second element is the column index)

Example of a field:

[
     'ooo------$--$$$------',
     '-----$$$--$--$---$--$',
     '-$-----$--$-$---$----',
     '------$$$----$---$---',
     '--$$--$---------$$---',
     '-----$$---$$$--$$--$-',
     '----$-----$-$----$--$',
     '$-----$-$$---$$---$--',
     '$--------------------',
     '-------$---$------$-$',
     '----$-$$----------$$-',
     '--$-$$$--$-$--$-----$',
     '$------$--$----------'
]
  • moves: String which consists of whole numbers and commands U, R, L and D separated by spaces.

Output

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

Where:

  • row: Row index of the last location of snake's head
  • col: Column index of the last location of snake's head
  • N: Number of steps leading up to and including the collision
Arrays
Games
Algorithms
Simulation
Puzzles

Stats:

CreatedApr 5, 2018
PublishedApr 5, 2018
Warriors Trained592
Total Skips11
Total Code Submissions2621
Total Times Completed136
Python Completions93
JavaScript Completions50
Total Stars44
% of votes with a positive feedback rating88% of 52
Total "Very Satisfied" Votes41
Total "Somewhat Satisfied" Votes10
Total "Not Satisfied" Votes1
Total Rank Assessments10
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • kodejuice Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • ZED.CWT Avatar
  • Awesome A.D. Avatar
  • user9644768 Avatar
Ad