Uncertainty in the Timeline of a Day

12

Suppose your alarm wakes you up one morning, but you hit snooze so you can sleep for 8 more minutes. When it rings again you grudgingly get up and take a shower, which you estimate takes 15 to 17 minutes. You then brush your teeth for exactly 2 minutes, and get dressed, which takes about 3 to 5 minutes. Finally, you eat a hurried breakfast in 6 to 8 minutes and run out the door.

We can denote this timing sequence as 8 15-17 2 3-5 6-8.

Given the uncertainty of your morning's routine, what is the probability you were doing each task at some particular number of minutes since you first woke up?

Assuming every task takes a whole number of minutes, we can chart every possible combination of uncertain time spans (e.g. 3, 4, and 5 minutes for brushing teeth). This chart shows all 27 possibilities, with time increasing to the right, and each task of N minutes represented by (N - 1) dashes and one vertical bar, just to mark its ending. The minute boundaries occur between characters, so the space between the 8 and 9 column is 8 min 59 sec turning into 9 min.

         1111111111222222222233333333334
1234567890123456789012345678901234567890  <-- Minute
-------|--------------|-|--|-----|
-------|--------------|-|--|------|
-------|--------------|-|--|-------|
-------|--------------|-|---|-----|
-------|--------------|-|---|------|
-------|--------------|-|---|-------|
-------|--------------|-|----|-----|
-------|--------------|-|----|------|
-------|--------------|-|----|-------|
-------|---------------|-|--|-----|
-------|---------------|-|--|------|
-------|---------------|-|--|-------|
-------|---------------|-|---|-----|
-------|---------------|-|---|------|
-------|---------------|-|---|-------|
-------|---------------|-|----|-----|
-------|---------------|-|----|------|
-------|---------------|-|----|-------|
-------|----------------|-|--|-----|
-------|----------------|-|--|------|
-------|----------------|-|--|-------|
-------|----------------|-|---|-----|
-------|----------------|-|---|------|
-------|----------------|-|---|-------|
-------|----------------|-|----|-----|
-------|----------------|-|----|------|
-------|----------------|-|----|-------|
1234567891111111111222222222233333333334  <-- Minute
         0123456789012345678901234567890

It is clear that the routine could have taken 40 minutes at most and 34 minutes at least.

The question is, at a particular minute, say minute 29, what is the chance you were doing each of the 5 tasks? Assume each uncertain time frame is uniformly distributed over the exact whole minutes. So a 4-7 task has 25% chance of taking 4, 5, 6, or 7 minutes.

From the chart it can be seen that at minute 29 there was a...

0/27 chance you were snoozing (task 1)
0/27 chance you were showering (task 2)
0/27 chance you were brushing (task 3)
24/27 chance you were dressing (task 4)
3/27 chance you were eating (task 5)

Similarly at minute 1 there was a 27/27 chance you were snoozing with 0/27 everywhere else.

At minute 38 for example, 17 of the potential routines have already ended. So in 10 out of 10 cases you will be eating. This means the probabilities look like

0/10 task 1, 0/10 task 2, 0/10 task 3, 0/10 task 4, 10/10 task 5

Challenge

Write a function that takes an integer for the minute value, and a string consisting of a sequence of single integers, or pairs of integers a-b with b > a, all separated by spaces (just like 8 15-17 2 3-5 6-8). All the integers are positive. The input minute will be less than or equal to the maximum time possible (40 in example).

The function should return another string denoting the unreduced fractional chance of being in each task at the given minute.

Examples

  • myfunc(29, "8 15-17 2 3-5 6-8") returns the string 0/27 0/27 0/27 24/27 3/27
  • myfunc(1, "8 15-17 2 3-5 6-8") returns the string 27/27 0/27 0/27 0/27 0/27
  • myfunc(38, "8 15-17 2 3-5 6-8") returns the string 0/10 0/10 0/10 0/10 10/10
  • myfunc(40, "8 15-17 2 3-5 6-8") returns the string 0/1 0/1 0/1 0/1 1/1

If your language does not have strings or functions you may use named variables, stdin/stdout, the command line, or whatever seems most appropriate.

Scoring

This is code golf. The shortest solution in bytes wins.

Calvin's Hobbies

Posted 2014-09-26T10:46:00.033

Reputation: 84 000

The question does not specify a particular probability distribution for the time spent on each task. Should it be normally distributed? Can I assume any distribution I want? – feersum – 2014-09-26T10:51:40.990

1@Calvin that's not a normal distribution. Maybe you wanted to have a uniform distribution? – feersum – 2014-09-26T11:01:05.953

Does each task include the left |, the right |, or half of each? – Peter Taylor – 2014-09-26T11:01:31.290

All mentioned issues fixed in question. Any other problems? – Calvin's Hobbies – 2014-09-26T11:39:21.293

1what happens if there is a chance that no task was happening? – proud haskeller – 2014-09-26T14:36:31.450

@proud the last two examples cover that case – Martin Ender – 2014-09-26T20:50:18.317

Do I have to use a function just because my language has them? A program using STDIN or command-linearguments would suit me much better... – Dennis – 2014-09-27T05:01:29.390

@Dennis Ah, I see. I'd prefer you to use functions. – Calvin's Hobbies – 2014-09-27T05:02:50.167

Answers

3

CJam, 124 115 100 92 89 bytes

This can be golfed a lot, but I have to sleep, so posting now itself :)

