Skip to content
Browse files

Completed refactor of #19

  • Loading branch information...
1 parent 4a3245e commit 26709c26d6e9acc767a95f0bd881699491bb7048 Eric Wilson committed Jan 31, 2011
Showing with 20 additions and 30 deletions.
  1. +12 −20 019.hs
  2. +7 −9 EulerUtil/Dates.hs
  3. +1 −1 README
View
32 019.hs
@@ -6,31 +6,23 @@ epoch = Date 1900 1 1
epochDayOfWeek :: Int
epochDayOfWeek = 1
-daysInPreviousYearsSinceEpoch :: Year -> Int
-daysInPreviousYearsSinceEpoch currentYear = sum $ map daysInYear [year epoch..currentYear-1]
-
-daysInPreviousMonthsInCurrentYear :: Date -> Int
-daysInPreviousMonthsInCurrentYear date = sum $ map daysInMonth (previousMonths date)
-
-previousMonths :: Date -> [Date]
-previousMonths date = [ Date (year date) m 1 | m <- [1..(month date)-1]]
-
daysSinceEpoch :: Date -> Int
daysSinceEpoch date = daysInPreviousYearsSinceEpoch (year date)
+ daysInPreviousMonthsInCurrentYear date
+ (day date - 1)
-
-monthsInYears :: Year -> Year -> [Date]
-monthsInYears start end = [ Date y m 1 | y <- [start..end], m<-[1..12] ]
-
-mod7 :: Int -> Int
-mod7 n = mod n 7
+ 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 = mod7 $ epochDayOfWeek + daysSinceEpoch date
+dayOfWeek date = (epochDayOfWeek + daysSinceEpoch date) `mod` 7
-isSunday :: Date -> Bool
-isSunday date = dayOfWeek date == 0
+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
-startsWithSunday :: Year -> Year -> [Date]
-startsWithSunday start end = filter isSunday $ monthsInYears start end
+euler19 :: (Year, Year) -> Int
+euler19 = length . startsWithSunday
View
16 EulerUtil/Dates.hs
@@ -6,7 +6,6 @@ module EulerUtil.Dates
, year
, month
, day
-, isLeapYear
, daysInYear
, daysInMonth
) where
@@ -20,13 +19,6 @@ data Date = Date { year :: Year
, day :: Day
} deriving (Show)
-isLeapYear :: Year -> Bool
-isLeapYear year
- | mod year 400 == 0 = True
- | mod year 100 == 0 = False
- | mod year 4 == 0 = True
- | otherwise = False
-
daysInYear :: Year -> Int
daysInYear year =
if isLeapYear year
@@ -41,4 +33,10 @@ daysInMonth date
where
m = month date
y = year date
-
+
+isLeapYear :: Year -> Bool
+isLeapYear year
+ | mod year 400 == 0 = True
+ | mod year 100 == 0 = False
+ | mod year 4 == 0 = True
+ | otherwise = False
View
2 README
@@ -19,5 +19,5 @@ Here's the current status: (No status listed if no code is checked in.)
#14: Some exploratory work
#15: SOLVED
#16: SOLVED
-#19: Some preliminary work
+#19: SOLVED
#20: SOLVED

0 comments on commit 26709c2

Please sign in to comment.
Something went wrong with that request. Please try again.