23
\$\begingroup\$

The solar year is 365 days, 5 hours, 48 minutes, 45 seconds, and 138 milliseconds, according to this video. With the current Gregorian calendar, the rules for leap years are as follows:

if      year is divisible by 400, LEAP YEAR
else if year is divisible by 100, COMMON YEAR
else if year is divisible by 4,   LEAP YEAR
else,                             COMMON YEAR

Unfortunately, this method is off by one day every 3216 years.

One possible method of reforming the calendar is the following rule:

if      year is divisible by 128, COMMON YEAR
else if year is divisible by 4,   LEAP YEAR
else,                             COMMON YEAR

This has the benefit of not requiring us to change our calendars again for another 625,000 years, give or take.

Say the entire world decides that, starting now, we use this system of every fourth year is a leap year except every 128th year, changing our calendars as follows:

YEAR    GREGORIAN    128-YEAR
2044    LEAP         LEAP
2048    LEAP         COMMON
2052    LEAP         LEAP
 ...
2096    LEAP         LEAP
2100    COMMON       LEAP
2104    LEAP         LEAP
 ...
2296    LEAP         LEAP
2300    COMMON       LEAP
2304    LEAP         COMMON
2308    LEAP         LEAP

How would this affect our day of the week algorithms?

The challenge

  • Given a date from the year 2000 to the year 100000, find the day of the week under this new calendar.
  • Any input and output format is allowed as long as you clearly specify which formats you are using.
  • This is code golf so try to make your solutions as golfy as possible!

Test cases

"28 February 2048" -> "Friday"
"March 1, 2048"    -> "Sat"
(2100, 2, 29)      -> 0           # 0-indexed with Sunday as 0
"2100-02-29"       -> 7           # 1-indexed with Sunday as 7
"28 Feb. 2176"     -> "Wednesday"
"1-Mar-2176"       -> "Th"
"28/02/100000"     -> "F"         # DD/MM/YYYYYY
"Feb. 29, 100000"  -> 6           # 1-indexed with Sunday as 7
"03/01/100000"     -> 1          # MM/DD/YYYYYY and 1-indexed with Sunday as 1

Suggestions and feedback on the challenge are welcome. Good luck and good golfing!

\$\endgroup\$
  • \$\begingroup\$ For test case #4, you mean 1-indexed, right? Otherwise there would have to be 8 days in that week. \$\endgroup\$ – Sebastian Nov 30 '17 at 20:37
  • \$\begingroup\$ Also, you say "good golfing", so is this a #code-golf challenge? If so, put in the winning criteria (e.g. lowest number of bytes/characters) and add that as a tag. \$\endgroup\$ – Sebastian Nov 30 '17 at 20:40
  • \$\begingroup\$ @Sebastian You are correct on both counts. I've already edited the challenge. Thank you for your feedback \$\endgroup\$ – Sherlock9 Nov 30 '17 at 20:46
  • 1
    \$\begingroup\$ When reading the title I immeadiately though of Matt Parker's video. Nice to see it linked in the thread as well :D \$\endgroup\$ – PattuX Nov 30 '17 at 22:22
  • 1
    \$\begingroup\$ Just take the standard day-of-the-week libraries and modify the global constants accordingly, right? ;) \$\endgroup\$ – Wildcard Dec 1 '17 at 4:18
8
\$\begingroup\$

C (gcc), 60 bytes

f(m,d,y){y-=m<3;return(y+y/4-y/128+"-bed=pen+mad."[m]+d)%7;}

Try it online!

Simple modification of Sakamoto's method. Takes input as integer arguments in the order month, day, year, and outputs the number of the day (0-indexed on Sunday).

\$\endgroup\$
  • \$\begingroup\$ What does the "-bed=pen+mad." part do? \$\endgroup\$ – ericw31415 Dec 1 '17 at 0:48
  • \$\begingroup\$ @ericw31415 It accounts for the length of each month in days, and simply for the sake of appearance it is shifted up by multiples of 7 instead of being characters at ordinals (31, 28...). \$\endgroup\$ – notjagan Dec 1 '17 at 0:52
  • \$\begingroup\$ Right, I forgot that char still represents a number, so you can do mod 7 directly. \$\endgroup\$ – ericw31415 Dec 1 '17 at 1:04
6
\$\begingroup\$

Wolfram Language (Mathematica), 57 55 53 bytes

