15
\$\begingroup\$

I have been recently told to read an entire physics textbook by the new year (true story, unfortunately). I need your help to determine what chapters I should read each day. This is where you come in.

Input

  • Two dates, in any format. The second date will always be later than the first.
  • A list of chapter numbers. This comma-separated list can contain single chapters (12) or inclusive ranges (1-3). Ex. 1-3,5,6,10-13.
  • A list of weekdays (represented by the first two letters of the name: Monday -> Mo) to exclude from the schedule. Ex. Mo,Tu,Fr.

Output

Output will be a newline-separated list of dates and chapter numbers (see format below). The chapters should be evenly distributed over all days in the range, excluding the weekdays provided. If the chapters do not distribute evenly, have the days with lower amounts of chapters at the end of the period of time. Dates in output can be in a different format than input. Days with no chapters can be ommited, or just have no chapters with it.

Example:

Input: 9/17/2015 9/27/2015 1-15 Tu

Output:

9/17/2015: 1 2
9/18/2015: 3 4
9/19/2015: 5 6
9/20/2015: 7 8
9/21/2015: 9 10
9/23/2015: 11
9/24/2015: 12
9/25/2015: 13
9/26/2015: 14
9/27/2015: 15
\$\endgroup\$
  • \$\begingroup\$ The input in the example should be `9/17/2015 9/27/2015 1-15 Tu' because 9/22 is a Tuesday. \$\endgroup\$ – DavidC Nov 17 '15 at 21:23
  • \$\begingroup\$ @DavidCarraher you are right, when I made that sample input I was thinking of November for some reason. \$\endgroup\$ – GamrCorps Nov 17 '15 at 21:36
  • 7
    \$\begingroup\$ If it was me the last date would have all the chapters :) \$\endgroup\$ – MickyT Nov 17 '15 at 23:29
  • \$\begingroup\$ @MickyT precisely my inspiration for this challenge. \$\endgroup\$ – GamrCorps Nov 18 '15 at 3:05
  • \$\begingroup\$ You'll soon discover how much amazing physics is. You're lucky actually. \$\endgroup\$ – fcalderan Nov 23 '15 at 15:21
2
\$\begingroup\$

PowerShell v4, 367 357 323 313 308 307 305 277 Bytes

param($a,$b,$c,$d)$e=@();$c=-split('('+($c-replace'-','..'-replace',','),(')+')'|iex|%{$_-join' '});while($a-le$b){if(-join"$($a.DayOfWeek)"[0,1]-notin$d){$e+=$a;$z++}$a=$a.AddDays(1)}$g=,0*$z;$c|%{$g[$c.IndexOf($_)%$z]++};1..$z|%{"$($e[$_-1]): "+$c[$i..($i+=$g[$_-1]-1)];$i++}

Edit - golfed 28 bytes by using explicit input formatting.

Explained:

param($a,$b,$c,$d)    # Parameters, takes our four inputs
$e=@()                # This is our array of valid output dates

$c=-split('('+($c-replace'-','..'-replace',','),(')+')'|iex|%{$_-join' '})
# Ridiculously complex way to turn the input chapters into an int array
# The first part changes "1,5-9,12" into a "(1),(5..9),(12)" format that
# PowerShell understands, then executes that with iex, which creates an 
# array of arrays. Then iterate through each inner array and joins them all
# together with spaces, then finally splits on spaces to create a 1D array

while($a-le$b){       # Until we reach the end day
  if(-join"$($a.DayOfWeek)"[0,1]-notin$d){
    # Not an excluded day of the week
    $e+=$a            # Add it to our list of days
    $z++              # Increment our count of total days
  }
  $a=$a.AddDays(1)    # Move to the next day in the range
}

$g=,0*$z              # Populate a new array with zeroes, same length as $e

$c|%{$g[$c.IndexOf($_)%$z]++}
# This populates $g for how many chapters we need each day

1..$z|%{"$($e[$_-1]): "+$c[$i..($i+=$g[$_-1]-1)];$i++}
# Goes through the days in $e, prints them, and slices $c based on $g

Usage

Expects the dates to be in .NET DateTime format. Expects the "skipped" days to be in an array (PowerShell equivalent of a list).

PS C:\Tools\Scripts\golfing> .\help-me-manage-my-time.ps1 (Get-Date '9/17/2015') (Get-Date '9/27/2015') '5,1-3,6,10-13,20-27' @('Su','Tu')
09/17/2015 00:00:00: 5 1 2
09/18/2015 00:00:00: 3 6
09/19/2015 00:00:00: 10 11
09/21/2015 00:00:00: 12 13
09/23/2015 00:00:00: 20 21
09/24/2015 00:00:00: 22 23
09/25/2015 00:00:00: 24 25
09/26/2015 00:00:00: 26 27
\$\endgroup\$
  • 3
    \$\begingroup\$ There are so many dollar signs here...This must be expensive! :D \$\endgroup\$ – kirbyfan64sos Nov 18 '15 at 21:01
  • \$\begingroup\$ @kirbyfan64sos Only 12% of the characters here are $ ... That's actually pretty average for PowerShell golfs, which seems to be around 10% to 15% (based on my own informal calculations of answers I've posted). \$\endgroup\$ – AdmBorkBork Nov 18 '15 at 21:11
  • \$\begingroup\$ Over to you again :-) \$\endgroup\$ – Willem Nov 21 '15 at 10:16
  • \$\begingroup\$ Hmm 308 for mine as well ... \$\endgroup\$ – Willem Nov 23 '15 at 20:52
  • \$\begingroup\$ Good job on 305! 300 now :-) \$\endgroup\$ – Willem Nov 24 '15 at 17:57
3
\$\begingroup\$

JavaScript (ES6), 317 310 291 bytes

(a,b,c,d)=>{u=0;c.split`,`.map(m=>{p=m[s]`-`;for(q=n=p[0];n<=(p[1]||q);r=++u)c+=","+n++},c="");c=c.split`,`;x=d.map(p=>"SuMoTuWeThFrSa".search(p)/2);for(g=[];a<b;a.setTime(+a+864e5))x.indexOf(a.getDay())<0&&(t=y=g.push(a+" "));return g.map(w=>w+c.slice(u-r+1,u-(r-=r/y--+.99|0)+1)).join`
`}

Usage

f(new Date("2015-09-17"),new Date("2015-09-27"),"5,1-4,6,10-13,20-27",["Su","Tu"])
=> "Thu Sep 17 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 5,1,2
Fri Sep 18 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 3,4,6
Sat Sep 19 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 10,11
Mon Sep 21 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 12,13
Wed Sep 23 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 20,21
Thu Sep 24 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 22,23
Fri Sep 25 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 24,25
Sat Sep 26 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 26,27"

Explanation

(a,b,c,d)=>{

  u=0;                                                 // u = total chapters
  c.split`,`.map(m=>{                                  // c = array of each chapter
    p=m[s]`-`;
    for(q=n=p[0];n<=(p[1]||q);r=++u)                   // get each chapter from ranges
      c+=","+n++
  },c="");
  c=c.split`,`;

  x=d.map(p=>"SuMoTuWeThFrSa".search(p)/2);            // x = days to skip
  for(g=[];a<b;a.setTime(+a+864e5))                    // for each day between a and b
    x.indexOf(a.getDay())<0&&                          // if this day is not skipped
      (t=y=g.push(a+" "));                             // add it to the list of days
                                                       // t = total days
                                                       // y = days remaining

  return g.map(w=>w+
    c.slice(u-r+1,u-(r-=r/y--+.99|0)+1)                // add the chapters of the day
  ).join`
`
}
\$\endgroup\$
2
\$\begingroup\$

Python 2 - 338 317 308 304 300

Here we go to get the ball rolling ...

def f(a,b,c,d):
 from pandas import*;import numpy as n
 s=str.split;e=n.array([])
 for g in s(c,','):h=s(g,'-');e=n.append(e,range(int(h[0]),int(h[-1])+1))
 k=[t for t in date_range(a,b) if s('Mo Tu We Th Fr Sa Su')[t.weekday()]not in d];j=len(k);e=array_split(e,j)
 for u in range(j):print k[u],e[u]

Example input:

f('9/17/2015','9/27/2015','5,1-3,6,10-13,20-27',['Su','Tu'])

Example Output:

2015-09-17 00:00:00 [ 5.  1.  2.]
2015-09-18 00:00:00 [ 3.  6.]
2015-09-19 00:00:00 [ 10.  11.]
2015-09-21 00:00:00 [ 12.  13.]
2015-09-23 00:00:00 [ 20.  21.]
2015-09-24 00:00:00 [ 22.  23.]
2015-09-25 00:00:00 [ 24.  25.]
2015-09-26 00:00:00 [ 26.  27.]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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