AoC2018/Day5/Part2.hs

24 lines
689 B
Haskell
Executable File

#! /usr/bin/env stack
-- stack --resolver lts-12.20 --install-ghc script
{-# LANGUAGE ViewPatterns #-}
module Main where
import Data.Function (fix, on)
import qualified Data.Char as C
import qualified Data.List as L
main :: IO ()
main = interact $ \input -> show $ L.minimumBy (compare `on` snd) $
[ let rs = react input' in ((rs, r), length rs)
| r <- ['a'..'z']
, let input' = [ c | c <- input, C.isAlpha c, c /= r, c /= C.toUpper r ]
]
react [] = []
react (x:(react -> ys))
| (y:ys') <- ys, x `reactsWith` y = ys'
| otherwise = x:ys
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
(C.isLower x && C.toUpper x == y)