Your task is to create a program that adds random numbers to previous sums in the ultimate number racing showdown.

Each racer (column) starts at 0 and adds either 1 or 0 to the previous sum at each step of the race until all of the racers reach the score required to win. The 1 or 0 should be chosen at random (the standard definition of random can be found here). The output will show the result of the race, with each column representing one racer, in this format:

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

Note: only the numbers, +, and = need to be included in the output.


Your program will accept the following two parameters as input:

  1. the number of racers (columns), which must be greater than two
  2. the score required to win, which must be greater than one

This is code-golf--the program with the least bytes wins.

Edit: there's a non-enforceable max score of 9--this is to preserve the integrity of the column. Also, spaces between columns can be omitted in the output.

  • \$\begingroup\$ What is the maximum number of columns and maximum score that would need to be supported? \$\endgroup\$ – nanofarad Jun 21 '16 at 11:32
  • \$\begingroup\$ There's no defined max, so it would be the same as the minimum: at least three columns and a score of two. \$\endgroup\$ – atlasologist Jun 21 '16 at 11:37
  • 3
    \$\begingroup\$ Will the required score have two digits? \$\endgroup\$ – Leaky Nun Jun 21 '16 at 12:35
  • 4
    \$\begingroup\$ "only the numbers, +, and = need to be included in the output." what about the spaces? \$\endgroup\$ – Leaky Nun Jun 21 '16 at 12:36
  • \$\begingroup\$ The spaces do not need to be preserved, they're in the example for clarity. Good question about the two digits--I suppose there is a max score, which is nine. I'll edit the question. \$\endgroup\$ – atlasologist Jun 21 '16 at 12:42

Jelly, 37 36 33 bytes


3 bytes thanks to Dennis.

Try it online


,‘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.
  • \$\begingroup\$ You can replace the first helper link with ,‘X (pair with incremented n, random choice). In the main link ṫ2 can be replaced with (dequeue) and with the variable . \$\endgroup\$ – Dennis Jun 23 '16 at 3:56

Pyth, 38 34 bytes


Try it online!


TSQL, 367 345 341 bytes


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
@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
'+REPLACE(@A,'+','=')IF @>''goto a

Try it online


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

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

WHILE @i<@r
    @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


IF @>''GOTO a

Note that the random seed on the test site will always be the same, giving the same result each time, in studio management it will give different results. Can use different values for racers and goal to get a different picture


Python 3, 237 bytes

from random import*
def f(n,t):
 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

A function that takes input via argument and prints to STDOUT. This approach makes use of the fact that the output follows a cycle of period four, of the form '+ value = value', for all racers. By using a counter modulo four, a list containing the desired value for each step as a string can be indexed into, and the result evaluated using Python's eval function.

How it works

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
(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
...' '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.

Try it on Ideone


Python 2, 191 bytes

from random import*
def c(p,w,r=[],l=0):
 while p:
 for z in map("".join,zip(*(t+l*' 'for t in r))):print z

Try it online!

Python 3, 200 bytes

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

Try it online!


Python 2, 278 bytes

import random
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])

where r is the no. of racers and w is the score to win

Try it here!

  • 2
    \$\begingroup\$ I tested your program, it doesn't show the result as described in the question, everything has been moved to the left. \$\endgroup\$ – t-clausen.dk Jun 23 '16 at 12:49

Perl 5, 150 bytes

$,=$";say@n=(0)x(@e=('=')x(@p=('+')x<>));$t=<>;while(grep$_<$t,@n){@r=map{$_+=($g=0|rand 2);$g}@n;for$l(p,r,e,n){say map{$n[$_]>$t?$":$$l[$_]}0..$#n}}

Try it online!

First input is number of racers, second is score needed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.