Euleryx Problem 19 – Counting Sundays
My workflow and macro:
Workflow:
Answer: 171
Last Week's Favourite Solution:
Yes, I am aware that it was over a week ago since the last post, but changing the name of the award just didn’t seem right! The winner of last week's favourite solution is @AkimasaKajitani . @AkimasaKajitani took it one step further and provided two separate solutions, one generating all possible routes, and the other calculating the optimal path to each end location, keeping only the optimal steps, each iteration. Please find their solutions on page one of last week's post or click here.
Mathematical Theory:
After a tough couple of weeks with the Advent of Code, we are starting back with a relatively kind project. This weeks problem could be easily solved using Alteryx date time capabilities, to easily convert the dates into days of the week. However, I believe this alone is a little too easy, so I decided to tackle this problem using numbers and string values only.
The essence of this challenge is spotting patterns. Based on the information provided, the only day of the week we currently know is Monday, 1st Jan 1900. So lets calculate how many days later, the subsequent months are:
Another fact we can use to help, is that each week contains only 7 days. So that means, after every multiple of 7 days, we arrive back on a Monday. If we then divide the “days later” value, by 7, then keep only the remainder, we can figure out, how many days after Monday, the 1st of the month is:
Looking at the remainders, we know that 1 day after Monday is Tuesday, 3 days after Monday is Thursday, and 6 days after Monday is Sunday. This means that April 1st 1900 was a Sunday.
If we extend the list out, so we have a running total for “days later” than 1900-01-01, we can then use this same divide by 7 logic to easily identify all the Sundays.
Method:
1) Generate one row per year, starting at 1900, and ending at 2000
2) Append the months on to the list of years
3) Update the days in the month to 29, for the relevant Februarys.
4) Calculate a running total for the number of days after 1900-01-01
5) Find the modulus of the days since 1900-01-01, when divided by 7. (I also had to adjust the running total slightly so it represented the days to the start of the month not the end)
6) Filter to days where the modulus equalled 6, as we know these to all be Sundays, then count the number of these records. (Make sure to filter to the correct dates too)
7) Submit your answer to the Project Euler Website!
Summary:
Creating the running total gave us the number of days since the 1st Jan 1900. By then exploiting the repetitive sequence formed by the days of the week, we were able to easily figure out which day of the week a date was on, by simply dividing the running total by 7.
Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.
With completeness of AoC, the Euler Projec is back! 👍
I simple generate all the days between 1 Jan 1901 to 31 Dec 2000 then count all the Sundays using DateTimeFormat as the first day of the month.
I guess I went the easy route... hard to resist when Designer has so many handy functions built right in 😀
