-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.hs
33 lines (27 loc) · 882 Bytes
/
Day5.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Javran.AdventOfCode.Y2018.Day5 (
) where
import Data.Bits
import Data.Char
import qualified Data.Set as S
import Javran.AdventOfCode.Prelude
data Day5 deriving (Generic)
canReact :: Char -> Char -> Bool
canReact x y = toLower x == toLower y && (isUpper x `xor` isUpper y)
doReduction :: String -> String -> String
doReduction stk = \case
[] -> reverse stk
x : xs -> case stk of
s : ss ->
if canReact x s
then doReduction ss xs
else doReduction (x : stk) xs
[] -> doReduction [x] xs
instance Solution Day5 where
solutionRun _ SolutionContext {getInputS, answerShow} = do
[xs] <- lines <$> getInputS
answerShow (length (doReduction [] xs))
let unitTypes = S.fromList $ fmap toLower xs
answerShow $
minimum $ do
c <- S.toList unitTypes
pure $ length $ doReduction [] (filter ((/= c) . toLower) xs)