Help me manage my time

15

1

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

GamrCorps

Posted 2015-11-17T16:40:54.553

Reputation: 7 058

The input in the example should be `9/17/2015 9/27/2015 1-15 Tu' because 9/22 is a Tuesday. – DavidC – 2015-11-17T21:23:09.663

@DavidCarraher you are right, when I made that sample input I was thinking of November for some reason. – GamrCorps – 2015-11-17T21:36:58.057

7If it was me the last date would have all the chapters :) – MickyT – 2015-11-17T23:29:01.193

@MickyT precisely my inspiration for this challenge. – GamrCorps – 2015-11-18T03:05:48.580

You'll soon discover how much amazing physics is. You're lucky actually. – Fabrizio Calderan – 2015-11-23T15:21:56.277

Answers

2

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

AdmBorkBork

Posted 2015-11-17T16:40:54.553

Reputation: 41 581

3There are so many dollar signs here...This must be expensive! :D – kirbyfan64sos – 2015-11-18T21:01:19.150

@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). – AdmBorkBork – 2015-11-18T21:11:55.473

Over to you again :-) – Willem – 2015-11-21T10:16:14.217

Hmm 308 for mine as well ... – Willem – 2015-11-23T20:52:12.187

Good job on 305! 300 now :-) – Willem – 2015-11-24T17:57:40.377

@willem Using explicit input formatting to expect the input variables to be in .NET DateTime format golfs another 28 bytes. Down to 277 now. – AdmBorkBork – 2015-11-24T19:41:56.220

Haha, do you think we can move the whole code to the input ? – Willem – 2015-11-25T04:25:29.173

@willem $args[0] 8 Bytes. Expects input to match desired output. :D – AdmBorkBork – 2015-11-25T13:31:35.677

Can inline the creation of $e to save 6 bytes – Veskah – 2019-06-24T19:33:56.687

Well I'll be damned, join is redundant here as well – Veskah – 2019-06-24T19:52:37.733

3

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`
`
}

user81655

Posted 2015-11-17T16:40:54.553

Reputation: 10 181

2

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.]

Willem

Posted 2015-11-17T16:40:54.553

Reputation: 1 528