DayName@{m=#~Mod~128;6+Mod[(9#-m)/8-6Clip@m,28],##2}&

Try it online!

Takes three inputs: the year, the month, and the day, in that order. For example, if you save the above function as fun, then fun[2048,2,28] tells you the day of week of February 28, 2048.

How it works

The formula m=#~Mod~128;6+Mod[(9#-m)/8-6Clip@m,28] converts the year into an equivalent year (a year with exactly the same days of week) between 6 AD and 33 AD. To do this, we subtract an offset and then take the year mod 28; but the offset changes every 128 years, and for years divisible by 128, we have to make a further adjustment because the equivalent year shouldn't be a leap year.

Anyway, once that's done, we look up the month and day in that equivalent year using the built-in DayName.

\$\endgroup\$
3
\$\begingroup\$

Python 2, 67 bytes

def f(m,d,y):y-=m<3;return(y+y/4-y/128+int("0032503514624"[m])+d)%7

Try it online!

int("..."[m]) can be replaced by ord("-bed=pen+mad."[m]).

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 65 59 bytes

(d,m,y)=>(y-=m<3,(+"0032503514624"[m]+y+(y>>2)-(y>>7)+d)%7)

(d,m,y)=>(y-=m<3,(+"0032503514624"[m]+~~y+~~(y/4)-~~(y/128)+d)%7)

Uses Sakamoto's method. Gives 0=Sunday, 1=Monday, 2=Tuesday...

-2 bytes thanks to Misha Lavrov
-4 bytes thanks to Arnauld

\$\endgroup\$
  • 1
    \$\begingroup\$ I think ~~y can just be changed to y. You're not going to get a fractional year in the input, right? But I admit I'm not fluent in JavaScript. \$\endgroup\$ – Misha Lavrov Dec 1 '17 at 6:23
  • 2
    \$\begingroup\$ How about +y+(y>>2)-(y>>7)? \$\endgroup\$ – Arnauld Dec 1 '17 at 7:22
  • \$\begingroup\$ @MishaLavrov Yes, that is true. For some reason I decided I should floor everything. \$\endgroup\$ – ericw31415 Dec 1 '17 at 21:44
2
\$\begingroup\$

Actually, 37 bytes

This is a port of notjagan's modification of Sakamoto's algorithm, but with a few stack-based tricks as described below. Input format is day, year, month. Output format is 0-indexed with Sunday as 0. Golfing suggestions welcome! Try it online!

;"0032503514624"Ei)3>±+;¼L;¼¼½L±kΣ7@%

Explanation

                     Implicit input: day, year, month (month is at TOS)
;"0032503514624"Ei)  Get the month code, convert to float, rotate to bottom of the stack
3>±+                 If 3>month, add -1 to year
;¼L                  Push floor(year/4) to stack
;¼¼½L±               Push floor(year/4) and append -floor(year/128) to stack.
kΣ                   Wrap the stack (y/128, y/4, y, month_code, d) in a list and sum
7@%                  Get the sum modulo 7
                     Implicit return
\$\endgroup\$
2
\$\begingroup\$

Jelly, 32 31 30 28 bytes

Another port of notjagan's modification of Sakamoto's algorithm but with a base-250 number in place of 032503514624 (don't need the extra 0 because Jelly is 1-indexed). Input format is month, year, day. Output format is 0-based with Sunday as 0. Golfing suggestion are very welcome as the way the links were tough to arrange and may still be golfable. Try it online!

Edit: -1 byte from using bit shift instead of integer division. -1 byte from rearranging the beginning and the input format. -2 bytes thanks to Erik the Outgolfer and caird coinheringaahing.

3>_@µæ»7,2I+µ“Ṿ⁵Ḥ9{’D³ị+⁵+%7

Explanation

         Three arguments: month, year, day
3>_@     Subtract (month<3) from year. Call it y.
µ        Start a new monadic chain.
æ»7,2    Bit shift y by both 7 and 2 (equivalent to integer division by 128 and by 4).
I+       y + y/4 - y/128
µ        Start a new monadic chain.
“Ṿ⁵Ḥ9{’  The number 732573514624 in base 250.
D        The list [7, 3, 2, 5, 7, 3, 5, 1, 4, 6, 2, 4].
³ị       Get the month code from the list (1-based indexing).
+⁵+      Add y, our month code, and day together.
%7       Modulus 7.
\$\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.