]> localhost Git - adventofcode2018.git/blame - day01/part2.hs
Day 3: Extract unoverlap into its own function
[adventofcode2018.git] / day01 / part2.hs
CommitLineData
b2f8fbc5
JK
1import qualified Data.Set as S
2
3main = do
4 contents <- getContents
5 let freqs = cycle $ map toInt (lines contents)
6 print $ findDouble (S.empty :: S.Set Int) 0 freqs
7
8toInt :: String -> Int
9toInt (x:xs) = if x == '+'
10 then read xs :: Int
11 else -(read xs :: Int)
12
13findDouble :: S.Set Int -> Int -> [Int] -> Int
14findDouble history total (x:xs)
15 | S.member total history = total
16 | otherwise = findDouble nextHist nextTotal xs
17 where nextHist = S.insert total history
18 nextTotal = total + x