Having rescued the crew and passengers of StarShip Titanic II, you sit down with a fellow apprentice from that ship to chat and play a few computer games. You end up challenging each other on the classic arcade game Snake.
Snake involves moving a character across a screen grid to eat the fruit that appears. Every time the snake eats a piece of fruit, the snake grows 1 piece longer. If the snake moves outside the boundaries of the game board, or crosses over itself, the game is over.
Your game of snake is based on the following rules. What was your score?
The following example operates on a reduced size game board of 8x8 and the input data of:
fruit
3,3 2,5 7,7 6,0
moves
DDDRRRDDLLLDRRRRRRRDD
At the start of the game, the snake is at 0,0 and the first piece of fruit is at 3,3 so the board would resemble
S.......
........
........
...F....
........
........
........
........
#1: Move down. Score updated to 1
........
S.......
........
...F....
........
........
........
........
#2: Move down. Score updated to 2
........
........
S.......
...F....
........
........
........
........
#3: Move down. Score updated to 3
........
........
........
S..F....
........
........
........
........
#4: Move right. Score updated to 4
........
........
........
.S.F....
........
........
........
........
#5: Move right. Score updated to 5
........
........
........
..SF....
........
........
........
........
#6: Move right. Score updated to 106 (1 point for the move, 100 for eating fruit). The next piece of fruit is placed onto the board.
........
........
........
..SS....
........
..F.....
........
........
#7: Move down. Score updated to 107
........
........
........
...S....
...S....
..F.....
........
........
#8: Move down. Score updated to 108
........
........
........
........
...S....
..FS....
........
........
#9: Move left. Score updated to 209. Next piece of fruit is placed onto the board.
........
........
........
........
...S....
..SS....
........
.......F
#10: Move left. Score updated to 210
........
........
........
........
........
.SSS....
........
.......F
#11: Move left. Score updated to 211
........
........
........
........
........
SSS.....
........
.......F
#12: Move down. Score updated to 212
........
........
........
........
........
SS......
S.......
.......F
#13: Move right. Score updated to 213
........
........
........
........
........
S.......
SS......
.......F
#14: Move right. Score updated to 214
........
........
........
........
........
........
SSS.....
.......F
#15: Move right. Score updated to 215
........
........
........
........
........
........
.SSS....
.......F
#16: Move right. Score updated to 216
........
........
........
........
........
........
..SSS...
.......F
#17: Move right. Score updated to 217
........
........
........
........
........
........
...SSS..
.......F
#18: Move right. Score updated to 218
........
........
........
........
........
........
....SSS.
.......F
#19: Move right. Score updated to 219
........
........
........
........
........
........
.....SSS
.......F
#20: Move down. Score updated to 320. Next piece of fruit is placed onto the board.
......F.
........
........
........
........
........
.....SSS
.......S
#21: Move down. OUT OF BOUNDS.
Therefore the final score in this example was 320.
Process the snake game recorded in the input data. What was the score?
Hint #1
If you are getting a score of 6124, it means you are playing all the moves until the end of the dataset. Double check your code that tests for:
Hint #2
After move #50, the score would be 250 and the board should resemble the following:
....................
....................
.....F..............
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
..........S.........
.........SS.........
....................
....................
After move #100, the score will be 500 and the board will resemble:
....................
....................
....................
....................
..F.................
....................
....................
....................
....................
....................
....................
....................
................SSS.
..................S.
..................S.
....................
....................
....................
....................
....................