2018/Day1B.hs

15 lines
559 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 17:09:41 +00:00
| S.member freq history = freq -- When the frequency is in the history set, return it
| otherwise = handle (freq + nextDelta) (S.insert freq history) xs -- Recurse otherwise
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