# 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:

1. You must know your code. Assume it's (38, 16, 22) for now.
2. 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
3. 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.
4. Turn the dial to the right and stop when the third number (22) lines up with the indicator
5. 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.

• Unless he can speed count, the inhaler would be pointless... Thus, my program is: function combination(code){alert("Help! Someone open this locker, the combination is "+code+"!")} Oct 29 '15 at 17:29
• @CᴏɴᴏʀO'Bʀɪᴇɴ, valid point...:-) But: 1. The Peter-person we're talking about isn't the sharpest tool in the shed. 2. You wouldn't want to tell the code to someone would you. 3. Who knows, maybe Stewie had a spare can of spray..? Oct 29 '15 at 17:32
• Oh, right. Stewie has an infinite can of pepper spray, right? Haha :3 Oct 29 '15 at 17:40
• Can we use alternative algorithms that still open that lock? Oct 30 '15 at 13:59
• Peter needs to learn martial arts. (And why is there no coach at the gym when Brian collapses? Budget cuts? ;) Oct 30 '15 at 22:34

# CJam, 52 39 bytes

q~[3X0].{@40,m<1$({(+W%}&:T*T@#)T<)}e_p  Try it online in the CJam interpreter. ### How it works q~ e# Read and evaluate all input. This pushes the initial position e# as an integer and the combination as an array. [3X0] e# Push [3 1 0]. This encodes the respective numbers of full turns .{ e# For each number in the combination (N) and the corresponding e# number of full turns (F): @ e# Rotate the initial position on top of the stack. 40,m< e# Push [0 ... 39] and rotate it that many units to the left. e# For position P, this pushes [P P+1 ... 39 0 ... P-2 P-1]. 1$(   e#   Copy F and subtract 1.
{     e#   If the result is non-zero:
(+  e#     Rotate the array of length 40 one unit to the left.
W%  e#     Reverse it.
}&    e#   For position P, this pushes [P P-1 ... 0 39 ... P+2 P+1].
:T*   e#   Save in T and repeat the array F.
T@    e#   Push T. Rotate N on top of the stack.
#)    e#   Find the index of N in T and add 1 to it.
T<    e#   Keep that many elements from the beginning of T.
)     e#   Pop the last element of the result (N).
}       e# N is the new initial position.
e_p     e# Flatten the resulting array and print it.


# Groovy, 189 175 bytes

Assumes the indicator is passed as arg0 and the combo is passed as arg1, arg2, and arg3 on the command line...

i=(args[0]as int)+1
r={i--;i=i<0?39:i;print"$i "} l={i=++i%40;print"$i "}
M={j,c->while(i!=j as int){c()}}
120.times{r()}
M(args[1],r)
40.times{l()}
M(args[2],l)
M(args[3],r)


# Perl 5, 129 + 1 (-a) = 130 bytes

sub c{$f=pop;do{say$f;$f+=$_[0];$f=$f==-1?39:$f==40?0:$f}while$f-$_[1]}$p=3;c(2*!$p-1,@F[$_,$p]),$p=$_ for 3,3,3,0,0,1,2;say$F[2]  Try it online! How? sub c{ # Takes 3 parameters: increment, ending position, starting position$f=pop;                    # first place to start counting
do{
say$f; # output current position$f+=$_[0]; # move position$f=$f==-1?39:$f==40?0:$f # roll over when passing zero }while$f-$_[1] # stop when ending positition reached } # @F gets defined by the -a command line option # @F holds the combination followed by the starting position$p=3;                       # starting position is in array index 3, this variable will track the array index of
# the current position on the dial

c(2*!$p-1,@F[$_,$p]),$p=$_ # call the movement function (c), setting direction to the left (1) or right (-1) as needed # based on the array index of the previous position (go left when moving from array index 0) for 3,3,3,0,0,1,2; # list of the array index of the next position say$F[2]                    # output final position


## Python 2, 262 bytes

It feels so long. But there is also a lot of turning going on.

def f(l,s):
r=lambda a,b,c=1:range(a,b,c)
a=r(39,l[0],-1);b=r(l[0],-1,-1)
c=r(l[1],l[2]-1,-1)if l[2]<l[1]else r(l[1],-1,-1);c.extend(r(39,l[2]-1,-1))
return'  '.join(xfor x in sum([r(s,-1,-1),a,b,a,b,a,b,r(39,l[0],-1),r(l[0],40),r(0,40),r(0,l[1]+1),c],[]))


Try it online!

I think I can concatenate some parts better in my last line but I am still new to code golf and I don't know how to tackle that list combination in a short way.

Any Ideas on improving this?

s!t=[s..39]++[0..mod(t-1)40]

• You can shorten declarations like l s t= by declaring an infix operator s#t= instead. It also works for more than two arguments: (a%b)c s=. Aug 17 '17 at 8:48
• And I think you can drop the s+1. Aug 17 '17 at 8:51