The scene is:
Peter is at the gym with his buddy Brian when Brian suddenly is in dire need of his inhaler. Brian manages to tell Peter the code to his combination lock before he collapses on the floor.
The moment Peter gets to Brian's locker and sees what the indicator is pointing at, Stewie ambushes him and sprays a full can of pepper spray in his face, thus blinding Peter.
Peter must now try to open the lock without looking at it. He starts turning the dial to the right, counting the numbers while he passes them. He then, at the correct number starts turning the dial to the left, still counting, and finally turns it to the right until the lock opens.
The challenge:
Write a function/program that takes two inputs, the combination from Brian, and the indicator position. Output the numbers Peter has to count.
Rules:
- The combination and the indicator position must be separate arguments.
- The input can be either from command prompt or as function arguments.
- The output must be printed to the screen / otherwise displayed (not to file)
- Assume that the starting position is not the same as the first number, and that all three numbers in the combination are unique
- It's the lock shown in the picture below, with possible numbers: 0-39.
Instructions:
To open the lock below, you need to follow a set of instructions:
- You must know your code. Assume it's (38, 16, 22) for now.
- Turn the dial 3 times to the right (passing the starting number three times), then stop when the first number (38) aligns with the indicator
- Turn the dial 1 full turn to the left, passing the first number, and stop when the second number (16) lines up with the indicator.
- Turn the dial to the right and stop when the third number (22) lines up with the indicator
- Pull the lock down
Example:
Input
38 16 22
33
Output
33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 39 38 39 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22
Standard code golf rules apply.
Solutions that are posted later can still win if they're shorter than Dennis' answer.
function combination(code){alert("Help! Someone open this locker, the combination is "+code+"!")}
\$\endgroup\$