-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path019.hs
28 lines (22 loc) · 908 Bytes
/
019.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import EulerUtil.Dates
epoch :: Date
epoch = Date 1900 1 1
epochDayOfWeek :: Int
epochDayOfWeek = 1
daysSinceEpoch :: Date -> Int
daysSinceEpoch date = daysInPreviousYearsSinceEpoch (year date)
+ daysInPreviousMonthsInCurrentYear date
+ (day date - 1)
where
daysInPreviousMonthsInCurrentYear date = sum $ map daysInMonth (previousMonths date)
daysInPreviousYearsSinceEpoch currentYear = sum $ map daysInYear [year epoch..currentYear-1]
previousMonths date = [ Date (year date) m 1 | m <- [1..(month date)-1]]
dayOfWeek :: Date -> Int
dayOfWeek date = (epochDayOfWeek + daysSinceEpoch date) `mod` 7
startsWithSunday :: (Year, Year) -> [Date]
startsWithSunday = filter isSunday . monthsInYears
where
monthsInYears (start, end) = [ Date y m 1 | y <- [start..end], m<-[1..12] ]
isSunday = (==0) . dayOfWeek
euler19 :: (Year, Year) -> Int
euler19 = length . startsWithSunday