13
\$\begingroup\$

Consider a date formatted in YYYY-MM-DD. You can use the joker * at the end of the date string. E.g. 2016-07-2* describes all the dates from 2016-07-20 to 2016-07-29.

Now, consider a period represented by a start date and an end date.

The algorithm must find the smallest possible list of dates representing the period.

Let's use an exemple. For the following period:

  • start date: 2014-11-29
  • end date: 2016-10-13

The algorithm must return an array containing the following list of dates:

  • 2014-11-29
  • 2014-11-30
  • 2014-12-*
  • 2015-*
  • 2016-0*
  • 2016-10-0*
  • 2016-10-10
  • 2016-10-11
  • 2016-10-12
  • 2016-10-13
\$\endgroup\$
7
  • 1
    \$\begingroup\$ The winning objective(s) are ambiguous. I suggest changing it to codegolf (i.e. solve the task in the fewest bytes possible) and always require answers to output the smallest list of dates. \$\endgroup\$
    – Billywob
    Commented Nov 9, 2016 at 10:22
  • \$\begingroup\$ Maybe it's simply not the best site for this, it is more a question (i.e. "I can't find this algorithm in Ruby on Rails") than a challenge "for fun". I'm rather new here... \$\endgroup\$
    – Raphael
    Commented Nov 9, 2016 at 10:28
  • \$\begingroup\$ I suggest you try stackoverflow and then ask it as a question rather than a challenge. If rephrased, the challenge is still interesting in my opinion and deserves a chance (as code golf). \$\endgroup\$
    – Billywob
    Commented Nov 9, 2016 at 10:36
  • 6
    \$\begingroup\$ Yeah, I think finding the algorithm is still fun and challenging, but I was not interested in the code-golf side... Well I rephrased it, changed the tags, and now I'm gonna ask stackoverflow while still following this post. Thanks! \$\endgroup\$
    – Raphael
    Commented Nov 9, 2016 at 10:39
  • 1
    \$\begingroup\$ Follow-up: I posted on Stackoverflow yesterday, but today I had a working code in Ruby (it doesn't "jokerize" months, but almost there): stackoverflow.com/questions/40506639/… \$\endgroup\$
    – Raphael
    Commented Nov 10, 2016 at 17:19

1 Answer 1

1
\$\begingroup\$

PHP, 541 343 bytes

I wanted to get the algorithm working in the first place; but golfing it down was actually far more fun than I expected (especially browsing the supported date & time formats).

Three major actions saved about 130 bytes; but the 70 bytes from minor golfings
(which also rendered one of the large steps obsolete) carried a lot of fun.

for($a=($f=strtotime)($argv[1]);!$p=$a>$z=$f($argv[2]);$a+=86400){$x=$z<$e=$f(Dec31,$a);(101<$q=date(md,$a))?$q-1001|$x?:$a=$e+$p="1*":($x?($t=$f(IX30,$a))>$z?:$a=$t+$p="0*":$a=$e+$p="*");$p?:($q%100>1|$z<($t=$f(date(Ymt,$a)))?$q%10>0&$q%100>1|$z<($t=min($t,$a+777600))?:$a=$t+$p="m-$q[2]*":$a=$t+$p="m-*");echo date("Y-".($p?:"m-d"),$a),"
";}

takes input from command line arguments. Run with -nror test it online.

notes

  • prints Y-m-3* for Y-m-30; add 7 bytes to fix: Insert |$a==$t after 777600)).
  • throws warnings in PHP 7.1; add 5 bytes to fix: Replace +$p with +!$p.
  • A breakdown and some golfings explained are ready to be posted;
    but I´ll wait a bit to see if someone else submits before I spoil.
\$\endgroup\$

Your Answer

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

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