12
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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? \$\endgroup\$
    – feersum
    Sep 26, 2014 at 10:51
  • 1
    \$\begingroup\$ @Calvin that's not a normal distribution. Maybe you wanted to have a uniform distribution? \$\endgroup\$
    – feersum
    Sep 26, 2014 at 11:01
  • \$\begingroup\$ Does each task include the left |, the right |, or half of each? \$\endgroup\$ Sep 26, 2014 at 11:01
  • \$\begingroup\$ All mentioned issues fixed in question. Any other problems? \$\endgroup\$ Sep 26, 2014 at 11:39
  • 1
    \$\begingroup\$ what happens if there is a chance that no task was happening? \$\endgroup\$ Sep 26, 2014 at 14:36

4 Answers 4

3
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ I'll accept this if you can get it to follow the updated rules. \$\endgroup\$ Oct 26, 2014 at 3:37
  • \$\begingroup\$ All the other examples give 0/27's. \$\endgroup\$ Oct 26, 2014 at 7:31
  • \$\begingroup\$ Now it's a bunch of `0/0's. \$\endgroup\$ Oct 26, 2014 at 9:23
  • \$\begingroup\$ @Calvin'sHobbies Let's take it to chat : chat.stackexchange.com/rooms/18161/… \$\endgroup\$
    – Optimizer
    Oct 26, 2014 at 10:28
  • \$\begingroup\$ Never mind, sorry, I was just giving the input wrong. \$\endgroup\$ Oct 26, 2014 at 20:06
3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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]. \$\endgroup\$
    – DavidC
    Sep 26, 2014 at 22:06
  • \$\begingroup\$ Lookup and Counts are welcome additions to the language. \$\endgroup\$
    – DavidC
    Sep 26, 2014 at 22:56
  • \$\begingroup\$ @DavidCarraher Thanks, but I had to switch to Flatten (instead of Join@@) because FirstPosition can now return Missing[NotFound] which can't be joined. \$\endgroup\$ Sep 28, 2014 at 14:41
1
\$\begingroup\$

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 
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ This is 98 bytes only. APL has their own codepage such that all of their symbols fit in the ASCII range. \$\endgroup\$
    – Optimizer
    Dec 8, 2014 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.