git.haldean.org aoc-2018 / master 05 / Aoc05.hs
master

Tree @master (Download .tar.gz)

Aoc05.hs @masterraw · history · blame

import Control.Arrow
import Data.Char
import Data.Function
import qualified Data.List as L
import qualified Data.Set as S
import System.Environment

collapse1 (a:b:ss)
  | toLower a == toLower b && a /= b = collapse1 ss
  | otherwise = a:(collapse1 $ b:ss)
collapse1 s = s
collapse s = let cs = collapse1 s in if cs == s then s else collapse cs
main1 = readFile "input.txt" >>= (collapse >>> length >>> subtract 1 >>> print)

minimize s =
  let units = S.fromList $ map toLower s
  in S.map (\u -> filter ((u /=) . toLower) s & collapse & length) units
     & S.toList & L.minimum & subtract 1
main2 = readFile "input.txt" >>= (collapse >>> minimize >>> print)

main = fmap head getArgs >>= \a -> if a == "1" then main1 else main2