‹ back to the board

Friday, November 6

Apprenticelogic & control flow

The Wisp-Trail of Cordray Heath

Out on Cordray Heath a will-o'-wisp leads lost travellers in circles, and the Lanternwardens of Bitwick will pay for a map of where it truly goes. Their scouts have written down the wisp's trail as a string of signs. You must work out where the trail ends.

Write wispTrail(trail), where trail is a string made only of the characters F, L, R and X.

A follower starts at position (0, 0) facing north. North increases y by 1, east increases x by 1, south decreases y by 1, and west decreases x by 1. Each sign is read in order, from first to last:

  • F: move one step in the direction currently faced.
  • L: turn 90 degrees to the left (counterclockwise). If reversal is active, turn 90 degrees to the right instead.
  • R: turn 90 degrees to the right (clockwise). If reversal is active, turn 90 degrees to the left instead.
  • X: the wisp flickers. Reversal switches from inactive to active, or from active to inactive. The follower does not move or turn.

Reversal is inactive at the start.

Return the final position as a list of two integers [x, y]. An empty trail returns [0, 0].

  • running state across iterations
  • Apprentice

examples

  1. case 1trail of "XRF"[-1, 0] - reversal is active, so R turns left to face west, then one step west
  2. case 2trail of "FXLFXLF"[1, 2] - step north; reversal on, L turns right to face east; step east; reversal off, L turns left to face north; step north
  3. case 3trail of "" (empty)[0, 0]
reward
220 XP
signature
wispTrail
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • empty trail stays at start
  • plain right turn
  • reversed right turns left
  • reversal toggled on and off
  • turn around and go south
  • double flicker cancels
  • full turn returns to north
  • square loop returns home
  • mixed reversed turns
The Wisp-Trail of Cordray Heath · bitwick