Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

When is my coworker's next birthday?

inspired by this post.

Consider 2 coworkers who work Monday - Friday, every week of every year, with no breaks or national holidays.

On day n (day, month and year), worker 1 asks worker 2 their birthday, in the hopes of throwing an office party for the occasion.

However, worker 2 dislikes office parties, and would like to ensure the next office party be thrown as late as possible.

What is the day (day and month but not year) they should claim their birthday is such that the maximum time (in days) elapses between day n and the next instance of the day claimed being a working day?

Input:

The date of day n, including day, month, and year, in the format of your choice.
The format may include a time of day (always the same one though).
n > 31 December 1582.

Output:

A date including day and month, such that the next instance of that day being a working day (Mon-Fri) is as far in the future from day n as possible.

The format is also flexible, and the output may not represent the actual next birthday in question, as long as the day and month are correct (for example, outputting 31 January 1934 5:00AM is a valid output even if the next birthday happens on 31 January 2006)

Edge polishing:

Your algorithm must minimally work with the current Gregorian calendar, including leap years. Assume the Gregorian calendar will keep being the standard for infinite time in the future, and that the current pattern of leap years stays the same:

A year is a leap year if it is divisible by 4, except if it is divisible by 100, except if it is divisible by 400.

You may consider that day n is already passed, such that worker 1 would have to wait at least a full year before throwing a party (because they were unprepared). You may also do the opposite. Specify which one you chose in your submission.

Test cases:

March 1st, 1892 -> February 29th. (4,381 days)
February 28th, 2024 -> February 22nd. (1,090 days)
May 26th, 2025 -> May 23rd. (1,093 days)
February 25, 2100 -> February 29th. (1,464 days)

Scoring

This is .

Notes:

I checked the test cases manually in outlook, so I could be very wrong. Additional interesting or edge-case tests are welcome.

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ Note that the longest possible duration in the linked Puzzling question is 4381 days ≈ 12 years due to leap days not occuring on years that are multiples of 100. 5e3 should be enough \$\endgroup\$ Commented Jun 1 at 4:05
  • \$\begingroup\$ @emanresuA my solution was only grabbing 3000 dates because it would extract the month and day and use that in another loop that calculated year separately, which is why it was returning the correct answer even in that case. The only reason I did that was because I thought it would save bytes over disqualifying dates that already had a valid weekday in an earlier year. However, your comment inspired me to look into the Ruby spec and find a way to do just that in way fewer bytes. Thanks! \$\endgroup\$ Commented Jun 2 at 7:37
  • \$\begingroup\$ i hadn't seen how far you golfed this since the first submission, very nice golf! \$\endgroup\$ Commented Jun 13 at 14:10

AltStyle によって変換されたページ (->オリジナル) /