Snake Collision
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
andD
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 tomove 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 ofo
: 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 directionR
(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 commandsU
,R
,L
andD
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 headcol
: Column index of the last location of snake's headN
: Number of steps leading up to and including the collision
Similar Kata:
Stats:
Created | Apr 5, 2018 |
Published | Apr 5, 2018 |
Warriors Trained | 592 |
Total Skips | 11 |
Total Code Submissions | 2621 |
Total Times Completed | 136 |
Python Completions | 93 |
JavaScript Completions | 50 |
Total Stars | 44 |
% of votes with a positive feedback rating | 88% of 52 |
Total "Very Satisfied" Votes | 41 |
Total "Somewhat Satisfied" Votes | 10 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 10 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |