On the advice of Ms. Pac-Man who's worried about him getting overweight, Pac-Man has decided to keep track of his daily Pac-Dot intake. Help him count the number of Pac-Dots on a given path in the maze!
To help you build your own encoding of the maze, you can get some raw data here.
In the context of this challenge, the following rules apply:
- First, the good news: the ghosts aren't there.
- Pac-Man always starts his race at the position indicated on the above picture, heading to the East. There is no Pac-Dot at the starting position.
- As long as he's following a straight path, he keeps advancing to the next squares.
- When he encounters a 90° turn without any other available path (orange squares on the map), he automatically and systematically takes the turn.
- When he encounters a junction where several paths are available (green squares on the map), he may either continue on the same direction -- if applicable -- or choose another direction (including doing a U-turn).
- When Pac-Man passes through one of the exits on the middle left or middle right side of the maze, he immediately reappears on the opposite side.
- Pac-Man eats all the Pac-Dots on the path he's following. Once a Pac-Dot has been eaten, it is removed from the maze.
You'll be given a string describing Pac-Man's behavior on the junctions that he's going to reach. This string will be made of the following characters:
L: do a 90° turn to the left
R: do a 90° turn to the right
F: go forwards (no direction change)
B: go backwards (do a U-turn)
When all characters have been processed, Pac-Man stops at the next junction he encounters.
You have to print or output the number of Pac-Dots eaten along the input path.
- You can write a full program or a function.
- You can take input in either uppercase or lowercase, as either a string or an array of characters. You may also use other characters (but only one character per direction) or integers in
[0 .. 9]. If you do so, please specify it clearly in your answer.
- You can assume that the input is always valid. (The jsFiddle below will detect errors, but you're not supposed to.)
- This is code-golf, so the shortest code in bytes wins.
- Standard loopholes are forbidden.
It may not be required nor optimal to store the exact shape of the maze.
Test cases and demo
The following test cases -- or any other input -- can be tested in this jsFiddle.
1. Input : "" Output : 1 Comment: Pac-Man just advances to the first junction, eats the Pac-Dot on it and stops. 2. Input : "L" Output : 7 3. Input : "FFR" Output : 13 4. Input : "LFLR" Output : 17 Comment: Pac-Man will exit on the middle right side and re-appear on the left side. 5. Input : "BBBB" Output : 2 6. Input : "BRRFFFL" Output : 15 7. Input : "LFFRLFFFFRF" Output : 50 8. Input : "BRFRLRFRLFR" Output : 54 Comment: Pac-Man will exit on the middle left side and re-appear on the right side. 9. Input : "FFLRLFFLLLLFFBFLFLRRRLRRFRFLRLFFFLFLLLLFRRFBRLLLFBLFFLBFRLLR" Output : 244 Comment: All cleared!