add day 10

This commit is contained in:
opfez 2021-12-12 21:36:03 +01:00
parent ed3846c40b
commit 0dea2e35b0
3 changed files with 205 additions and 0 deletions

45
day10/first.hs Normal file
View File

@ -0,0 +1,45 @@
type Parser = Char -> Maybe Char
parseChar :: Char -> Parser
parseChar c x
| c == x = Nothing
| otherwise = Just x
mapOpenClose :: Char -> Char
mapOpenClose c = case c of
'(' -> ')'
'[' -> ']'
'{' -> '}'
'<' -> '>'
_ -> error "not element in map"
score :: Char -> Integer
score c = case c of
')' -> 3
']' -> 57
'}' -> 1197
'>' -> 25137
_ -> 0
openDelim c = (c=='(') || (c=='[') || (c=='{') || (c=='<')
closingDelim c = (c==')') || (c==']') || (c=='}') || (c=='>')
-- Keep stack of parse functions. Apply the top one on any closing delimeter,
-- check for Just x.
parseLine :: [Parser] -> String -> Char
parseLine _ [] = ' '
parseLine parsers (c:str)
| openDelim c = parseLine ((parseChar $ mapOpenClose c):parsers) str
| closingDelim c = case p c of
Just _ -> c
Nothing -> parseLine (tail parsers) str
where p = head parsers
solution :: [String] -> Integer
solution = foldr (+) 0
. map score
. filter (/=' ') -- painful, I blame Haskell's lack of proper sum types
. (map $ parseLine [])
main = interact $ show . solution . lines

102
day10/input Normal file
View File

