Simplified version of "Signpost" puzzle from Simon Tatham's Portable Puzzle Collection.
How the puzzle is created
We pick up randomly positive integer N
and one of permutations of 1…N
with 1
at the first position:
N = 12
Numbers 1
and N
always stay open, and 2
is always on the right of 1
.
So we look at numbers 2
and 3
. 3
is on the left from 2
,
so we mark 2
with left arrow:
Next we look at 3
and 4
. 4
is on the right from 3
, so we mark 3
with right arrow:
And so on so that every arrow above the number points towards the number that follows it (though the next number can be any distance away in that direction):
After all we hide initial sequence except 1
and N
:
and write down puzzle in form of array:
["1", "→", "→", "→", "→", "←", "→", "←", "12", "←", "←", "←"]
Task
Given by an array of directions and two key positions, restore the initial sequence of numbers.
Input
List of directions and keys in any form, suitable for your language: array, json, string etc.
Let for example "→"
be "r"
and "←"
be "l"
:
["1", "r", "r", "r", "l", "r", "l", "12", "l", "l", "l"]
You may take N
as a separate input or calculate it as length of list.
You may assume that key points are given not as numbers,
but as symbols (for example "1"
= "b"
(egin) and N
= "e"
(nd) )
Input always valid: non-empty, no typos, errors and necessarily corresponds to some valid sequence.
Output
Encrypted sequence as array, echo-print or as you like.
You may only use test cases that are composed of existing sequences
and therefore have at least one solution.
But if there may be several, you can print any or all of them.
Test cases
In "r" / "l" / "1" / "N" form:
["1", "2"] → [1, 2]
["1", "3", "l"] → [1, 3, 2]
["1", "r", "5", "l", "l"] → [1, 3, 5, 2, 4] or [1, 2, 5, 4, 3]
["1", "r", "6", "r", "r", "l"] → [1, 2, 6, 3, 4, 5]
["1", "r", "r", "r", "r", "l", "r", "l", "12", "l", "l", "l"] → [1, 3, 11, 5, 9, 10, 6, 8, 12, 4, 2, 7]
["1", "7", "l", "l", "l", "l", "l"] → [1, 7, 6, 5, 4, 3, 2]
["1", "r", "r", "l", "7", "l", "l"] → [1, 5, 3, 4, 7, 2, 6] or [1, 6, 4, 5, 7, 3, 2]