Last active
July 31, 2020 15:03
-
-
Save wbbradley/a820063a8580edd4563ec6b70cffe430 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://projecteuler.net/problem=19 | |
1 Jan 1900 was a Monday. | |
Thirty days has September, | |
April, June and November. | |
All the rest have thirty-one, | |
Saving February alone, | |
Which has twenty-eight, rain or shine. | |
And on leap years, twenty-nine. | |
A leap year occurs on any year evenly divisible by 4, but not on a century | |
unless it is divisible by 400. | |
How many Sundays fell on the first of the month during the twentieth century | |
(1 Jan 1901 to 31 Dec 2000)? | |
*/ | |
fn is_sunday(dow) => dow % 7 == 0 | |
fn is_leap(year) => (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 | |
fn days_in_month(year, month) { | |
if month in [4, 6, 9, 11] { | |
return 30 | |
} else if month == 2 { | |
return year.is_leap ? 29 : 28 | |
} else { | |
return 31 | |
} | |
} | |
let weekdays = { | |
0: "sunday", | |
1: "monday", | |
2: "tuesday", | |
3: "wednesday", | |
4: "thursday", | |
5: "friday", | |
6: "saturday", | |
} | |
fn day_of_week(day_count) { | |
if weekdays[day_count % 7] is Just(x) { | |
return x | |
} else { | |
return "bugday" | |
} | |
} | |
fn main() { | |
var day_count = 1 | |
var sundays = 0 | |
for year in [1900..2000] { | |
for month in [1..12] { | |
if day_count.is_sunday and year >= 1901 { | |
sundays += 1 | |
} | |
day_count += days_in_month(year, month) | |
} | |
} | |
print(sundays) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment