2018/Day1B.hs

16 lines
611 B
Haskell

module Day1B where
import Day1A (read')
import qualified Data.IntSet as S
handle :: Int -> S.IntSet -> [Int] -> Int
handle freq history (nextDelta:xs)
| (S.size s) == (S.size history) = freq -- When the frequency is in the history set, return it
| otherwise = handle (freq + nextDelta) (S.insert freq history) xs -- Recurse otherwise
where s = S.insert freq history
handle _ _ _ = error "xs should not be empty :("
main :: IO ()
main = interact $ show . handle 0 S.empty . deltaFreqs
where deltaFreqs = cycle . map read' . lines -- Convert input to Ints and repeat infinitely