65 lines
1.3 KiB
Haskell
65 lines
1.3 KiB
Haskell
{-
|
|
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
|