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)
オンラインそれを試してみたり「今日のエミュレートされた自己指定した日付とオンラインそれを試してみてください。
説明:
着信テキストの壁。
一般に、コードは次の擬似コードに従います。
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)mod 7、
3月から12月までの期間:
- qある(月)dY
[1, 31]
- mは1から始まる()M O N TのH
[3, 12]
- Kは世紀の()ye a r mod 100
- Jは0から始まる世紀です()⌊ YE R100⌋
そして、1月と2月の間:
- qある(月)dY
[1, 31]
- mは1から始まる()m o n t h + 12
[13, 14]
- Kは前年の世紀の年()(year−1)mod100
- Jは前年の0から始まる世紀です()⌊year−1100⌋
結果は曜日になります。0=土曜日、1 =日曜日、...、6 =金曜日です。出典:Zellerの合同h
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;
出典:
年がle年かどうかを判断するアルゴリズム。(編集:7バイトを節約したうるう年をチェックする代替方法を使用するため、関連性がなくなりました。)
月の日数を決定するアルゴリズム。
6a)Integer isLeapYear = ...;
05AB1Eプログラムでは次のように行われます。
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:
6g)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
6h)Else:
および6i)nextDate.day = 1;
は次のように行われます。
ë # 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)