| Bytes | Lang | Time | Link |
|---|---|---|---|
| 029 | Dyalog APL | 251006T013748Z | Aaron |
| 103 | Kotlin 1.1 | 170909T203543Z | jrtapsel |
| 013 | Jelly | 170907T181257Z | miles |
| 016 | Husk | 170907T192015Z | H.PWiz |
| 016 | Jelly | 170907T181345Z | Jonathan |
| 057 | Mathematica | 170907T183054Z | JungHwan |
| nan | Perl 5 | 170907T185136Z | Xcali |
| 109 | Python 3 | 170907T172334Z | anon |
Dyalog APL, 29 bytes
{+⌿↑⍵{∊⍵⍺⍴¨0 1}¨0,+\2(0⌈-)/⍵}
(0⌈-) # Find the max between zero and the difference between the left and right args
2 / # applied between each pair of elements
⍵ # of the input
+\ # Sum-scan
0, # Prepend a zero as the first log cannot be offset
⍵{ }¨ # For-each element of the input and the previously generated set of differences
⍵⍺⍴¨0 1 # Take right-many zeroes and left-many ones
∊ # Enlist (flatten)
↑ # Mix into a matrix
+⌿ # Sum down the columns
💎
Created with the help of Luminespire.
Kotlin 1.1, 113 103 bytes
{var l=0
var r=0
it.flatMap{l=maxOf(l,r-it+1)
r=l+it-1
(l..r).toList()}.groupBy{it}.map{it.value.size}}
Beautified
{
// Current row leftmost value
var l = 0
// Current row rightmost value
var r = 0
// For each log
it.flatMap {
// Work out the new leftmost point
l = maxOf(
l,
r - it+1)
// Use it to work out the new rightmost point
r = l + it-1
// Record the used columns
(l..r).toList()}
// Group the column numbers together
.groupBy { it }
// Count the amount of times each column is used
// Put the results into a list
.map { it.value.size }
}
Test
var z:(List<Int>)->List<Int> =
{var l=0
var r=0
it.flatMap{l=maxOf(l,r-it+1)
r=l+it-1
(l..r).toList()}.groupBy{it}.map{it.value.size}}
fun main(args: Array<String>) {
println(z(listOf(5, 8, 6, 2, 4, 3, 6, 2)))
println(listOf(2,2,3,3,3,2,4,6,3,3,1,2,2))
}
Jelly, 19 13 bytes
IN0»0;+\+"RṬS
Saved 2 bytes thanks to @Jonathan Allan.
Explanation
IN0»0;+\+"RṬS Input: array A
I Increments
N Negate
0» Max with 0
0; Prepend 0
+\ Cumulative sum
+" Vectorized add with
R Range, vectorizes over each integer
Ṭ Create a list with 1's at the specified indices
S Sum
Husk, 16 bytes
Fż+Ṡzo`Ṙ↔ḋ2eo∫Ẋ<
Explanation
Ẋ For all adjacent pairs, x y
< return max(0,x-y)
o∫ Cumulative sum, with an extra 0 at the start
Ṡz Zip the input list and ^ with ...
e Make a two element list
↔ḋ2 The list [0,1]
o`Ṙ Repeat each in ^ by ^^
Fż+ Sum the columns
Jelly, 18 16 bytes
-2 bytes prompted by help from miles
Maybe there is a quicker way using mathematics rather than construction like this does?
IN0;»0+\0ẋ;"1ẋ$S
Try it online! or see the test-suite.
How?
IN0;»0+\0ẋ;"1ẋ$S - Link: list of positive integers, logLengths e.g. [4, 3, 3, 1, 4, 3]
I - incremental differences [-1, 0,-2, 3,-1]
N - negate (vectorises) [ 1, 0, 2,-3, 1]
0; - zero concatenated with that [0, 1, 0, 2,-3, 1]
»0 - maximum (vectorises) of that and zero [0, 1, 0, 2, 0, 1]
+\ - cumulative reduce with addition [0, 1, 1, 3, 3, 4]
0ẋ - zero repeated (vectorises) [[],[0],[0],[0,0,0],[0,0,0],[0,0,0,0]]
$ - last two links as a monad (right is implicitly logLengths):
1ẋ - one repeated [[1,1,1,1],[1,1,1],[1,1,1],[1],[1,1,1,1],[1,1,1]]
" - zip with:
; - concatenation
[[1,1,1,1],[0,1,1,1],[0,1,1,1],[0,0,0,1],[0,0,0,1,1,1,1],[0,0,0,0,1,1,1]]
... this is an upside-down version of the logs like those in the OP
with 0 for spaces and 1 for # with any right-hand-side spaces missing.
S - sum [1, 3, 3, 5, 2, 2, 2]
Mathematica, 76 60 58 57 bytes
#2&@@@Tally[l=j=0;##&@@(Range@#+(j+=Ramp[l-(l=#)]))&/@#]&
Perl 5, 64 + 1 (-a) = 65 bytes
for(@F){$e<$s+--$_?$e=$s+$_:($s=$e-$_);$_++for@r[$s..$e]}say"@r"
Python 3, 114 109 bytes
Edit: Saved 5 bytes thanks to @Mr.Xcoder
f=lambda x,i=1:x[i:]and x[i]>=x[~-i]and f(x,i+1)or[i]+f([q-(e<i)for e,q in enumerate(x)if(e<i)<q])if x else[]