29
5
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!
The maze
To help you build your own encoding of the maze, you can get some raw data here.
Pac-Man's journey
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.
The challenge
Input
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 leftR
: do a 90° turn to the rightF
: 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.
Output
You have to print or output the number of Pac-Dots eaten along the input path.
Rules
- 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.
Hint
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!
1
Here's some more data to help out: https://pastebin.com/G4MnbVww . It's a list of every junction and the number of pac-dots on the road to the next junction depending on which direction you go (0=up, 1=left, 2=down, 3=right). There might be some errors, and keep in mind that junctions 12, 13, 15, 16, 18, and 19 have no dot in the middle, while all the others do.
– Esolanging Fruit – 2017-04-04T01:23:04.130@Challenger5 This is looking good. Because the moves are relative, you'd probably want to also keep track of the new orientation of Pac-Man when the next junction is reached, though. – Arnauld – 2017-04-04T02:30:16.013
BTW In the game pac man cannot do a u-turn – Matthew Roh – 2017-04-04T06:57:58.937
4@SIGSEGV By 'u-turn', I just mean changing to the opposite direction, which is possible at any time in the original arcade game and all the clones I know. Should I use another term? – Arnauld – 2017-04-04T07:16:54.383
I'm pretty sure Pac-Man started heading left in the arcade game, not right. – mbomb007 – 2017-04-04T20:40:12.390
@mbomb007 Yes, you are right. I shouldn't have relied on my aging memory and checked that only after posting. (Not that it changes anything to the challenge. ^^) – Arnauld – 2017-04-04T20:43:35.893
"You may also use other characters" - Can we use '\0', (char)1, (char)2, (char)3 instead of FLBR? – ngn – 2017-04-14T16:18:57.497
@ngn Do you mean NUL, SOH, STX, ETX? That's kinda unexpected but doesn't infringe the rules. So, yes. Digits would be fine as well (updated). – Arnauld – 2017-04-14T16:24:49.077
Yes, that's what I meant. Thanks. – ngn – 2017-04-14T16:33:03.470