| 1 | import qualified Data.Set as S |
| 2 | |
| 3 | main = do |
| 4 | contents <- getContents |
| 5 | let freqs = cycle $ map toInt (lines contents) |
| 6 | print $ findDouble (S.empty :: S.Set Int) 0 freqs |
| 7 | |
| 8 | toInt :: String -> Int |
| 9 | toInt (x:xs) = if x == '+' |
| 10 | then read xs :: Int |
| 11 | else -(read xs :: Int) |
| 12 | |
| 13 | findDouble :: S.Set Int -> Int -> [Int] -> Int |
| 14 | findDouble 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 |