コンビネーションロックカウンター


20

シーンは次のとおりです。

ブライアンが突然吸入器を切に必要としているとき、ピーターは彼の相棒ブライアンとジムにいます。ブライアンは、床に倒れる前にピーターにコンビネーションロックのコードを伝えることができました。

ピーターがブライアンのロッカーに着き、インジケーターが何を指しているのかを見た瞬間、スチューイーは彼を待ち伏せし、彼の顔にコショウのスプレーをいっぱいスプレーして、ピーターを盲目にします。

ピーターは、見ないでロックを開こうとする必要があります。彼はダイヤルを右に回し始め、数字を渡しながらカウントします。その後、彼は正しい番号でダイヤルを左に回し始めますが、カウントを続け、最後にロックが開くまで右に回します。


チャレンジ:

ブライアンからの組み合わせとインジケーターの位置の2つの入力を受け取る関数/プログラムを作成します。Peterがカウントしなければならない数字を出力します。

ルール:

  • 組み合わせとインジケータの位置は別々の引数である必要があります。
  • 入力は、コマンドプロンプトから、または関数の引数として行うことができます。
  • 出力は画面に印刷する必要があります/そうでなければ表示されます(ファイルではなく)
  • 開始位置は最初の数字と同じではなく、組み合わせの3つの数字はすべて一意であると仮定します
  • これは、下の図に示されているロックで、可能な番号は0〜39です。

手順:

以下のロックを開くには、一連の指示に従う必要があります。

  1. コードを知っている必要があります。今のところ(38、16、22)と仮定します。
  2. ダイヤルを3回右に回し(開始番号を3回渡します)、最初の番号(38)がインジケーターと一致したら停止します
  3. ダイヤルを左に1回転させて最初の数字を渡し、2番目の数字(16)がインジケーターと一致したら停止します。
  4. ダイヤルを右に回し、3番目の数字(22)がインジケーターと一致したら停止します
  5. ロックを引き下げる

ここに画像の説明を入力してください

例:

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

標準コードのゴルフ規則が適用されます。

後で投稿されたソリューションは、デニスの答えよりも短い場合でも勝つことができます。


9
彼は、カウントを高速化することができない限り、吸入器は無意味だろう...このように、私のプログラムは以下のとおりです。function combination(code){alert("Help! Someone open this locker, the combination is "+code+"!")}
コナー・オブライエン

2
@CᴏɴᴏʀO'Bʀɪᴇɴ、有効なポイント... :-)しかし:1.私たちが話しているピーターの人は、小屋で最も鋭い道具ではありません。2.コードを誰かに伝えたくないでしょう。3.誰が知っているか、多分Stewieに予備のスプレー缶があったかもしれない。
スティーヴィーグリフィン

1
ああ、そう。Stewieにはペッパースプレーの缶が無限にありますよね?母:3
コナーオブライエン

まだそのロックを開く代替アルゴリズムを使用できますか?
bmarks

1
ピーターは武道を学ぶ必要があります。(そして、ブライアンが倒れたときにジムにコーチがいないのはなぜですか?予算削減?)
kirbyfan64sos

回答:


3

CJam、52 39バイト

q~[3X0].{@40,m<1$({(+W%}&:T*T@#)T<)}e_p

CJamインタープリターでオンラインで試してください。

使い方

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.

1

グルーヴィー、 189 175バイト

コマンドラインでインジケータがarg0として渡され、コンボがarg1、arg2、およびarg3として渡されると仮定します...

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)

1

Perl 5、129 + 1(-a)= 130バイト

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]

オンラインでお試しください!

どうやって?

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

1

Python 2、262バイト

とても長く感じます。しかし、多くの変化が起こっています。

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],[]))

オンラインでお試しください!

私は最後の行でいくつかの部分をよりうまく連結できると思いますが、私はまだゴルフをコーディングするのが初めてであり、そのリストの組み合わせに短期間で取り組む方法がわかりません。

これを改善するアイデアはありますか?


0

Haskell135 112バイト

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

オンラインでお試しください!

ライコニのおかげで23バイト節約


代わりにl s t=中置演算子を宣言することにより、宣言を短縮できs#t=ます。また、3つ以上の引数に対しても機能します(a%b)c s=
ライコニ

そして、私はあなたがドロップすることができると思いますs+1
ライコニ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.