From 05d587e1eb206daec3094d9ae343b8bd81238bf0 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Sat, 3 Aug 2024 15:58:33 -0400 Subject: [PATCH] Project Euler #19 --- src/projectEuler/question19.hs | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/projectEuler/question19.hs diff --git a/src/projectEuler/question19.hs b/src/projectEuler/question19.hs new file mode 100644 index 0000000..60814e2 --- /dev/null +++ b/src/projectEuler/question19.hs @@ -0,0 +1,64 @@ +{- +https://projecteuler.net/problem=19 + +You are given the following information, but you may prefer to do some research for yourself. + +- 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)? +-} + +daysInMonth :: [Int] +daysInMonth = + [ 31 -- Jan + , 28 -- Feb + , 31 -- Mar + , 30 -- Apr + , 31 -- May + , 30 -- Jun + , 31 -- Jul + , 31 -- Aug + , 30 -- Sep + , 31 -- Oct + , 30 -- Nov + , 31 -- Dec + ] + +isLeapYear :: Int -> Bool +isLeapYear yyyy + | yyyy `isDivBy` 400 = True + | yyyy `isDivBy` 100 = False + | otherwise = yyyy `isDivBy` 4 + where + isDivBy yyyy n = yyyy `rem` n == 0 + +daysOfWeek :: [Int] +daysOfWeek = cycle + [ 1 -- Sun + , 0 -- Mon + , 0 -- Tue + , 0 -- Wed + , 0 -- Thu + , 0 -- Fri + , 0 -- Sat + ] + +daysInYear :: Int -> Int +daysInYear yyyy = + if isLeapYear yyyy + then 366 + else 365 + +years :: [Int] +years = [1901..2000] + +ans :: Int +ans = sum $ drop 2 $ take (sum $ map daysInYear years) daysOfWeek +-- 5217