Codeforces – Problem A – Checking the Calendar

Today I have not been very productive, after all it is Sunday and a person should relax. However, I have noticed the Intel Code Challenge in CodeForces, thus I have decided just to try the first problem. The problem seemed trivial, but as usual – it was not that easy. Actually, it took me more than 15 minutes to come up with a solution of something that should have been in my mind from the start.


Doesn’t matter, at the end it was solved 🙂

The problem is the following:

You are given names of two days of the week.

Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong to one year.

In this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.

Names of the days of the week are given with lowercase English letters: “monday”, “tuesday”, “wednesday”, “thursday”, “friday”, “saturday”, “sunday”.

The input consists of two lines, each of them containing the name of exactly one day of the week. It’s guaranteed that each string in the input is from the set “monday”, “tuesday”, “wednesday”, “thursday”, “friday”, “saturday”, “sunday”.

Print “YES” (without quotes) if such situation is possible during some non-leap year. Otherwise, print “NO” (without quotes).

The tricky part is that in order to save time, I started to think about the possibilities between the months in a year. They are the following:

31 – 28

31 – 31

31 – 30

28 – 31

30 – 31

And to try to come up with a solution from there. Actually this was not a great idea and a bit useless. They just ask you for three cases. The case with 28, 30 and 31 days. Because we only have this possibilities in a non leap year. And the information about the days per month was simply given to distract you. With me they have succeeded. However, once I realized what was wanted, it was just a matter of time to code it and it ran successfully from the first time. Still, a great first problem in a competition.

Here comes my solution:

Available in GitHub as well. 😀

Take away message from the whole story – take a look at the exact client needs and do not try to get overflowed with information! 😀

Tagged with: