Day 5, parts 1 & 2: Initial solutions.
This commit is contained in:
parent
c8462f47b7
commit
992a3015ac
|
|
@ -0,0 +1,21 @@
|
||||||
|
#! /usr/bin/env stack
|
||||||
|
-- stack --resolver lts-12.20 --install-ghc script
|
||||||
|
{-# LANGUAGE FlexibleContexts, LambdaCase #-}
|
||||||
|
module Main where
|
||||||
|
|
||||||
|
import Data.Function (fix)
|
||||||
|
import qualified Data.Char as C
|
||||||
|
import qualified Data.List as L
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = interact $ show . react . takeWhile C.isAlpha
|
||||||
|
|
||||||
|
react input =
|
||||||
|
let scan (x:y:zs) = if x `reactsWith` y then scan zs else (x : scan (y:zs))
|
||||||
|
scan zs = zs
|
||||||
|
input' = scan input
|
||||||
|
result = (input', length input')
|
||||||
|
in if input' == input then result else react input'
|
||||||
|
|
||||||
|
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
|
||||||
|
(C.isLower x && C.toUpper x == y)
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
#! /usr/bin/env stack
|
||||||
|
-- stack --resolver lts-12.20 --install-ghc script
|
||||||
|
{-# LANGUAGE FlexibleContexts, LambdaCase #-}
|
||||||
|
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) $
|
||||||
|
[ react $ filter (\c -> C.isAlpha c && c /= r && c /= C.toUpper r) input
|
||||||
|
| r <- ['a'..'z']
|
||||||
|
]
|
||||||
|
|
||||||
|
react input =
|
||||||
|
let scan (x:y:zs) = if x `reactsWith` y then scan zs else (x : scan (y:zs))
|
||||||
|
scan zs = zs
|
||||||
|
input' = scan input
|
||||||
|
result = (input', length input')
|
||||||
|
in if input' == input then result else react input'
|
||||||
|
|
||||||
|
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
|
||||||
|
(C.isLower x && C.toUpper x == y)
|
||||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue