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
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