From 0f238f22a1e9c340bf8adef6e73d8d89946c936a Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sat, 1 Dec 2018 11:03:42 -0800 Subject: [PATCH] Day 01 - minor editing, contracting part 2. --- src/Day01.hs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/Day01.hs b/src/Day01.hs index f02d2c2..36a7696 100644 --- a/src/Day01.hs +++ b/src/Day01.hs @@ -1,25 +1,21 @@ module Day01 (main) where -import Data.Set (empty, insert, member) +import Data.IntSet (empty, insert, member) -freqChangeList :: String -> [Int] -freqChangeList = map (read . dropWhile (== '+')) . lines +parseChanges :: String -> [Int] +parseChanges = map (read . dropWhile (== '+')) . lines -totalFrequency :: [Int] -> Int -totalFrequency = foldr1 (+) +part1 :: [Int] -> Int +part1 = sum -firstFreqTwice :: [Int] -> Int -firstFreqTwice changeList = - let freqs = scanl1 (+) $ cycle changeList - in dupl freqs empty - where dupl (f:fs) s = - if member f s - then f - else dupl fs (insert f s) +part2 :: [Int] -> Int +part2 = dupl empty . scanl1 (+) . cycle + where dupl s (f:fs) + | member f s = f + | otherwise = dupl (insert f s) fs main :: IO () main = do - input <- readFile "input/01.txt" - let changeList = freqChangeList input - print $ totalFrequency changeList - print $ firstFreqTwice changeList + changes <- parseChanges <$> readFile "input/01.txt" + print $ part1 changes + print $ part2 changes