AoC2018/Day02/Part2.hs

22 lines
627 B
Haskell
Executable File

#! /usr/bin/env stack
-- stack --resolver lts-12.20 --install-ghc script
module Main where
import Control.Arrow ((***))
import qualified Data.List as L
import qualified Data.HashSet as S
main :: IO ()
main = interact $ (show .) $ (. lines) $ \codes ->
loopRight (S.empty, L.concatMap deletions codes) $ \(seen, d:ds) ->
if S.member d seen
then Left (snd d)
else Right (S.insert d seen, ds)
loopRight :: b -> (b -> Either a b) -> a
loopRight b f = either id (`loopRight` f) (f b)
deletions :: [a] -> [(Int, [a])]
deletions [] = []
deletions (x:xs) = (0, xs) : map ((+1) *** (x:)) (deletions xs)