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”.
Input
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”.
Output
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Collections.Generic; //http://codeforces.com/contest/724/problem/A class Problem1 { static void Main() { string str_first = Console.ReadLine(); string str_second = Console.ReadLine(); List<string> my_days = new List<String> { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" }; int i_first = my_days.FindIndex(my_day => my_day.Equals(str_first)); int i_second = my_days.FindIndex(my_day => my_day.Equals(str_second)); if ((i_first == i_second) || ((i_first+2) % 7 == i_second) || ((i_first + 3) % 7 == i_second)) { Console.WriteLine("YES"); }else{ Console.WriteLine("NO"); } } } |
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! 😀