Count the timespans
JavaScript (ES6), 85 bytes
Takes input as a list of Date
pairs. Expects the list to be sorted by starting date. Returns an array of integers.
f=(a,d=+a[0][0])=>[a.map(([a,b])=>n+=!(r|=d<b,d<a|d>b),r=n=0)|n,...r?f(a,d+864e5):[]]
Try it online!
or 84 bytes if we can take JS timestamps as input (as suggested by @Shaggy)
JavaScript, 75 73 bytes
Takes input as a sorted array of arrays of date primitive pairs, outputs an object where the keys are the primitives of each date and the values the counts of those dates in the ranges.
a=>a.map(g=([x,y])=>y<a[0][0]||g([x,y-864e5],o[y]=~~o[y]+(x<=y)),o={})&&o
Try it
I was working on this 60 byte version until it was confirmed that dates that don't appear in any of the ranges must be included so hastily updated it to the solution above.
a=>a.map(g=([x,y])=>x>y||g([x+864e5,y],o[x]=-~o[x]),o={})&&o
Try it online (or with human readable dates in the output)
Octave, 63 bytes
@(x)histc(t=[cellfun(@(c)c(1):c(2),x,'un',0){:}],min(t):max(t))
Try it online!
Now that was ugly!
Explanation:
Takes the input as a cell array of datenum
elements (i.e. a string "2001-01-01"
converted to a numeric value, looking like this:
{[d("2001-01-01") d("2001-01-01")]
[d("2001-01-01") d("2001-01-03")]
[d("2001-01-01") d("2001-01-02")]
[d("2001-01-03") d("2001-01-03")]
[d("2001-01-05") d("2001-01-05")]};
where d()
is the function datenum
. We then use cellfun
to create cells with the ranges from the first column to the second for each of those rows. We concatenate these ranges horizontally, so that we have a long horizontal vector with all the dates.
We then create a histogram using histc
of these values, with bins given by the range between the lowest and the highest date.