l~\:N;S/{'-/2*2<~i),\i>}%_{m*{(\+}%}*{[0\{1$+}*]}%:B;,,{0B{I>2<~N<!\N<*+}/}fI]_:+m*'/f*S*

Try it online here

Input is like:

29 "8 15-17 2 3-5 6-8"

Where first integer is the input minute and the second string is the time range sequence (as shown in examples in the question, just without the ,)

Output for the above mentioned input:

0/27 0/27 0/27 24/27 3/27

Optimizer

Posted 2014-09-26T10:46:00.033

Reputation: 25 836

I'll accept this if you can get it to follow the updated rules. – Calvin's Hobbies – 2014-10-26T03:37:57.900

All the other examples give 0/27's. – Calvin's Hobbies – 2014-10-26T07:31:19.187

Now it's a bunch of `0/0's. – Calvin's Hobbies – 2014-10-26T09:23:09.360

@Calvin'sHobbies Let's take it to chat : http://chat.stackexchange.com/rooms/18161/room-for-optimizer-and-calvins-hobbies

– Optimizer – 2014-10-26T10:28:49.173

Never mind, sorry, I was just giving the input wrong. – Calvin's Hobbies – 2014-10-26T20:06:26.197

3

Mathematica, 237 216 bytes

I'm sure I can shorten this a bit, but not now. At least I finally got to use the new associations from Mathematica 10! :)

f=(j=#;s=StringSplit;r=ToString;t=Lookup[Counts@Flatten[FirstPosition[#,n_/;n>=j]&/@Accumulate/@Tuples@i],#,0]&/@Range@Length[i=ToExpression[#~s~"-"&/@s@#2]/.{a_,b_}:>a~Range~b];Riffle[r@#<>"/"<>r@Tr@t&/@t," "]<>"")&

Ungolfed:

    f = (
   j = #;
   s = StringSplit;
   r = ToString;
   t = Lookup[
       Counts@Flatten[
         FirstPosition[#, n_ /; n >= j] & /@ 
          Accumulate /@ Tuples@i], #, 0] & /@ 
     Range@Length[
       i = ToExpression[#~s~"-" & /@ s@#2] /. {a_, b_} :> a~Range~b];
   Riffle[r@# <> "/" <> r@Tr@t & /@ t, " "] <> "") &

Usage as specified in the challenge:

f[29, "8 15-17 2 3-5 6-8"]

It returns 0/1 for all elements if the first input is larger than the maximum time span.

Martin Ender

Posted 2014-09-26T10:46:00.033

Reputation: 184 808

I think Cases[] is not necessary given how Tuples works. If so, then t = Lookup[Counts[Join @@(FirstPosition[#, n_ /; n >= j] & /@ Accumulate /@ Tuples@i)], #, 0]. – DavidC – 2014-09-26T22:06:56.210

Lookup and Counts are welcome additions to the language. – DavidC – 2014-09-26T22:56:43.193

@DavidCarraher Thanks, but I had to switch to Flatten (instead of Join@@) because FirstPosition can now return Missing[NotFound] which can't be joined. – Martin Ender – 2014-09-28T14:41:04.737

1

Haskell, 232

f=(\(a,b)->[a..fst$head$reads(tail$b++" ")++[(a,b)]]).head.reads
n%l=(tail>>=zipWith(-))(0:map(\i->drop i&l*e[x|x<-map sum$mapM f$take i$w l,x>=n])[1..e$w l])>>=(++'/':show(id&l)++" ").show
(&)i=product.map(e.f).i.w
w=words
e=length

run like this:

*Main> putStrLn $ 1 % "8 15-17 2 3-5 6-8"
27/27 0/27 0/27 0/27 0/27 

proud haskeller

Posted 2014-09-26T10:46:00.033

Reputation: 5 866

1

APL, 162

{{⍵,'/',y}¨⌊|-2-/0,(y←+/,⍺≤⊃⌽x)×1,⍨¯1↓⍺{+/÷∘⍴⍨⍺≤,⍵}¨x←∘.+\{⊃{⍺,⍺↓⍳⍵}/⍎('-'⎕R' ')⍵}¨('\S+'⎕S'\0')⍵}

Example runs

      f←{{⍵,'/',y}¨⌊|-2-/0,(y←+/,⍺≤⊃⌽x)×1,⍨¯1↓⍺{+/÷∘⍴⍨⍺≤,⍵}¨x←∘.+\{⊃{⍺,⍺↓⍳⍵}/⍎('-'⎕R' ')⍵}¨('\S+'⎕S'\0')⍵}
      29 f '8 15-17 2 3-5 6-8'
 0 / 27  0 / 27  0 / 27  24 / 27  3 / 27 

      1 f '8 15-17 2 3-5 6-8'
 27 / 27  0 / 27  0 / 27  0 / 27  0 / 27 

      38 f '8 15-17 2 3-5 6-8'
 0 / 10  0 / 10  0 / 10  0 / 10  10 / 10 

      40 f '8 15-17 2 3-5 6-8'
 0 / 1  0 / 1  0 / 1  0 / 1  1 / 1

I hope you don't mind the weird spacing

TwiNight

Posted 2014-09-26T10:46:00.033

Reputation: 4 187

This is 98 bytes only. APL has their own codepage such that all of their symbols fit in the ASCII range. – Optimizer – 2014-12-08T11:00:05.290