Add solution for 2021 day 10

This commit is contained in:
Patrick Auernig 2021-12-10 16:54:38 +01:00
parent 4b895a6c66
commit 7961d143bb
10 changed files with 204 additions and 2 deletions

View File

@ -13,3 +13,6 @@ trim_trailing_whitespace = true
[*.rb]
indent_size = 2
[*.ml]
indent_size = 2

View File

@ -13,7 +13,7 @@
| 07 | ✓ | ✓ | [Swift] |
| 08 | ✓ | | [C#] |
| 09 | ✓ | ✓ | [C] |
| 10 | | | |
| 10 | ✓ | ✓ | [OCaml] |
| 11 | | | |
| 12 | | | |
| 13 | | | |
@ -45,3 +45,4 @@
[swift]: https://www.swift.org
[c#]: https://docs.microsoft.com/en-us/dotnet/csharp/
[c]: http://www.open-std.org/jtc1/sc22/wg14
[ocaml]: https://ocaml.org

3
2021/day-10/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.cmi
*.cmx
*.o

6
2021/day-10/Justfile Normal file
View File

@ -0,0 +1,6 @@
@part PART INPUT_FILE="inputs/puzzle.txt":
ocamlopt -o part_{{PART}} common.ml part_{{PART}}.ml
./part_{{PART}} {{INPUT_FILE}}
clean:
rm -f *.cmx *.cmi *.o part_one part_two

21
2021/day-10/common.ml Normal file
View File

@ -0,0 +1,21 @@
let rec read_line in_chan =
try
let line = input_line in_chan in
line :: read_line in_chan
with End_of_file ->
close_in in_chan;
[]
let char_list str = List.init (String.length str) (String.get str)
let read_file path = open_in path |> read_line |> List.map char_list
let is_closing ch = List.mem ch [')'; ']'; '}'; '>']
let is_pair o c =
match (o, c) with
| ('(', ')') -> true
| ('[', ']') -> true
| ('{', '}') -> true
| ('<', '>') -> true
| (_, _) -> false

View File

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

View File

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

29
2021/day-10/part_one.ml Normal file
View File

@ -0,0 +1,29 @@
open Common
let score ch =
match ch with
| ')' -> 3
| ']' -> 57
| '}' -> 1197
| '>' -> 25137
| _ -> 0
let rec count_errors line stack errors =
match (line, stack) with
| ([], _) -> errors
| (x :: xs, y :: ys) when is_closing x ->
if is_pair y x
then count_errors xs ys errors
else count_errors xs ys (errors + score x)
| (x :: xs, _) ->
count_errors xs (x :: stack) errors
let () =
let path = Sys.argv.(1) in
read_file path
|> List.map (fun cs -> count_errors cs [] 0)
|> List.fold_left (+) 0
|> print_int;
print_endline ""

35
2021/day-10/part_two.ml Normal file
View File

@ -0,0 +1,35 @@
open Common
let score ch =
match ch with
| '(' -> 1
| '[' -> 2
| '{' -> 3
| '<' -> 4
| _ -> 0
let rec score_remaining stack points =
match stack with
| [] -> points
| x :: xs -> score_remaining xs (points * 5 + (score x))
let rec score_closing line stack =
match (line, stack) with
| ([], _) -> Some (score_remaining stack 0)
| (x :: xs, y :: ys) when is_closing x ->
if is_pair y x
then score_closing xs ys
else None
| (x :: xs, _) ->
score_closing xs (x :: stack)
let () =
let path = Sys.argv.(1) in
read_file path
|> List.filter_map (fun cs -> score_closing cs [])
|> List.sort compare
|> fun lst -> List.nth lst (List.length lst / 2)
|> print_int;
print_endline ""

View File

@ -8,4 +8,4 @@
- [2018](2018/README.md) (0% completed)
- [2019](2019/README.md) (0% completed)
- [2020](2020/README.md) (16% completed)
- [2021](2021/README.md) (34% completed)
- [2021](2021/README.md) (38% completed)