22
22
-- will recommend what should go in place of the underscores.
23
23
24
24
module AOC.Challenge.Day10 (
25
- -- day10a
26
- -- , day10b
25
+ day10a
26
+ , day10b
27
27
) where
28
28
29
29
import AOC.Prelude
@@ -47,14 +47,63 @@ import qualified Text.Megaparsec.Char.Lexer as PP
47
47
48
48
day10a :: _ :~> _
49
49
day10a = MkSol
50
- { sParse = Just . lines
50
+ { sParse = mapMaybeLinesJust Just
51
51
, sShow = show
52
- , sSolve = Just
52
+ , sSolve = Just . sum . map countScore
53
53
}
54
+ where
55
+ countScore = go []
56
+ where
57
+ go [] (' )' : _) = 3
58
+ go [] (' ]' : _) = 57
59
+ go [] (' }' : _) = 1197
60
+ go [] (' >' : _) = 25137
61
+ go (s: ss) (' )' : xs) = if s == ' (' then go ss xs else 3
62
+ go (s: ss) (' ]' : xs) = if s == ' [' then go ss xs else 57
63
+ go (s: ss) (' }' : xs) = if s == ' {' then go ss xs else 1197
64
+ go (s: ss) (' >' : xs) = if s == ' <' then go ss xs else 25137
65
+ go ss (' (' : xs) = go (' (' : ss) xs
66
+ go ss (' [' : xs) = go (' [' : ss) xs
67
+ go ss (' {' : xs) = go (' {' : ss) xs
68
+ go ss (' <' : xs) = go (' <' : ss) xs
69
+ go [] [] = 0
70
+ go (_: _) [] = 0
71
+
54
72
73
+ -- \case
74
+ -- '<':xs -> countAngle xs
75
+
76
+ takeMid xs = xs !! n
77
+ where
78
+ n = length xs `div` 2
55
79
day10b :: _ :~> _
56
80
day10b = MkSol
57
81
{ sParse = sParse day10a
58
82
, sShow = show
59
- , sSolve = Just
83
+ , sSolve = Just . takeMid . sort . map getScore . mapMaybe countScore
60
84
}
85
+ where
86
+ getScore = go 0
87
+ where
88
+ go n (' (' : xs) = go (n * 5 + 1 ) xs
89
+ go n (' [' : xs) = go (n * 5 + 2 ) xs
90
+ go n (' {' : xs) = go (n * 5 + 3 ) xs
91
+ go n (' <' : xs) = go (n * 5 + 4 ) xs
92
+ go n [] = n
93
+ go n xs = error xs
94
+ countScore = go []
95
+ where
96
+ go [] (' )' : _) = Nothing
97
+ go [] (' ]' : _) = Nothing
98
+ go [] (' }' : _) = Nothing
99
+ go [] (' >' : _) = Nothing
100
+ go (s: ss) (' )' : xs) = if s == ' (' then go ss xs else Nothing
101
+ go (s: ss) (' ]' : xs) = if s == ' [' then go ss xs else Nothing
102
+ go (s: ss) (' }' : xs) = if s == ' {' then go ss xs else Nothing
103
+ go (s: ss) (' >' : xs) = if s == ' <' then go ss xs else Nothing
104
+ go ss (' (' : xs) = go (' (' : ss) xs
105
+ go ss (' [' : xs) = go (' [' : ss) xs
106
+ go ss (' {' : xs) = go (' {' : ss) xs
107
+ go ss (' <' : xs) = go (' <' : ss) xs
108
+ go [] [] = Nothing
109
+ go ss@ (_: _) [] = Just ss
0 commit comments