05AB1E,130个 128 133 131 124 123 字节
žežfžg)V0[Y`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()ćsO7%2@+Y`т‰0Kθ4ÖUD2Qi\28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë\1Dǝ¤>2}}ǝVYI'.¡Q#
我疯了..
对于高尔夫语言05AB1E,输入是否带有.
或都没有关系-
。但是,05AB1E没有任何内置对象可用于Date对象或计算。关于日期的唯一内置函数是今天的年/月/日/小时/分钟/秒/微秒。
因此,因此,您看到的几乎所有代码都是手动计算,可以转到第二天,并计算星期几。
由于我在Zeller的公式中忘记了一部分,所以增加了5个字节(1月和2月为1年)。
在线尝试或以模拟的自指定日期“ today” 在线尝试。
说明:
文本墙传入。
通常,该代码遵循以下伪代码:
1 Date currentDate = today;
2 Integer counter = 0;
3 Start an infinite loop:
4* If(currentDate is NOT a Saturday and currentDate is NOT a Sunday):
5 Counter += 1;
6* currentDate += 1; // Set currentDate to the next day in line
7 If(currentDate == parsed input-string):
8 Stop the infinite loop, and output the counter
1)Date currentDate = today;
是05AB1E程序的这一部分:
že # Push today's day
žf # Push today's month
žg # Push today's year
) # Wrap them into a single list
V # Pop and store this list in variable `Y`
2)Integer counter = 0;
和3)Start an infinite loop:
在05AB1E程序中很简单:
0 # Push 0 to the stack
[ # Start an infinite loop
4)If(currentDate is NOT a Saturday and currentDate is NOT a Sunday):
是手工计算的第一部分。由于05AB1E没有内置日期,因此我们必须手动计算星期几。
通用的公式是:
h=(q+⌊13(m+1)5⌋+K+⌊K4⌋+⌊J4⌋−2J)mod7,
3月到12月的几个月:
- q是day该月的(
[1, 31]
)
- m是1索引的month(
[3, 12]
)
- K是本世纪的年份(yearmod100)
- J是0索引世纪(⌊year100⌋)
一月和二月:
- q是day该月的(
[1, 31]
)
- m是1的索引month+12(
[13, 14]
)
- K是上一年的世纪((year−1)mod100)
- J是上一年的0索引世纪(⌊year−1100⌋)
结果在星期几h,其中0 =星期六,1 =星期日,...,6 =星期五。
资料来源:Zeller的全等
我们可以在05AB1E程序的这一部分中看到这一点:
Y # Push variable `Y`
` # Push the day, month, and year to the stack
U # Pop and save the year in variable `X`
Ð # Triplicate the month
3‹ # Check if the month is below 3 (Jan. / Feb.),
# resulting in 1 or 0 for truthy/falsey respectively
12* # Multiply this 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
13* # Multiplied by 13
5÷ # Integer-divided by 5
s3‹ # Check if the month is below 3 again (resulting in 1 / 0)
Xα # Take the absolute difference with the year
© # Store this potentially modified year in the register
т% # mYear modulo-100
D4÷ # mYear modulo-100, integer-divided by 4
®т÷©4÷ # mYear integer-divided by 100, and then integer-divided by 4
®·( # mYear integer-divided by 100, doubled, and then made negative
) # Wrap the entire stack into a list
ć # Extract the head (the counter variable that was also on the stack)
s # Swap so the calculated values above are as list at the top
O # Take the sum of this entire list
7% # And then take modulo-7 to complete the formula,
# resulting in 0 for Saturday, 1 for Sunday, and [2, 6] for [Monday, Friday]
2@ # Check if the day is greater than or equal to 2 (so a working day)
5)Counter += 1;
又直截了当:
# The >=2 check with `2@` results in either 1 for truthy and 0 for falsey
+ # So just adding it to the counter variable is enough
6)currentDate += 1; // Set currentDate to the next day in line
再复杂一点,因为我们必须手动进行。因此,它将扩展为以下伪代码:
a Integer isLeapYear = ...;
b Integer daysInCurrentMonth = currentDate.month == 2 ?
c 28 + isLeapYear
d :
e 31 - (currentDate.month - 1) % 7 % 2;
f If(currentDate.day < daysInCurrentMonth):
g nextDate.day += 1;
h Else:
i nextDate.day = 1;
j If(currentDate.month < 12):
k nextDate.month += 1;
l Else:
m nextDate.month = 1;
n nextDate.year += 1;
资料来源:
确定一年是否为a年的算法。(编辑:不再相关,因为我使用另一种方法来检查节省了7个字节的leap年。)
用于确定一个月中天数的算法。
Integer isLeapYear = ...;
在05AB1E程序中这样完成6a):
Y # Push variable `Y`
` # Push the days, month and year to the stack
т‰ # Divmod the year by 100
0K # Remove all items "00" (or 0 when the year is below 100)
θ # Pop the list, and leave the last item
4Ö # Check if this number is visible by 4
U # Pop and save the result in variable `X`
我的05AB1E答案中也使用了它,因此添加了一些示例年份来说明步骤。
6b) currentDate.month == 2 ?
和6c)28 + isLeapYear
的操作如下:
D # Duplicate the month that is now the top of the stack
2Q # Check if it's equal to 2
i # And if it is:
\ # Remove the duplicated month from the top of the stack
28X+ # Add 28 and variable `X` (the isLeapYear) together
6d) :
和6e)31 - (currentDate.month - 1) % 7 % 2;
的操作如下:
ë # Else:
< # Month - 1
7% # Modulo-7
É # Is odd (shortcut for %2)
31 # Push 31
α # Absolute difference between both
} # Close the if-else
6f)If(currentDate.day < daysInCurrentMonth):
是这样完成的:
‹ # Check if the day that is still on the stack is smaller than the value calculated
i # And if it is:
6克) nextDate.day += 1;
是这样完成的:
Y # Push variable `Y`
¬ # Push its head, the days (without popping the list `Y`)
> # Day + 1
0 # Push index 0
# (This part is done after the if-else clauses to save bytes)
}} # Close the if-else clauses
ǝ # Insert the day + 1 at index 0 in the list `Y`
V # Pop and store the updated list in variable `Y` again
6小时) Else:
nextDate.day = 1;
然后像这样完成和6i):
ë # Else:
Y # Push variable `Y`
1 # Push a 1
¾ # Push index 0
ǝ # Insert 1 at index 0 (days part) in the list `Y`
6j) If(currentDate.month < 12):
:
D # Duplicate the list `Y`
Ås # Pop and push its middle (the month)
D12‹ # Check if the month is below 12
i # And if it is:
6k) nextDate.month += 1;
:
> # Month + 1
1 # Push index 1
# (This part is done after the if-else clauses to save bytes)
}} # Close the if-else clauses
ǝ # Insert the month + 1 at index 1 in the list `Y`
V # Pop and store the updated list in variable `Y` again
6l)Else:
,6m)nextDate.month = 1;
和6n)nextDate.year += 1;
然后像这样完成:
ë # Else:
\ # Delete the top item on the stack (the duplicated month)
1 # Push 1
D # Push index 1 (with a Duplicate)
ǝ # Insert 1 at index 1 (month part) in the list `Y`
¤ # Push its tail, the year (without popping the list `Y`)
> # Year + 1
2 # Index 2
# (This part is done after the if-else clauses to save bytes)
}} # Close the if-else clauses
ǝ # Insert the year + 1 at index 2 in the list `Y`
V # Pop and store the updated list in variable `Y` again
最后在8)If(currentDate == parsed input-string):
和9)处Stop the infinite loop, and output the counter
:
Y # Push variable `Y`
I # Push the input
'.¡ '# Split it on dots
Q # Check if the two lists are equal
# # And if they are equal: stop the infinite loop
# (And output the top of the stack (the counter) implicitly)