1
0
Fork 0

Day 01 - minor editing, contracting part 2.

This commit is contained in:
Jonathan Chan 2018-12-01 11:03:42 -08:00
parent f46e146baa
commit 0f238f22a1
1 changed files with 13 additions and 17 deletions

View File

@ -1,25 +1,21 @@
module Day01 (main) where module Day01 (main) where
import Data.Set (empty, insert, member) import Data.IntSet (empty, insert, member)
freqChangeList :: String -> [Int] parseChanges :: String -> [Int]
freqChangeList = map (read . dropWhile (== '+')) . lines parseChanges = map (read . dropWhile (== '+')) . lines
totalFrequency :: [Int] -> Int part1 :: [Int] -> Int
totalFrequency = foldr1 (+) part1 = sum
firstFreqTwice :: [Int] -> Int part2 :: [Int] -> Int
firstFreqTwice changeList = part2 = dupl empty . scanl1 (+) . cycle
let freqs = scanl1 (+) $ cycle changeList where dupl s (f:fs)
in dupl freqs empty | member f s = f
where dupl (f:fs) s = | otherwise = dupl (insert f s) fs
if member f s
then f
else dupl fs (insert f s)
main :: IO () main :: IO ()
main = do main = do
input <- readFile "input/01.txt" changes <- parseChanges <$> readFile "input/01.txt"
let changeList = freqChangeList input print $ part1 changes
print $ totalFrequency changeList print $ part2 changes
print $ firstFreqTwice changeList