Optimize day 5

This commit is contained in:
Sijmen 2018-12-05 23:26:47 +01:00
parent e9b068912a
commit 08eaeb02e6

View file

@ -2,19 +2,18 @@
-- stack --resolver lts-12.5 script
module Day5A where
import Data.Bits (xor)
import Data.Char
import qualified Data.Foldable as F
invertCase :: Char -> Char
invertCase c
| isUpper c = toLower c
| otherwise = toUpper c
invertCase = chr . xor 0x20 . ord
calcLength :: String -> String -> Int
calcLength ys (x:y:xs)
| x == invertCase y = case length ys of
0 -> calcLength ys xs
_ -> calcLength (tail ys) (head ys:xs)
| x == invertCase y = if null ys
then calcLength ys xs
else calcLength (tail ys) (head ys:xs)
| otherwise = calcLength (x:ys) (y:xs)
calcLength ys [x] = calcLength (x:ys) []
calcLength ys [] = length ys