| Bytes | Lang | Time | Link |
|---|---|---|---|
| 013 | Haskell + hgl | 250720T225122Z | Wheat Wi |
| 107 | Nim | 250719T143514Z | janAkali |
| 008 | Japt | 250716T150152Z | Shaggy |
| 135 | C64 Basic | 250719T102241Z | OSI8 |
| 004 | Uiua | 250717T220853Z | nyxbird |
| 121 | Clojure | 250717T170818Z | NikoNyrh |
| 086 | Python3 | 250716T145047Z | Ajax1234 |
| 007 | 05AB1E | 250715T131923Z | Kevin Cr |
| 017 | Charcoal | 250715T234540Z | Neil |
| 005 | Jelly | 250715T194911Z | Jonathan |
| 054 | JavaScript ES11 | 250715T120653Z | Arnauld |
| 068 | JavaScript | 250715T095311Z | tata |
Haskell + hgl, 13 bytes
uef nb$df*^nb
Explanation
nbgets all the distinct elements of a list (basically which cars are still in the race)df*^nbsubtracts the result ofnbfrom the total list. i.e. it removes one lap.uefiterates the above two functions until the list is empty and collects the results of thenbs.
21 bytes
uue$l2m(rv<<nx<rv)nbr
Explanation
Two functions uue and nbr almost solve this task. uue is an "unfold", basically a variant of uef from the first solution. It takes a function which breaks a list apart and repeats it until the input is empty collecting the pieces.
nbr takes the first occurrence of every element of the list out, however it reorders the remaining elements. So instead of doing what we want uue nbr just sort of repeats the first lap a couple of times, eliminating cars if they don't finish a lap.
So we have to fix this ordering problem, and we end up paying quite a bit to do this. What I do is take the intersection of the remainder with the reverse of the original list. This takes the order of the reversed original list and imposes it on the fragment. Then I reverse the result.
This is done with this monstrosity: rv<<nx<rv
Reflection
I think this is a really good challenge. I'm happy enough with the first answer, but I'm pretty unhappy with the second. hgl is really close to being really short with just uue nbr, but it fumbles it. I'm even disappointed in how many bytes it takes to fix the issue in the second answer.
So I have a couple of things I think could be improved:
- I sort of understand why
nbris the way it is. But I ultimately think it is a poor choice. Implementing it the way that this challenge needs would be useful, and probably would be a faster function. But instead of changing the behavior, since it's a 3 byte built in, I think I should just add an alternate version. - If I'm doing that I might as well add
df*^nbas a function. It wouldn't save bytes here, but might as well. - I spend too much on
rv<<nx<rv. This is extremely close to the "sandwich" operationswc. There should be a version of the sandwich operation for binary functions, something likesw2. This would be more analogousrv<<(nx.*rv)but both work for shorter. It would only save bytes ifsw2was given an infix as well.
Nim, 112 109 107 bytes
proc(s= @[1]):auto=(var l:seq[s.type];for c in s:(var i=0;while(l.grow(i+1,@[]);c in l[i]):i+=1
l[i]&=c)
l)
Try it in Wandbox! (with added let f= header)
Ungolfed:
proc raceOrder(cars = @[1]): auto =
var laps: seq[cars.type]
for car in cars:
var i=0
while(laps.grow(i+1,@[]); car in laps[i]):
i+=1
laps[i] &= car
return laps
Japt, 8 bytes
I tried this initially and it didn't work; Nyxbird's solution inspired me to try it again and it worked this time 🤷🏼♂️
ü@¯Y è¶X
ü@¯Y è¶X :Implicit input of array U
ü :Group by
@ :Passing each X at index Y through the following function
¯Y : Slice U to index Y
è : Count the elements
¶X : Equal to X
Original, 10 bytes
Was stuck on 12 for ages, with the possibility of 10 only dawning on me just before I posted. If outputting a 3D-array were allowed then the last 3 bytes could be replaced with o.
í üÎÕËñÌmÎ
í üÎÕËñÌmÎ :Implicit input of array
í :Interleave with indices
ü :Group & sort by
Î : First element (car)
Õ :Transpose
Ë :Map
ñ : Sort by
Ì : Last element
m : Map to
Î : First elements
C64 Basic, 135 bytes
The code has 135 bytes without counting the DATA lines which may be different depending on the test case.
Additional limitations:
- car numbers can only be integer values up to 255
- the total number of cars cannot exceed 28
Uiua, 4 bytes
⊕□⊸⧆
⊕ group into □ boxes ⊸ by ⧆ occurences.
⧆ occurences gives the number of times each element of an array has appeared (up to the current index), so ⧆[1 2 1 3 1 2] is [0 0 1 0 2 1].
Clojure, 121 bytes
#(map reverse(vals(second(reduce(fn[[R P]c](let[r(+(or(R c)0)1)][(assoc R c r)(update P r conj c)]))[{}(sorted-map)]%))))
Python3, 86 bytes
def f(c):
while c:
l=[[],[]]
for i in c:l[i not in l[1]]+=[i]
yield l[1];c=l[0]
05AB1E, 17 11 7 bytes
.¡IN£s¢
-6 bytes porting JonathanAllan's Jelly answer (but without convenient Ġ builtin).
-4 bytes thanks to @Neil
Try it online or verify all test cases.
Original 17 bytes answer:
vÐÙþkDŠõsǝ}\¯)Ù¨è
Try it online or verify all test cases.
Explanation:
.¡ # Group the values in the (implicit) input-list by:
I # Push the input-list
N # Push the 0-based group_by-index
£ # Leave that many leading items from the input-list
s # Swap so the current value is at the top of the stack
¢ # Count how many times it occurs in this input-list prefix
# (after which the list of lists of values is output implicitly)
v # Loop the (implicit) input-size amount of times:
Ð # Triplicate the current list
# (which will be the implicit input-list in the first iteration)
Ù # Uniquify the top copy
þ # Remove potential empty strings by only keeping numbers
k # Get all (0-based) indices of these values in the duplicated list
D # Duplicate that list of indices
Š # Triple-swap to make the order indices,list,indices
õsǝ # Insert empty strings at those indices in the list,
# to mark the current lap as done
}\ # After the loop: discard the top list of empty strings
¯ # Push an empty list (work-around for single-lap input-lists)
) # Wrap all other lists of indices into a list
Ù¨ # Uniquify and remove the last item to remove all trailing empty lists
è # Index each inner index into the (implicit) input-list
# (after which the list of lists of values is output implicitly)
Charcoal, 17 bytes
IE⌈Eθ№θιΦθ⁼ι№…θμλ
Try it online! Link is to verbose version of code. Explanation:
θ Input array
E Map over elements
№ Count of
ι Current element
θ In input array
⌈ Take the maximum
E Map over implicit range
θ Input array
Φ Filtered where
№ Count of
λ Inner element in
θ Input array
… Truncated to length
μ Inner index
⁼ Equals
ι Outer value
I Cast to string
Implicitly print
Jelly, 5 bytes
ĠZṢ€ị
A monadic Link that accepts a list of car numbers in lap marker observed race order and yields a list of lists of car numbers at progressive laps, each in race order.
How?
ĠZṢ€ị - Link: List, Cars e.g. [7, 1, 3, 7, 3, 7, 1, 3, 3]
Ġ - group the indices of {Cars} by their respective values
-> [[2, 7], [3, 5, 8, 9], [1, 4, 6]]
Z - transpose -> [[2, 3, 1], [7, 5, 4], [8, 6], [9]]
Ṣ€ - sort each -> [[1, 2, 3], [4, 5, 7], [6, 8], [9]]
ị - index into {Cars} -> [[7, 1, 3], [7, 3, 1], [7, 3], [3]]
JavaScript (ES11), 54 bytes
a=>a.map(p=n=>(o[p[n]=p[n]+1|0]||=[]).push(n),o=[])&&o
JavaScript, 68 bytes
a=>a.map(c=>o.find(r=>!r.includes(c))?.push(c)??o.push([c]),o=[])&&o
f=
a=>a.map(c=>o.find(r=>!r.includes(c))?.push(c)??o.push([c]),o=[])&&o
console.log(JSON.stringify(f([10,20,30,10,20,30,10,20,30])))
console.log(JSON.stringify(f([10,20,30,20,10,30,20,10])))
console.log(JSON.stringify(f([10,20,30,10,20,10,30,20,30])))
console.log(JSON.stringify(f([10,20,30,10,20,30,10,20,10,30,20,10,20,30])))

