2018-11-26 02:32:15 +00:00
|
|
|
module Day01 (main) where
|
|
|
|
|
2018-12-01 08:41:50 +00:00
|
|
|
import Data.Set (empty, insert, member)
|
|
|
|
|
|
|
|
freqChangeList :: String -> [Int]
|
|
|
|
freqChangeList = map (read . dropWhile (== '+')) . lines
|
|
|
|
|
|
|
|
totalFrequency :: [Int] -> Int
|
|
|
|
totalFrequency = foldr1 (+)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-11-26 02:32:15 +00:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
input <- readFile "input/01.txt"
|
2018-12-01 08:41:50 +00:00
|
|
|
let changeList = freqChangeList input
|
|
|
|
print $ totalFrequency changeList
|
|
|
|
print $ firstFreqTwice changeList
|