4Ö2‚I3@è³¹²23*9÷Æ7%
-12 bytes thanks to @DominicVanEssen (and @Neil) for reminding me that the challenge years are guaranteed to be 2000-2002
-2 bytes porting @Arnauld's JavaScript (top) answer, so make sure to upvote him as well!
-1 byte thanks to @DominicVanEssen again in @Arnauld's port
-2 bytes taking three loose inputs instead of a triplet
Three loose input-integers in the order yyyy,MM,dd
; output is an integer where 6
= Sunday, 5
= Monday, ..., 0
= Saturday.
Try it online or verify all test cases.
Explanation:
4Ö # Check if the (implicit) first input-year is divisible by 4
2‚ # Pair this 0 or 1 with 2
I # Push the second input-month
3@ # Check if it's >=3
è # Use that to index into the pair
³ # Push the third input-day
¹ # Push the first input-year
² # Push the second input-month
23* # Multiply the month by 23
9÷ # Integer-divided by 9
Æ # Reduce the entire stack by subtracting:
# [m%4<1,2][m>=3]-d-y-m*23//9
7% # Modulo-7, resulting in 6 to 0 for Sunday to Saturday
Original 36 24 bytes answer:
`UD3‹©12*+>₂*T÷®XαD4÷O7%
Try it online or verify all test cases.
Explanation:
05AB1E lacks any date builtins (except for the current year/month/day/etc.), so everything is done manually using Zeller's congruence, taken from this earlier 05AB1E answer of mine:
The formula to do this is:
$${\displaystyle h=\left(q+\left\lfloor{\frac{13(m+1)}{5}}\right\rfloor+K+\left\lfloor{\frac{K}{4}}\right\rfloor+\left\lfloor{\frac{J}{4}}\right\rfloor-2J\right){\bmod{7}}}$$
Where for the months March through December:
- \$q\$ is the \$day\$ of the month (
[1, 31]
)
- \$m\$ is the 1-indexed \$month\$ (
[3, 12]
)
- \$K\$ is the year of the century (\$year \bmod 100\$)
- \$J\$ is the 0-indexed century (\$\left\lfloor {\frac {year}{100}}\right\rfloor\$)
And for the months January and February:
- \$q\$ is the \$day\$ of the month (
[1, 31]
)
- \$m\$ is the 1-indexed \$month + 12\$ (
[13, 14]
)
- \$K\$ is the year of the century for the previous year (\$(year - 1) \bmod 100\$)
- \$J\$ is the 0-indexed century for the previous year (\$\left\lfloor {\frac {year-1}{100}}\right\rfloor\$)
Resulting in the day of the week \$h\$, where 0 = Saturday, 1 = Sunday, ..., 6 = Friday.
But, since the challenge states the year is guaranteed to be 2000-2002, we can simplify the formula to this instead:
$${\displaystyle h=\left(q+\left\lfloor{\frac{13(m+1)}{5}}\right\rfloor+K+\left\lfloor{\frac{K}{4}}\right\rfloor\right){\bmod{7}}}$$
Resulting in the day of the week \$h\$, where 0 = Friday, 1 = Saturday, ..., 6 = Thursday.
As for the actual program:
` # Push the day, month, and year of the (implicit) input-triplet to the stack
U # Pop and save the year in variable `X`
D # Duplicate the month
3‹ # Check if the month is below 3 (Jan. / Feb.),
# resulting in 1 or 0 for truthy/falsey respectively
© # Store this in variable `®` (without popping)
12* # Multiply it by 12 (either 0 or 12)
+ # And add it to the month
# This first part was to make Jan. / Feb. 13 and 14
> # Month + 1
₂* # Multiplied by 26
T÷ # Integer-divided by 10
® # Push month<3 from variable `®` again
Xα # Take the absolute difference with the year
D4÷ # mYear, integer-divided by 4
O # Sum all values on the stack together
7% # And then take modulo-7 to complete the formula,
# resulting in 0 to 6 for Friday to Thursday
# (which is output implicitly as result)