Day 5, parts 1 & 2: Optimize `reacts` to only use one pass.
This commit is contained in:
parent
d9bce28737
commit
e984a3cc10
|
|
@ -1,21 +1,18 @@
|
||||||
#! /usr/bin/env stack
|
#! /usr/bin/env stack
|
||||||
-- stack --resolver lts-12.20 --install-ghc script
|
-- stack --resolver lts-12.20 --install-ghc script
|
||||||
{-# LANGUAGE FlexibleContexts, LambdaCase #-}
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Data.Function (fix)
|
|
||||||
import qualified Data.Char as C
|
import qualified Data.Char as C
|
||||||
import qualified Data.List as L
|
import qualified Data.List as L
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = interact $ show . react . takeWhile C.isAlpha
|
main = interact $ show . length . react . takeWhile C.isAlpha
|
||||||
|
|
||||||
react input =
|
react [] = []
|
||||||
let scan (x:y:zs) = if x `reactsWith` y then scan zs else (x : scan (y:zs))
|
react (x:(react -> ys))
|
||||||
scan zs = zs
|
| (y:ys') <- ys, x `reactsWith` y = ys'
|
||||||
input' = scan input
|
| otherwise = x:ys
|
||||||
result = (input', length input')
|
|
||||||
in if input' == input then result else react input'
|
|
||||||
|
|
||||||
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
|
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
|
||||||
(C.isLower x && C.toUpper x == y)
|
(C.isLower x && C.toUpper x == y)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#! /usr/bin/env stack
|
#! /usr/bin/env stack
|
||||||
-- stack --resolver lts-12.20 --install-ghc script
|
-- stack --resolver lts-12.20 --install-ghc script
|
||||||
{-# LANGUAGE FlexibleContexts, LambdaCase #-}
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Data.Function (fix, on)
|
import Data.Function (fix, on)
|
||||||
|
|
@ -9,16 +9,15 @@ import qualified Data.List as L
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = interact $ \input -> show $ L.minimumBy (compare `on` snd) $
|
main = interact $ \input -> show $ L.minimumBy (compare `on` snd) $
|
||||||
[ react $ filter (\c -> C.isAlpha c && c /= r && c /= C.toUpper r) input
|
[ let rs = react input' in ((rs, r), length rs)
|
||||||
| r <- ['a'..'z']
|
| r <- ['a'..'z']
|
||||||
|
, let input' = [ c | c <- input, C.isAlpha c, c /= r, c /= C.toUpper r ]
|
||||||
]
|
]
|
||||||
|
|
||||||
react input =
|
react [] = []
|
||||||
let scan (x:y:zs) = if x `reactsWith` y then scan zs else (x : scan (y:zs))
|
react (x:(react -> ys))
|
||||||
scan zs = zs
|
| (y:ys') <- ys, x `reactsWith` y = ys'
|
||||||
input' = scan input
|
| otherwise = x:ys
|
||||||
result = (input', length input')
|
|
||||||
in if input' == input then result else react input'
|
|
||||||
|
|
||||||
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
|
x `reactsWith` y = (C.isUpper x && C.toLower x == y) ||
|
||||||
(C.isLower x && C.toUpper x == y)
|
(C.isLower x && C.toUpper x == y)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue