AoC2018/Day1/Part2.hs

19 lines
551 B
Haskell
Executable File

#! /usr/bin/env stack
-- stack --resolver lts-12.20 --install-ghc script
module Main where
import Data.List (scanl')
import qualified Data.Set as S
loopRight :: b -> (b -> Either a b) -> a
loopRight b f = case f b of
Left a -> a
Right b' -> loopRight b' f
main :: IO ()
main = interact $ show . \input ->
let changes = fmap (read . dropWhile (=='+')) $ lines input
freq = scanl' (+) 0 (cycle changes)
in loopRight (S.empty, freq) $ \(s, f:fs) ->
if S.member f s then Left (f :: Int) else Right (S.insert f s, fs)