Skip to content
This repository was archived by the owner on Nov 17, 2024. It is now read-only.

Commit b9a5d39

Browse files
committed
raw day 10
1 parent c0d3c8c commit b9a5d39

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

β€Žsrc/AOC/Challenge/Day10.hsβ€Ž

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
-- will recommend what should go in place of the underscores.
2323

2424
module AOC.Challenge.Day10 (
25-
-- day10a
26-
-- , day10b
25+
day10a
26+
, day10b
2727
) where
2828

2929
import AOC.Prelude
@@ -47,14 +47,63 @@ import qualified Text.Megaparsec.Char.Lexer as PP
4747

4848
day10a :: _ :~> _
4949
day10a = MkSol
50-
{ sParse = Just . lines
50+
{ sParse = mapMaybeLinesJust Just
5151
, sShow = show
52-
, sSolve = Just
52+
, sSolve = Just . sum . map countScore
5353
}
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+
5472

73+
-- \case
74+
-- '<':xs -> countAngle xs
75+
76+
takeMid xs = xs !! n
77+
where
78+
n = length xs `div` 2
5579
day10b :: _ :~> _
5680
day10b = MkSol
5781
{ sParse = sParse day10a
5882
, sShow = show
59-
, sSolve = Just
83+
, sSolve = Just . takeMid . sort . map getScore . mapMaybe countScore
6084
}
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

β€Žtest-data/2021/10a.txtβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[({(<(())[]>[[{[]{<()<>>
2+
[(()[<>])]({[<{<<[]>>(
3+
{([(<{}[<>[]}>{[]{[(<()>
4+
(((({<>}<{<{<>}{[]{[]{}
5+
[[<[([]))<([[{}[[()]]]
6+
[{[{({}]{}}([{[{{{}}([]
7+
{<[[]]>}<{[{[{[]{()[[[]
8+
[<(<(<(<{}))><([]([]()
9+
<{([([[(<>()){}]>(<<{{
10+
<{([{{}}[<[[[<>{}]]]>[]]
11+
>>> 26397

β€Žtest-data/2021/10b.txtβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[({(<(())[]>[[{[]{<()<>>
2+
[(()[<>])]({[<{<<[]>>(
3+
{([(<{}[<>[]}>{[]{[(<()>
4+
(((({<>}<{<{<>}{[]{[]{}
5+
[[<[([]))<([[{}[[()]]]
6+
[{[{({}]{}}([{[{{{}}([]
7+
{<[[]]>}<{[{[{[]{()[[[]
8+
[<(<(<(<{}))><([]([]()
9+
<{([([[(<>()){}]>(<<{{
10+
<{([{{}}[<[[[<>{}]]]>[]]
11+
>>> 288957

0 commit comments

Comments
Β (0)