2018/Day1B.hs

16 lines
611 B
Haskell
Raw Normal View History

2018-12-01 13:52:17 +00:00
module Day1B where
import Day1A (read')
2018-12-01 15:21:38 +00:00
import qualified Data.IntSet as S
2018-12-01 13:52:17 +00:00
2018-12-01 15:21:38 +00:00
handle :: Int -> S.IntSet -> [Int] -> Int
2018-12-01 13:52:17 +00:00
handle freq history (nextDelta:xs)
2018-12-01 15:21:38 +00:00
| (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
2018-12-01 13:52:17 +00:00
handle _ _ _ = error "xs should not be empty :("
main :: IO ()
main = interact $ show . handle 0 S.empty . deltaFreqs
2018-12-01 15:21:38 +00:00
where deltaFreqs = cycle . map read' . lines -- Convert input to Ints and repeat infinitely