10
\$\begingroup\$

Background

Back in the late 90's / first 00's when Flash Web Design was so much cool that noone could live without having a full Flash website, or at least an animated widget, I was hired to develop a "horse races viewer" in Flash/Actionscript, in the shape of an 80's videogame style animation, so the site's visitors could not only read the race results, but they could see it in a moving animation! WOW! Impressive!

They provided me a CSV file with all the races details: start and arrival order, horse names, driver names, prizes, etc. My Flash app read that file for each race and displayed the above said animation.

Nowadays the Flash support is significantly declined, so we must revert to ascii-art!

Task

Your task is to create a full program or function that reads the race data in CSV format from standard input and outputs an ascii-art representation of the race as shown in the example below.

INPUT

CSV data with 2 fields: 1) start order; 2) arrival time at the Finish in the format 1.13.4 (1 minute, 13 seconds, 4 tenths of second). If the time reports R means that the horse is Retreated (didn't finish the race) due to incident, fall or other reason. Note: The arrival time could be the same for 2 or more horses, in this case they share the arrival position.

1,1.13.4
2,1.13.0
3,R
4,1.12.7
5,1.11.5
6,1.13.4
7,1.12.1
8,1.17.9

OUTPUT

For each CSV row, output a racetrack like this:

1_|______________4(1.13.0)___________________________

The racetrack is composed by:

  • 1 which is the horses start order.
  • _| where _ is a spacer and the | is the finish line.
  • 50 x _ that represents 50 tenths of second.
  • 5(1.13.4) that is the arrival position followed by the arrival time. This must be positioned respecting the time differences between horses. For example: you position the 1st arrived on the Finish line at time 1.11.5, the second arrives at time 1.12.1, the difference is 1.12.1 - 1.11.5 = 6 tenths of second, so the second horse should be positioned at the 6th character, and so on. If the time difference is more than 50 tenths of seconds (or 5 seconds) you must position the horse at the end. The same if the horse is R (Retreated).

So the whole racetrack for the CSV data above should be:

  F=Finish line
1_|____________________________5(1.13.4)_____________
2_|______________4(1.13.0)___________________________
3_|__________________________________________________R
4_|___________3(1.12.7)______________________________
5_1(1.11.5)__________________________________________
6_|____________________________5(1.13.4)_____________
7_|_____2(1.12.1)____________________________________
8_|__________________________________________________6(1.17.9)
  012345678901234567890123456789012345678901234567890

There is no need to add F=Finish line, and the last line 0123456789... that is only for explaining purpose.

Test cases

RACE:
1,1.14.9
2,R
3,R
4,1.14.2
5,1.15.2
6,1.15.3
7,1.15.3

RACE:
1,1.13.6
2,1.13.8
3,R,
4,1.15.9
5,1.13.8
6,R,
7,1.14.4
8,1.15.6
9,1.14.1
10,1.13.9
11,1.13.2
12,1.14.3
13,1.15.0

RACE:
1,1.13.4
2,1.13.0
3,R
4,1.12.7
5,1.11.5
6,1.13.4
7,1.12.1
8,1.17.9

RACE:
1,1.17.3
2,1.20.4
3,1.17.0
4,1.18.8
5,1.18.5
6,1.18.4
7,1.18.4
8,1.17.8
9,1.18.3
10,1.18.7
11,R

RACE:
1,1.17.5
2,R
3,1.17.7
4,1.16.9
5,1.16.1
6,1.18.9

RACE:
1,1.12.8
2,1.13.0
3,1.13.2
4,1.12.7
5,1.11.5
6,1.13.0
7,1.12.1
8,1.12.8

Rules

  • Shortest code wins.
\$\endgroup\$
  • \$\begingroup\$ In the example where 5th is tied, wouldn't horse 8 still place 7th? \$\endgroup\$ – Jonathan Allan Nov 2 '16 at 18:33
  • \$\begingroup\$ May we assume that the minute number will always be 1? \$\endgroup\$ – ETHproductions Nov 2 '16 at 18:37
  • \$\begingroup\$ @JonathanAllan It depends on the rules, in the example I assumed that if the horses share the 5th position, the next will be considered 6th. \$\endgroup\$ – Mario Nov 2 '16 at 18:43
  • \$\begingroup\$ @ETHproductions Yes you can assume the minute is always 1. \$\endgroup\$ – Mario Nov 2 '16 at 18:43
  • 2
    \$\begingroup\$ Also, may we assume that there will always be a non-R horse? (I know this isn't a sensible question in real life, but this is code.) \$\endgroup\$ – PurkkaKoodari Nov 2 '16 at 19:21
1
\$\begingroup\$

JavaScript (ES6), 261 bytes

Takes an array of time strings "1.ss.t" as input. The start order is implicit.

a=>a.map((t,i)=>(u='_',++i>9?'':' ')+i+u+u.repeat(x=(x=t>'9'?50:t.slice(2)*10-s[0].slice(2)*10)>50?50:x,p=s.indexOf(t)+1+`(${t})`).replace(u,'|')+(x<50?p:'')+u.repeat((x=50-p.length-x)>0?x:0)+(x>0?'':t>'9'?t:p),s=a.filter((v,i)=>a.indexOf(v)==i).sort()).join`
`

Demo

let f =

a=>a.map((t,i)=>(u='_',++i>9?'':' ')+i+u+u.repeat(x=(x=t>'9'?50:t.slice(2)*10-s[0].slice(2)*10)>50?50:x,p=s.indexOf(t)+1+`(${t})`).replace(u,'|')+(x<50?p:'')+u.repeat((x=50-p.length-x)>0?x:0)+(x>0?'':t>'9'?t:p),s=a.filter((v,i)=>a.indexOf(v)==i).sort()).join`
`

console.log(f([
  "1.13.4",
  "1.13.0",
  "R",
  "1.12.7",
  "1.11.5",
  "1.13.4",
  "1.12.1",
  "1.17.9"
]));

console.log(f([
  "1.13.6",
  "1.13.8",
  "R",
  "1.15.9",
  "1.13.8",
  "R",
  "1.14.4",
  "1.15.6",
  "1.14.1",
  "1.13.9",
  "1.13.2",
  "1.14.3",
  "1.15.0"
]));

\$\endgroup\$
1
\$\begingroup\$

Python 2, 282 272 246 bytes

Similar to Arnauld the input is assumed to be already stripped from the starting number since it is implicit.

H=input().split("\n")
T=[float(c[2:])if c[2:]else 99for c in H]
P=[min(int(10*(t-min(T))),50)for t in T]
S=sorted(list(set(T)))
i=0
u="_"
for h,p,t in zip(H,P,T):i+=1;s=S.index(t)+1;print`i`+u+"|"*(s>1)+u*p+[h,"%d(%s)"%(s,h)][t<99]+u*(41-p+(s<2))
\$\endgroup\$

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.