[Question]
  [
# 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
[![enter image description here](https://i.stack.imgur.com/fCGusm.jpg)](https://i.stack.imgur.com/fCGusm.jpg)
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.
  
[Answer]
# CJam, ~~52~~ 39 bytes
```
q~[3X0].{@40,m<1$({(+W%}&:T*T@#)T<)}e_p
```
Try it online in the [CJam interpreter](http://cjam.aditsu.net/#code=q~%5B3X0%5D.%7B%4040%2Cm%3C1%24(%7B(%2BW%25%7D%26%3AT*T%40%23)T%3C)%7De_p&input=33%20%5B38%2016%2022%5D).
### 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.
```
[Answer]
# 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)
```
[Answer]
# [Perl 5](https://www.perl.org/), 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!](https://tio.run/##FY3LCsIwFER/JcJd@EgkDytqCHXVnV9QQtHaYKGYS6uIlP668ZYZhrMYZrDpuyyl4X1j9QjBYUR7j@Nw/UKwEDYOqlJ6Ikd2QuXmeJppJ3NJMH0ebddAEFRTfgJ0xtZLvV4ACsXPRQkVB/QrClpiIfbM8FmSpLi281FRap@SOTC1Z1ozY34RX218Dklcsq1UMonrHw "Perl 5 – 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
```
[Answer]
## 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(`x`for 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!](https://tio.run/##VZDdCoMwDIXvfYrc2bJM/Blj0/kkUrBq3RxapSq4p3et1sEuQvOdJIdDh8/06mW4rpWooSYtjjR2QKUt74qKA8cCyzSIFZdPQTaiDvBUkeiObeYzPAc0KTRbMOxAuQkB0ysh28WmBgMPI4t2FGA39mlSemKZhKyIdT7OtJkS06ykC@B6776RJF/yulewQCNhnDuSKTJaGzQJj/rLiDbhxTet/3tNhpMelwwzRuk6qEZO@iOy6IYQXBHCkCFEEV2/)
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?
[Answer]
# [Haskell](https://www.haskell.org/), ~~135~~ 112 bytes
```
s!t=[s..39]++[0..mod(t-1)40]
s#t=[s,s-1..0]++[39,38..mod(t+1)40]
(a%b)c s=[s#s,s#s,s#s,s#a,a!a,a!b,b#c,[c]]>>=id
```
[Try it online!](https://tio.run/##PcrRCsIgFIDh@57iDBsoOtGdiO3CvcjwQl2QtK1I398mRRf/1f/dXXrc1rWU1GQzJylxtJzPSsrtudDcaXZR9pRInSJ1WkpVP44Ch5/hX0Nd61mAdEBy0H9OuKbmhSdBzMHaaTJxKZuLOxh4veOe4QwUh1ZfGfQ9IJYP "Haskell – Try It Online")
*Saved 23 bytes thanks to Laikoni*
]