@ -0,0 +1,102 @@
(({({({(({{<{{()<>}}>}}([<[(<>{})(()()))[[{}[]]{<>{}}]>(<(<>[])<[]()>><[{}[]]([]<>)>)]{{({{}[]}{{}{}})}})
[{(<(<<([[[{(([]{})<(){}>)<{<>}(()[])>}]]{[([(()[])[[][]]]<((){})((){})>)({{{}[]}<{}>})]}]){{[[<<{<>}(<
((<[([(({<<<{{<><>}({}<>)}{{<>{}}[[]()]}>>([<[{}{}]>{[()<>]<<>()>}]<(<{}()><[]{}>)<<[]<>>}>)>}[<{<([<><>]<
[<[<({{({{[({[[][]]<{}<>>})<[<{}{}>]([<><>](<>{}))>]{[{[[]{}}{{}{}}}<(<><>)({})>]{({()()}[<>()])
[<{<<({[{{{(<({}()){{}[]}><(<><>)[[]{}]>)}<(([{}{}]{<>[]}))<(<<>[]>){<[]>(<>{})}>>}({{((<>())<
{[([<[<({((([<[]()>{{}()}]<[{}{}]{[]()}>){{<<>[]>}(<<>()>)})([<({}<>)<{}()>>{[()[]]({}[])}]))(([{<
(({[<[((<([[(<[]()>{{}[]})([[]{}]<<><>>)]({{{}()}((){})})]<[<{[]{}}(()<>)>{(()[])<(){}>}]((<()
([<[[((<<({({([]{})<()()>}({()()}([]())))<{(<>[])<()[]>}>}>{{<[[<>()](()<>)](<[]{}><<>()>)
[[{{(<{[[<<{{<()()>(<>())}<{<><>}<<>[]>>}>{<<({}[]){()<>}>([()[]]({}<>})>(({<>()}[[]<>])<{()<>}{<>()}>)}>[[[
<[<{[{{[[(<[{<[]>[[]{}]}<[(){}]<[]<>>)]><[<(<><>)(<>{})>{<<>{}>[[]{}]}]<[[[][]]<<>()>][<<>
<{[<<({[(<[<((()<>)[<><>]){((){})[()[]]}>[{([][]){[]<>)}{[{}[]](<>{})}]]{{{({}[]){()()}}({(){}}{()})}[[{[]
[<([[(<[{<<<<{[]()}<<>{}>><{[]{}}((){})>>>[<<(<>())<{}<>>><({}<>][<>()]>><<[{}[]][<><>]>(<()[]>{{}})>]>}(<<[
(<(<[({([{(<([[]()]<()[]>)[(()())]><({{}[]}<(){}>)[(<>[])(<>[])]>)[<[<<>{}>[{}[]]]<<<>[]>{()()}>>
[{[{<[<(<<<<([()<>]{()<>}){({}[]){{}{}}}>{([[][]]([]()))<<<>{}>{{}[]}>}>{({{[]<>}[<>{}]}[<[]()>((){})]>[([{}
[{{({<<{(<(<(<<><>><<><>>){<{}[]>)>)>)}>>{[<{[<<{(()<>)<{}{}>}{<()()>(()[])}>([<<>[]><()<>>]<[{}{}]<()<>>>)>
<{<{({<{<<<{(({}{})(<>[]))}[([{}()]({}<>))]>[<[<<><>><[]<>>]>]>[[((<[]<>>)[[()[]]<[][]>])<<{{}[
([([(({<<(({{((){})[{}()]}((()()){{}<>})}[[(()())]])(<(((){}){[]()})({{}[]}{{}{}})>{{[<>[]]{[]()}}[[{}<
{[[{[{[{<({[<{[]<>}({}<>)><[<><>]>]<<({}<>)<<>()>>>]<([{<>{}}]<<<>[]><{}()>>){<[()[]][<>{}]>[<<>
<<[<[(<<(<(((<[]<>>){{[]()}(()[])})[<{[][]}(<>())>{<()<>>{()[]}}])(({[[]{}]<{}<>>}(<()<>>[()[]]))<[[
<[<([([([{[<[[<><>]]{([][])[<>{}]}>[{{[][]}{<>()}}(({}[])<[]{}>)]]{<({[]}[()[]])>}}([([(()<>)]{([]{}){{}
{<<[{[{[{(((([<><>](<><>))[<(){}>{<>()}])[{<<>{}>}(<<><>>[()[]])]>({({()<>}({}{}))<[{}[]]{()<>}>}<
{(({((((<[{(<(<>())><([]<>)[{}()]>)[([{}[]]{[][]})[<[]{}>{<>()}]]}<[{{{}<>}[{}[]]}]{<<()>>}>][({(<{}()><{}[]
[([[<<[[{[{<<[{}<>]>(({}())(<>{}))>}]}{{[(({{}{}}[()<>])<(()<>){[]()}>)<<{[]<>}{<>[]}><[[]{}]([][])>>
([<{[(<([{<{[<<><>>[[]()]]<({}<>)[{}{}]>}<<[[]()]{<>{}}><<[][]>[{}()]>>>}{<{<{[]{}}[[]()]>(<
((<<[[<(<[(((<<>{}>(()[]))({[]{}}([]<>)))[{<<>[]><{}[]>}{(<>[]){(){}}}]>[[[[{}<>]<()()>]](<(()<>)(<>)>{[
[(<((<<<[(<<<<[]{}>{()()}>>>{((([]())([]()))){{{[]<>}{<>[]}}{[{}()]{<>()}}}}}]{<<{(([]{}))[<{}<>>({
<<[({[<(<[{(({{}()}{{}<>}))}<<{[[]()][<>{}]}({<>()}<[]<>>)>>][<(<[()<>]>[(<>[])<{}{}>])>[{<[{}{}]([]())>((<>
[{{[<<[<[[({(({}())<<>()>>{{<>}[<><>]}})<<[[<>[]][[]]]({()<>}<()()>)>{{{<><>}}([(){}]<[]>)}>][{<{((){
{[({[[((([{<<<{}{}><()()>><([]{})>><[{()()}{<>{}}]>}][<[<(<>{})(<>{})>((<>{}){{}<>})]<{<[]()>(<>{})}(<
((<{({({({<{<<{}[]>({}{})>[<[]<>>({}[])]}>})})})}({{(<[[([<([]())([][])>][({()()}{{}{}})])[[{
{{{{{[((([({<<<>{}>(()<>)>{[<>[]]{{}()})}[(<[]{}>[<>()])<(()<>)[<>()]>])<({[{}()][()[]]}{[{}<>
{[(([{(<<<[([<{}{}>[{}<>]]<<<>()>[{}()]>)([[<>{}][{}<>]]{{{}()}<<>[]>})]><{{[([]{})[{}[]]]}{({<><
{<{([<[{[{[[[{{}{}}<{}()>]<{{}()}>]]([[([]())[(){}]]<<[]<>>{(){}}>]<[[{}()](()[])]{{()()}}>)}[(<[((
{{{{{[{{(<(((<<>[]>([][])){{()<>}[()<>]})(<{()[]}{<>[]}>({<>()}[(){}])))>)}((<{{<<()[]>(()<>)><<()()>[{
[[[(<[{[{([<<<<>><<>>><[[]{}](()<>)>>]<{[(()<>)[[]<>]]{<[][]>[[]()]>}[<([]<>)[{}{}]>{[(){}]([]
<[<(<{{{{<{[{(()())}[{(){}}{{}()}]]<<[<><>]({})>[(<><>)]>>>({{{(<>[]){()()}}<((){}){()[]}>}({({}()
<[{{[<([[[(<<<()[]>({}())><[{}[]]{[][]}}>)<([{[]()}{{}{}}]{[[]{}][{}<>]})[<<<>{}>><{[]}<<>()>>]>]{
{((<<{[{<[(<[[<><>]{[]<>}][[<>()][{}{}]]>[<{{}()}({}[])>[[<>[]][<>[]]]])<{<({}{})>{<{}<>]{(){}}}
<[{([<<{(([((([]<>))<<{}()>(()[])>)(([()[]]{[]()}))]<[<<{}()><[]{}>><([]<>)<(){}>>][{[<><>][<>
[<(<{[{[[(<{<[<>()]>}([<<>[]>{<><>}]{(()[])(()<>)})>)]((<<(<()<>>{[]{}}){<[]()>}>([<{}<>>([]())
{({[[<<{{[{<{<[]()>}{<{}<>><{}>}>(<{()<>}((){})><{<>{}}([][])>)}][[<{{()()}({}[])}<{<>{}}<<>{}>>>{<<[]()
[{<([<<[{[[<(<<>()><[][]>)>(<<<>{}><{}()>>[[{}<>]<()<>>])]][{([[{}()]]{({}())(()()]})(<<()<>>(()[])>)}<[([[]
[([((([[[(<<{[()<>][{}{}])([<><>]<{}>)>((<{}<>><()<>>)({{}<>}<{}{}>))>[{((()[])[(){}])}({{{}{}
<{[<[[{{<[{([[<><>]]<<{}()>([]<>)>)([(<>[])][({}()){()<>}])}{<([[]<>][<>{}])(<()()>({}[]))>
([{({[({{<((<(()<>){[]{}}><<<>[]><()[]>>)(<{[]<>}[{}[]]><{{}()}[[]()]>))>}}{[[[[<(()())<<>()>
{<<<[<<<{<<{<({}()){[]{}}>}>>}><{<{<[{[][]}(<>)]([[]<>][<><>])>}([{({}[])<{}{}>}]{[[()[]]][[<>[]]<[]{}>]})>}>
[[[(<<(<((([<([]<>)[<>]><([]<>)<{}{}>>]{({{}[]}<()<>>)[<()<>>]}))){{<{<[[][]](<><>)>{<{}{}>{{}[]}}}<[[[]{}]
[[{({[[((((<<(<>{})(()<>)){[<>[]]}>))[(<<<[]<>>{<><>}>>(({{}()}(<>{}))))(<[{[]<>}((){})]{<{}<>>
<<<(([[{{<<({{<><>}<<>{}>})>{[<<<>>[()[]]>[{()[]}{<>[]}]]{{[<><>]}(<{}<>>(<>[]))}}>}}<{<[(({{}{}}{[]()})
{({(({<({[{(<({}<>)[()()]><[<>{}]>){{(()<>){[][]}}([<>()](<>()))}}{[<<{}()>([]{})>[<<>()><<>{}>]][
{<{[(<([[{<({<()[]>[<>[]]}[[<>()]<()()>])>{[[{()<>}{()()}][[{}<>][{}()]]]([{{}<>}]<{<>[]}([])>)}}[<[[<[](
({(<({([{<[[[(<>]({}{})][<()><()[]>]]]<(<[()<>]>{{()[]}[()[]]})([[<>[]][()<>]](({}())([]())))>>}]{(({
{[[<<{[{<[[<((<>[])<{}{}>)>]{<<[(){}]<{}{}>>>[<{()<>}<{}<>>><[()<>]>]}]>><[[({<(<>[]){()<>}>}[[[{}<>]]([[]<>]
<{<<{[{<{([{[[[][]]]}]((<{[]<>}{()}>(<[]{}>[()[]]))<{{()()}[[]{}]}<{<>[]}{<>[]}>]))}<{[([<[][]>
({[({[[[{(<[{(<>())([][])}<([]())({}())}]>({(<()<>>{{}{}})<[()[]]<<>>>}([[{}[]][()]]{{{}[]}<[][]>})))[
[{[{{{([{[({{<[]()>[{}{}]}<{()}<{}<>>>}<[<()[]>{[]()}]>){{{[<>{}][<>[]]}{<()<>>{<>[]}}}[{(<>[])<()[]>}
<<[[{({([([{{(()())<<><>>}}]<(({[]<>})<<(){}><<><>>>)>)(<{{({}[])<[]()>}{({}[])[<>[]]}}>)]<<[<[{()[]}<{}>]
{{[{<(<[<(({<<[]{}]><[[]<>]<<><>>>}<(<(){}>)>)[[([{}{}][[]{}])[<()()>]]<{{{}[]}{{}}}[[[]()]<{}[]>]>])
{<({(<{[[[({(((){})([]()))<(()())([]{})>}[((()()>([][]))])][{[{{{}()}{()()}}[[[]()]{[]<>}]]}{(({[]<>
([{{<<<([<[({<<>[]>{[][]}}(({}{})[<>{}]))]{{((<>{})){<()()>(<>())}}}>]({[([([][])]{[(){}](()[])}){<[{}()}>}
<[{<[<<{[[{[[({}())<{}()>]]([(()<>)][(()())(()[])])}<(<[[][])<()()>>)([[{}](<>[])](<{}()>{[][]
(<([{({{(({{{({}[])}({{}<>}<[]()>)}<<<{}>{()<>}>({<><>}{<>[]})>}({{[<>()][<>[]]}}(<({})<[]()>>)
[{(<[[[<<{{{(([]())([][]))<{()<>}(()<>)>}<[{[]()}[()()]][[[]()][{}()]])}({<(<>()){<><>}>}[[(()()){(){}}]{(<
[(([[{[{((((<<<>()>({}())>{{{}()}[[][]]})<<<{}{}>([])>>)<[{[[]{}]}{<{}{})<{}>}]<(<{}{}>{<>[]}){<[]{}>{
<{<<<((([(<{<<{}[]>[[][]]>[<<><>>]}>)<[[[<()()>{<><>}](<(){}>[[]{}])]][<{{<>()}({}<>)}>{[{<><>)]{(()<>)
(((<(([([[[({<[]>[[][]]}[{<><>}<[]()>])<({[]()}(()[]))>]<[{[{}{}]}(<[]{}><()()>>]<(<()()>)({<><>})>>]<(<
[[{<{({<(((<<{[]()}(()<>)>([{}()][()<>])><[([]<>)[()[]]]{{()()}(<><>)}>){[(<[]>)[[[]{}]{[]()}]]}){[[{<()[
{([{(<{({<((<[()]<()<>>>({{}}[{}<>])))>><{<{<[<>[]]<[]{}>>}<[[{}]({}{})]{(<>())<()()>}>>(<
<[<{{[({{([{{{{}[]}({}<>]}{<[]{}><()[]>}}<[[()][()()]]{{()()}({})}>]{[(<()<>><{}<>>)[[()]{[]{}}]]<{<[]
(<<<[((<[((<[<<>[]>(()<>)]{{[][]}<{}<>>}>[[<{}()>[{}<>]]{{(){}}[{}()]}])<<([{}[]]{(){}})(<[]
({({[<[[{<<([((){}){[]<>}])>({[<()>(<><>)]})>[[({{(){}}}[[<>[]]<[][]>])[<(()())(<><>)>{([])}]]({[([]<>)
<[[[{(([[<[((<<><>>(<>[])}[{<>[]}[<>[]]]){<[<><>]>[(()[])[[]{}]]}](<{<[]<>>(<>[])}((()())<[]<>>)>({<(){}>
[{{{[[{([{{[{(<>())([])}]<(<[]><{}>)>}}{[{[<()[]>{()}][{()<>}{[][]}]}((({}<>)({}()))(<<><>>
[[{[[{{{{{<(<[[]<>]{[]}>)(<<()][()<>]>[{[]()}{<>[]}])>[(<<[]()>{[]<>}>(<[]<>>{<>{}}))[[{<>()}
([[{[[[([(<{[{{}{}}{<>[]}]{{<><>}}}>)<(<{<(){}>[<>[]]}{<{}[]>{{}[]}}>[[({}<>)(()())]<<{}{}><[]()>>
({{{([[({({({{()[]}({}{})}({()<>}[{}<>]))}{{<[{}[]>[[]()]>{[{}()]<[]<>>}}[(<()()><{}{}>)({[]<>}<<><>>)
{{<{{{[{((<[([[]{}]<<>{}>){<<>()>(<>[])}]{{([]()){[]{}}}}><({{{}[]}<[]()>}{<<>[]><[]{}>}>{<[()()](<>
[{([<[<([[[<<(()[]){(){}}>[[{}{}](<><>)]>({[{}()]}<(()<>){()[]}>)]]<<[[(()[])<<>{}>]{<()<>}<{}<>
<[{<({{[([[((<{}>[{}[]])((<>[])([]{})))(([{}[]]{{}()})({{}()}(<>{})))][[(<<>[]>){[{}{}]{[][
<<<<[(({{({([<{}()>{<>[]}](({}<>)<<><>>)){([[]()]{<>{}}){(<><>)}}}){<{[[<>()]]({{}{}}<()()>)}>[[([()](()[]))]
<({[({{{({<{([<>()]){[[]{}]([])}}(<[<>[]][()<>]>([<>[]][<>{}]))>[[((())([]()))<([]<>)((){})>]<
((({[<((<{<([({}{})[{}<>]][<(){}>({}{})])({[[][]]{<><>}}<<<>()><()()>>)>}([[[{<>[]}][{<>[]}
[{[[(((<{<{({(()[]){[]{}}){([]())[<>[]]})[(({}){[]{}})({[]{}}{()()})]}<{({<>{}}){({}<>){()()}}
{(<{[({<(([<<([]{})<(){}>>([{}[]]{{}{}})>]{<{[<>()]{[]<>}}>}))>({{<{<(<>{})([]{})>}<{{()<>}<[][
{(<<[([<[<({([()[]]{<><>})[[<>[]]([][])]}{({[]{}}[[]])(<[]()>)})>]>{<(({[[[]][{}[]]][<{}{}>(()<>)]}<{[{}<>]({
{<({<<{(<{([[<[]<>)[<>[]]](<()[]>)])[[{{()[]}<[][]>}{[()[]][<><>]}][{<{}()>([]<>)}(<<>[]>({}[])
<({[[<{{<[{[<{()<>}>][<[[]<>](<>())>({{}()))]}[((<{}()>([]{}))(([]{})<<><>>))<<{<>{}}[{}[]]>>
{[({[(<([([<[<{}<>>[<><>]]{{<>()}{()()}}><([()<>]<<>()>)({<>[]}({}()))>]<<[[()[]]<<><>>]<((){})(<><
<((({<(<({{{[({}())(()())][(<>)]}[[{{}[]}[<>[]]]([[][]]{()()})]}({{([]{})<[]()>}[(())({}<>)]}<{{[][]}{()()}}
[{{([[([{{{(<<[]()>[()[]]>{{[]<>}[[]()]})[<([]<>)<[][]>>]}}}(([[{(<>{}){[]{}}}{[(){}]{<>[]}}
([[{[{<[([[<[[()[]]([]())]<{<>{}}[<>[]>>>{{(<>())([]<>)}<({}[])<[]{}>>}]{<<{[][]}{<>()}>{(()())<(){}>}><
(<[({<(<(((<[<<>()>{{}()}]{(<>)<()()>}>([(<>[]}<[][]>]{{{}()}(<>)}))[[[{{}[]}{{}}]([[]<>]<{}()>)]])[{<[{[
{<([{(([[[[{((<><>)(<>())){({}())<[]{}>}}<{(<>[]){<>[]}}<{{}<>}[(){}]>>]]][[(<{<{}()>{[]}}[<<>>{{}<>}]>([
<{[[<({[[({([{()()}{[][]}]([[][]]<{}>))}{<[<{}())<[]()>]{(()()){{}<>}}>({[{}<>]{()[]}}<{()<>}
[<<((<(([(<{[[()<>]{{}<>}]([[]()][<>[]])}{(<[]>)((()[]){[]()))}>{{(([][])({}{}))<[(){}]>}})
[<[<{[{{[[[<<((){})<()>><{{}[]}{[]()}>><((()){[][]})>][<{<<>[]>[<>[]]}({<>[]}{()()})><{(<>{})<<>()>}>]][([[
([<[<{<{(((<([{}<>])([<>[]]([][]))>[<([]())<()<>>><<()()>{{}()}>])>[<<<{()<>}<<>[]>>{<{}()
<({{<(<([({<[{<>[]}]<[{}{}]{()[]}>>(({[]()}))}[<<{(){}}([][])>(<()()>{()})>{([[][]]<<>{}>)<<[][]>([]())>}
{[(([<([[<[<{[()]}>[<[<>{}][<><>]>{[()]{<>}}]](<{([][]){[]()}}[{()()}(<>[])]><[[[]<>][[][]]]<
<<({<<<({(<<[<[]()>{[][]}][<{}{}>{[]<>}]>[{{<><>}<()()>}(<{}{}>(<>))]>{[[<()()>[[]()]]{<<>[]><[]{}>}]})
[[[(<<(({[(((((){})(<>{}))<[[]()]{{}{})>)(<[()[]]{[]<>}><[<><>]{<>{}}>))([<({}<>)({}[])>[[(){}]
{[[{<[{([<[<{(<><>)}({{}}([][]))>]{{<[<>{}]>(<<>()>[[]<>])}}>({[<<{}<>><()<>>>(<<>()>{<><>})]}[(<<(

58
day10/second.hs Normal file
View File

@ -0,0 +1,58 @@
import Data.List (sort)
type Parser = Char -> Maybe Char
median :: Integral a => [a] -> a
median xs = xs !! mid
where len = length xs
mid = div len 2
parseChar :: Char -> Parser
parseChar c x
| c == x = Nothing
| otherwise = Just x
mapOpenClose :: Char -> Char
mapOpenClose c = case c of
'(' -> ')'
'[' -> ']'
'{' -> '}'
'<' -> '>'
_ -> error "not element in map"
score :: [Char] -> Integer
score cs = aux 0 cs
where aux res [] = res
aux x (c:cs) = aux (x * 5 + (delimScore c)) cs
delimScore :: Char -> Integer
delimScore c = case c of
')' -> 1
']' -> 2
'}' -> 3
'>' -> 4
_ -> 0
openDelim c = (c=='(') || (c=='[') || (c=='{') || (c=='<')
closingDelim c = (c==')') || (c==']') || (c=='}') || (c=='>')
-- Keep stack of parse functions. Apply the top one on any closing delimeter,
-- check for Just x.
parseLine :: [Char] -> String -> [Char]
parseLine closers [] = closers
parseLine closers (c:str)
| openDelim c = parseLine (mapOpenClose c:closers) str
| closingDelim c = case p c of
Just _ -> []
Nothing -> parseLine (tail closers) str
where p = parseChar (head closers)
solution :: [String] -> Integer
solution = median
. sort
. map score
. filter (/="")
. (map $ parseLine [])
-- main = interact $ show . solution . lines