# Downward Number Race

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.

## Input

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.

• What is the maximum number of columns and maximum score that would need to be supported? – nanofarad Jun 21 '16 at 11:32
• There's no defined max, so it would be the same as the minimum: at least three columns and a score of two. – atlasologist Jun 21 '16 at 11:37
• Will the required score have two digits? – Leaky Nun Jun 21 '16 at 12:35
• "only the numbers, +, and = need to be included in the output." what about the spaces? – Leaky Nun Jun 21 '16 at 12:36
• 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. – atlasologist Jun 21 '16 at 12:42

,‘X
<³$Ð¿µ 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.  • 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 ⁶. – Dennis Jun 23 '16 at 3:56 # Pyth, 38 34 bytes j.tm+j\=jL\+C,.u+NYJ.WnsHQ+ZO2Y0J+\=QE j.tmj\=jL\+.T,.u+NYJ.WnsHQ+ZO2Y0JE  Try it online! ## TSQL, 367345 341 bytes Golfed 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  Try it online Ungolfed: 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  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): 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  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 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.  Try it on Ideone # Python 2, 191 bytes from random import* def c(p,w,r=[],l=0): while p: p-=1;s='0' whilew>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  Try it online! # Python 3, 200 bytes 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)  Try it online! Python 2, 278 bytes 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])  where r is the no. of racers and w is the score to win Try it here! • I tested your program, it doesn't show the result as described in the question, everything has been moved to the left. – 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}}