ダウンワードナンバーレース


10

あなたの仕事は、究極のナンバーレースの対決で以前の合計に乱数を追加するプログラムを作成することです。

各レーサー(列)は0から始まり、すべてのレーサーが勝つために必要なスコアに達するまで、レースの各ステップで前の合計に1または0を加算します。1または0はランダムに選択する必要があります(ランダムの標準的な定義はここにあります)。出力には、各列が1人のレーサーを表す、次の形式でレースの結果が表示されます。

>> racers:5,score needed:2

0 0 0 0 0 # all racers start at 0
+ + + + + # add
1 0 0 0 1 # random 1 or 0
= = = = = # equals
1 0 0 0 1 # sum
+ + + + +
0 0 0 0 1
= = = = =
1 0 0 0 2 # winner!
+ + + +  
1 1 1 1  
= = = =  
2 1 1 1  
  + + +  
  1 1 1  
  = = =  
  2 2 2   # losers

注:出力に含める必要があるのは、数字の+および=だけです。

入力

プログラムは、次の2つのパラメーターを入力として受け入れます。

  1. レーサー(列)の数。2より大きい必要があります。
  2. 勝つために必要なスコア。1以上である必要があります

これはコードゴルフで、バイト数が最も少ないプログラムが優先されます。

編集:強制できない最大スコア9があります。これは、列の整合性を維持するためです。また、列間のスペースは出力で省略できます。


サポートする必要がある列の最大数と最大スコアはいくつですか?
— nanofarad

最大値は定義されていないため、最小値と同じです。少なくとも3つの列と2つのスコア。
— アトラスロジスト2016年

3
必要なスコアは2桁ですか?
— Leaky Nun

4
"出力に含める必要があるのは、数値、+、および=のみです。" スペースはどうですか?
— Leaky Nun

スペースは保持する必要はありません。わかりやすくするために例に示しています。2桁についてのよい質問です。9の最大スコアがあると思います。質問を編集します。
— アトラスロジスト2016年

回答:


5

ゼリー、37 36 33バイト

,‘X
0Ç<³$пµżIFµ“+=”ṁṖ⁸żF
ÇСḊz⁶G

デニスのおかげで3バイト。

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

説明

,‘X                    Helper link. Argument: n. Radomly return n or n+1.

 ‘                     Increment n
,                      Pair. Yield [n, n+1]
  X                    Return a random item from the pair.

0Ç<³$пµżIFµ“+=”ṁṖ⁸żF   Monadic link. Argument: s. Generate one racer.

0                       Start with value 0.
  <³$пµ                While value is less than s:
 Ç                        Use helper link to increment current value.
                        Collect intermediate results in a list.
         I              Compute consecutive differences.
        ż               Zip intermediate results with their next increment value 0 or 1.
          Fµ            Flatten. Let's call the current list A.
                        Odd items of A are racer state and even items are random 0 or 1.
            “+=”        Yield "+=".
                 Ṗ      Yield A without its last element.
                ṁ       Mold i.e Repeat the characters of the string until it contains length(A)-1 characters.
                  ⁸ż    Zipwith. Pair the elements of A with the correponding characters
                    F   Flatten.

