-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.hs
36 lines (29 loc) · 1.07 KB
/
Day05.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
34
35
36
-- Day 5: Alchemical Reduction --
--
-- Usage: runhaskell Day05.hs < ../inputs/05.txt
import Data.Char
import Data.List
react :: (Integer, String) -> String -> (Integer, String)
react (deletes, acc) (a : b : rest)
| a /= b && toLower a == toLower b = react (succ deletes, acc) rest
| otherwise = react (deletes, a : acc) (b : rest)
react (deletes, acc) [a] = react (deletes, a : acc) []
react (deletes, acc) [] = (deletes, reverse acc)
reactFully :: String -> String
reactFully str =
case (react (0, "") str) of
(0, remainder) -> remainder
(_, remainder) -> reactFully remainder
uniqueChars :: String -> String
uniqueChars str = nub $ map toLower str
part1 :: String -> String
part1 input = show $ length $ reactFully input
part2 :: String -> String
part2 input = show $ minimum $ map (length . reactFully) versions
where
versions = map (\c -> filter ((/= c) . toLower) input) uniques
uniques = uniqueChars input
main :: IO ()
main = interact (\input -> unlines [part1 $ parse input, part2 $ parse input])
where
parse = head . lines