aboutsummaryrefslogtreecommitdiff
path: root/2021/day10/10b.hs
blob: 39b427fd9948a9b2f0eee5456692ee1362b04d6e (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Main where

import Data.List (sort)

isStartParen :: Char -> Bool
isStartParen '(' = True
isStartParen '[' = True
isStartParen '{' = True
isStartParen '<' = True
isStartParen _ = False

isEndParen :: Char -> Bool
isEndParen ')' = True
isEndParen ']' = True
isEndParen '}' = True
isEndParen '>' = True
isEndParen _ = False

flipParen :: Char -> Char
flipParen '(' = ')'
flipParen ')' = '('
flipParen '[' = ']'
flipParen ']' = '['
flipParen '{' = '}'
flipParen '}' = '{'
flipParen '<' = '>'
flipParen '>' = '<'
flipParen _ = undefined

getScore :: Char -> Int
getScore ')' = 3
getScore ']' = 57
getScore '}' = 1197
getScore '>' = 25137

completionScore :: Char -> Int
completionScore ')' = 1
completionScore ']' = 2
completionScore '}' = 3
completionScore '>' = 4
completionScore _ = undefined

consumeBlock :: Char -> String -> Either Char String
consumeBlock t "" = Right ""
consumeBlock t (c:cs) | c == flipParen t  = Right cs
                      | isStartParen c    = (consumeBlock c cs) >>= (consumeBlock t)
                      | otherwise         = Left c

corruptionScore :: String -> Int
corruptionScore "" = 0
corruptionScore (c:cs) = case consumeBlock c cs of
                      Left c -> getScore c
                      Right s -> corruptionScore s

getCompletion :: String -> String -> String
getCompletion stack "" = map flipParen stack
getCompletion stack (c:cs) | isStartParen c = getCompletion (c : stack) cs
                           | otherwise      = getCompletion (tail stack) cs

main :: IO ()
main = do
  s <- readFile "./input"
  let notCorrupted = filter ((== 0) . corruptionScore) (lines s)
  let scores = sort $ map ((foldl (\acc x -> (acc * 5) + (completionScore x)) 0) . getCompletion "") notCorrupted

  print $ scores !! (div (length scores) 2)