| Bytes | Lang | Time | Link |
|---|---|---|---|
| 114 | JavaScript Node.js | 250213T110524Z | Weird Gl |
| nan | Scala 3 | 240930T115738Z | 138 Aspe |
| 013 | Jelly | 220213T085715Z | emanresu |
| 085 | R | 220214T141730Z | Kirill L |
| 016 | Vyxal | 220213T090319Z | emanresu |
| 076 | Python 3 with numpy | 220213T113920Z | m90 |
| 091 | Python 3.8 | 220213T112621Z | m90 |
| 032 | Charcoal | 220213T084732Z | Neil |
JavaScript (Node.js), 114 bytes
f=(L,X,i=0,s=X.flat().length)=>i<s?(X=X.map(v=>v.slice(--v[0]<1&&0<++i)),"#".repeat(i/s*L+.5|0)+`
`+f(L,X,i,s)):""
Displays the evolution of the progress bar separated by newlines.
JavaScript (Node.js), 107 bytes
f=(L,X,t,i=0,s=X.flat().length)=>t<1?"#".repeat(i/s*L+.5|0):f(L,X.map(v=>v.slice(--v[0]<1&&0<++i)),t-1,i,s)
Displays the current state for a specific time.
Scala 3, 111 109 bytes
A Scala port of @m90's Python answer.
Saved 2 bytes thanks to @ceilingcat
Golfed version. Attempt This Online!
(L,X,t)=>{val f=X.map(_.scanLeft(0)(_+_).tail).flatten;"#"*Math.round(L*f.count(_<=t).toDouble/f.size).toInt}
Ungolfed version. Attempt This Online!
import scala.util.Random
object Main {
def calculateHash(L: Int, X: List[List[Int]], t: Int): String = {
val cumulativeSums = X.map(_.scanLeft(0)(_ + _).tail) // Calculate cumulative sums
val flattenedSums = cumulativeSums.flatten // Flatten the list of lists
val meanValue = flattenedSums.count(_ <= t).toDouble / flattenedSums.size // Calculate mean value
"#" * (Math.round(L * meanValue).toInt) // Return the hash string
}
def main(args: Array[String]): Unit = {
for (i <- 1 to 10) {
println(calculateHash(20, List(List(2, 2, 2), List(3, 3, 3), List(10)), i))
}
}
}
Jelly, 13 bytes
ÄṬSÄ÷Ṁ$×+.ḞRG
A dyadic link that outputs a bar graph (sorta).
I'm quite proud of ÄṬSÄ which generates the numbers before stretching. I'm also somewhat annoyed that the code to stretch the numbers is six bytes. I feel like there's got to be a better way to do that, but I'm not sure what.
The below explanation uses [[1,1],[2,1],[3,2],[4,1]] and 16 as an example.
Ä Take the cumulative sums of each item
Generating a list of lists of indices at which another task segment completes.
For the example list, [[1,2],[2,3],[3,5],[4,5]]
Ṭ Take the untruth of each item
Turning each into a boolean list with 1s at the specified indices
Each task is now a boolean list where 1s represent that a task will be represented at that instant
For the example, [[1,1],[0,1,1],[0,0,1,0,1],[0,0,0,1,1]]
S Reduce the list by vectorised addition
The result becomes a single list where the number at each index
Is the number of tasks completed at that instant
[1,2,2,1,2]
Ä Take the cumulative sum, getting the total tasks completed at that instant
[1,3,5,6,8]
--$ Run what's next on the result of the above
÷ Float divide the list by...
Ṁ Its maximum
Producing a list of floats between 0 and 1
[.125,.375,.625,.875,1]
× Multiply the result of the above by the other input
+.Ċ Round the results.
[2,6,10,14,16]
RG Format into a grid
R, 85 bytes
\(L,x)barplot(sapply(max(u<-unlist(Map(cumsum,x))):1,\(i)(.5+mean(u<=i)*L)%/%1),ho=T)
Try it on rdrr.io! with graphical output, but with older and longer function syntax.
Vyxal, 16 bytes
ƛ¦Þǔ;∑¦Ḣ:G/*⌈×*⁋
Straightforward port of my Jelly answer, go see that explanation for a better idea of how it works. Jelly has some nicer builtins.
ƛ ; # Map each task to...
¦ # Cumulative sums (instants where a step will complete)
Þǔ # Untruth (a boolean list with 1s at those indices)
∑ # Reduce the whole thing by addition
¦Ḣ # Get cumulative sums and remove the leading zero
:G/ # Divide by the maximum
* # Multiply by the input
⌈ # Get the ceiling
×*⁋ # Make a bar graph
Python 3 with numpy, 76 bytes
lambda L,X,t:int(L*mean(t>=hstack(map(cumsum,X)))+.5)*'#'
from numpy import*
cumsum transforms a list of step lengths into an array of the steps' finishing times, and hstack combines those arrays into one long array. The comparison produces 1 for finished steps and 0 for unfinished steps, and then mean gives the proportion of finished steps.
Python 3.8, 91 bytes
lambda L,X,t:int(sum((s:=0)+sum(t>=(s:=s+y)for y in x)for x in X)*L/sum(map(len,X))+.5)*'#'
Charcoal, 32 bytes
E⌈EηΣι⁺·⁵∕×ΣEηΣE묛Ӆλ⊕ξ⊕ιθΣEηLλ
Try it online! Link is to verbose version of code. Outputs a bar graph. Explanation:
η List of tasks
E Map over elements
ι Current list of subtasks
Σ Take the sum
⌈ Take the maximum
E Map over implicit range
η List of tasks
E Map over elements
λ Current list of subtasks
E Map over elements
λ Current list of subtasks
… Truncated to length
ξ Innermost index
⊕ Incremented
Σ Take the sum
¬› Is less then or equal to
ι Outer value
⊕ Incremented
Σ Take the sum
Σ Take the sum
× Multiplied by
θ Desired maximum length
∕ Divided by
η List of tasks
E Map over elements
λ Current list of subtasks
L Take the length
Σ Take the sum
⁺ Plus
·⁵ Literal number `0.5`
Implicitly print as bar graph
35 bytes for a version that outputs in real time:
RFφ⌈EηΣιP⁺·⁵∕×ΣEηΣE꬛Ӆκ⊕ν⊕ιθΣEηLκ