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