ÇСṫ2z” G               Main link. Arguments: s (score needed), r (#racers)

ÇС                     Call the link above r times.
                        Generate a list of r racers.
   Ḋ                    Remove first element of the list (its a garbage s value)
    z⁶                  Transpose with space as fill value.
      G                 Grid. Format the result.

最初のヘルパーリンクを,‘X(インクリメントされたnのペア、ランダム選択)に置き換えることができます。メインリンクでṫ2は、Ḋ(デキュー)および” 変数に置き換えることができます⁶。
— デニス


2

TSQL、367 345 341バイト

ゴルフ

DECLARE @r int=20, -- racers
        @g char=2  -- goal

DECLARE @ varchar(99)=REPLICATE('0',@r)a:PRINT @
DECLARE @A varchar(99)='',@s varchar(99)='',@i int=0WHILE @i<@r
SELECT
@i+=1,@A+=char(43-x*11),@s+=IIF(x=1,' ',LEFT(y,1)),@=RIGHT(@,@r-1)+IIF(x=1,' ',REPLACE(LEFT(@,1)+y,@g+1,' '))FROM(SELECT
IIF(LEFT(@,1)IN('',@g),1,0)x,ROUND(RAND(),0)y)z
PRINT @A+'
'+@s+'
'+REPLACE(@A,'+','=')IF @>''goto a

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

非ゴルフ:

DECLARE @r int=10, -- racers
        @g char=2  -- goal

DECLARE @ varchar(99)=REPLICATE('0',@r)
a:
PRINT @
DECLARE @A varchar(99)='',@s varchar(99)='',@i int=0

WHILE @i<@r
  SELECT
    @i+=1,
    @A+=char(43-x*11),
    @s+=IIF(x=1,' ',LEFT(y,1)),
    @=RIGHT(@,@r-1)+IIF(x=1,' ',REPLACE(LEFT(@,1)+y,@g+1,' '))
  FROM(SELECT IIF(LEFT(@,1)IN('',@g),1,0)x,ROUND(RAND(),0)y)z

PRINT @A+'
'+@s+'
'+REPLACE(@A,'+','=')

IF @>''GOTO a

テストサイトのランダムシードは常に同じで、毎回同じ結果が得られることに注意してください。スタジオ管理では、異なる結果が得られます。レーサーと目標に異なる値を使用して、異なる画像を取得できます


1

Python 3、237バイト

from random import*
def f(n,t):
 x='0'*n,;i=j=0;y=''
 while' '*n!=x[i]:
  if j==n:j=0;x+=y,;y='';print(x[i]);i+=1
  y+=' 'if x[i][j]in(' ',str(t))else eval(["'+'","str(randint(0,1))","'='","str(int(x[i-3][j])+int(x[i-1][j]))"][i%4]);j+=1

引数を介して入力を受け取り、STDOUTに出力する関数。このアプローチは、すべてのレーサーに対して、出力が「+値=値」の形式の期間4のサイクルに従うという事実を利用しています。4を法とするカウンターを使用することにより、各ステップの望ましい値を文字列として含むリストにインデックスを付け、その結果をPythonのeval関数を使用して評価できます。

使い方

from random import*                       Import Python's random module to access the
                                          randint function
def f(n,t):                               Function with input number of racers n and target
                                          number t
x='0'*n,;i=j=0;y=''                       Initialise return tuple storage x, state number
                                          i, racer number j and append string y for x
while' '*n!=x[i]:                         Loop through all j for some i. If the current
                                          state consists only of spaces, all racers have
                                          finished, so stop
y+=...eval([...][i%4])...                 Index into list, using i mod 4, to find the
                                          desired process for the cycle step, and append to
                                          y
(If first step of cycle)
...+...                                   Plus sign
(If second step of cycle)
...str(randint(0,1))...                   Random number from (0,1)
(If third step of cycle)
...=...                                   Equals sign
(If fourth step of cycle)
...str(int(x[i-3][j])+int(x[i-1][j]))...  Addition of random number to previous racer
                                          'score'
...' 'if x[i][j]in(' ',str(t))...         But append space if the racer has previously
                                          finished, or has reached the target
...j+=1                                   Increment j
if j==n:j=0;x+=y,;y='';print(x[i]);i+=1   If j=n, all j must have been looped through.
                                          Reset j, append new state y to x, reset y, print
                                          current state to STDOUT and increment i. When
                                          this first executes, x contains only the initial
                                          state, meaning that this is printed and the cycle
                                          starts with the second state.

イデオーネでお試しください


1

Python 2、191バイト

from random import*
def c(p,w,r=[],l=0):
 while p:
	p-=1;s='0'
	while`w`>s[-1]:s+="+%s="%randint(0,1);s+=`eval(s[-4:-1])`;l+=2
	r+=[s]
 for z in map("".join,zip(*(t+l*' 'for t in r))):print z

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


Python 3、200バイト

from random import*
def c(p,w,r=[],l=0):
 while p:
  p-=1;s='0'
  while str(w)>s[-1]:s+="+%s"%randint(0,1);s+="=%s"%eval(s[-3:]);l+=2
  r+=[s]
 for z in map("".join,zip(*(t+l*' 'for t in r))):print(z)

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


0

Python 2、278バイト

import random
r=5
w=2
s=(0,)*r
while s.count(w)<len(s):
    print ''.join(map(lambda a:str(a),s))+"\n"+'+'*len(s)
    s = tuple(map(lambda x: x<w and x+random.randrange(2) or x,s))
    print ''.join(map(lambda a:str(a), s))+"\n"+'='*len(s)
    s = tuple([x for x in s if x!= w])

ここで、rはnoです。レーサーのwは勝つためのスコアです

ここでお試しください!


2
私はあなたのプログラムをテストしました、それは質問で説明された結果を表示しません、すべてが左に移動されました。
— t-clausen.dk

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.