Add solution for 2021 day 10
This commit is contained in:
parent
4b895a6c66
commit
7961d143bb
@ -13,3 +13,6 @@ trim_trailing_whitespace = true
|
||||
|
||||
[*.rb]
|
||||
indent_size = 2
|
||||
|
||||
[*.ml]
|
||||
indent_size = 2
|
||||
|
@ -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
3
2021/day-10/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*.cmi
|
||||
*.cmx
|
||||
*.o
|
6
2021/day-10/Justfile
Normal file
6
2021/day-10/Justfile
Normal 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
21
2021/day-10/common.ml
Normal 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
|
94
2021/day-10/inputs/puzzle.txt
Normal file
94
2021/day-10/inputs/puzzle.txt
Normal file
@ -0,0 +1,94 @@
|
||||
[{<<<{{[(<{{{{[]()}{[]()}}<[[]<>]<[]()>>}([[[][]]]<[{}<>]((){})>)}>)[<([<<(){}>{(){}}>[[<>{}](<>[
|
||||
<<{(<({[<{{<[<()()><{}{}>][[()[]][(){}]]>((<[]<>>(<>{}))((<>{}){<>()}))}<{({{}}[<>{}])({{}{}}{{}
|
||||
(<[(<<[((({<[{()[]}]>}{[[[{}[]]{{}{}}][{{}()}<<>()>]]})))<[([(<[{}{}]>{<<>{}>})<((<>()){[][]})<{<
|
||||
{({<[[[(<{{<<[[]()]>]}{<({<>{}}([]<>))[({}{}){()()}]>((<[][]>{[]()})[(()<>)[{}{}]])}}>){[<[{[{[]}{<><>
|
||||
(<{([(<[{[{<[<()<>>({}[])]>}][<{({<>}(<><>))}({[<>{}](<>())}{[{}()]<()<>>})><[<<{}<>><()[])>[<{}[]
|
||||
{<{[({<<<[[<(({}))>(([<><>][<><>])<<()()][{}{}]>)]][<[{<{}{}>(<>[])}](({{}[]}(()()))<<[]()>
|
||||
(<{[[(({(<([([[]{}][()[]]){{()[]}[<><>]}](<(<><>){[][]}>({{}{}}{{}()})))>){((([{()}[{}[]]]))[<[
|
||||
{<[(<([<{(<[[({}{})({}())]]>[(<<{}<>>([]())><({}[]}{()[]}>)([<(){}>][[{}<>][<><>]])])([{{<()()>[()[]]}[[<>](
|
||||
{(({([([<((<({<>{}}<[]>)(<[]{}>)>))({{{[<><>]<<><>>}[(<>{})[{}()]]}{[<[]{}>([]())]{({}{})}}}<({([]())}({<>()
|
||||
<<{<[<(([<[{[{[]{}}([][])]{{<><>}{<><>}}}{({<>[]})({<>[]})}]}<<[[[[]()]{[]()}]<{{}{}}[{}{}]>](<(()())[[][]]>[
|
||||
[{[<[{(([<<{{(()[])(<>[])}}{(<<>{}>([]<>))}]>({{((<><>)[[]{}])((()<>))}[[({}[])({}{})]([{}()]([]())
|
||||
{[{[{[{({([<{[<>()][(){}]}<[[]()]{(){}}>>]{{(([][])([]{}))}})}(<{{<({}())(<>[]>><[()()]>}{[((){}){<>{}}]
|
||||
[{{[<<[[(<({<{()()}(()[])>(<{}[]>)}[(<<>{}><[][]>)[({}<>]([]{})]])[<[<<>()><[]<>>]{<[][]>[<>]}>{[{{}()
|
||||
{[(([<({[<(({([][])<{}()>}{<{}[]><[]<>>))(<{[]<>}[()()]>))({{({}{})}<<<>[]>([][])>}[[{()[]}<<>{}>]{(<>[])
|
||||
{[<[(({{{{[((([]())[{}()])[[{}[])<{}<>>])((<()[]>{{}[]})(<<><>>(<>{})))]}}}[([[({[{}[]]<(){}>})(
|
||||
{[{((<[<{{([{([]())(<>{})}[<{}[]><[][]>]])<((<[]{}>{()[]})[(()())(<>())])(({{}()}([]())))>}}>[{[<<<((
|
||||
[{[[{{{<{<<({<{}{}>([][])}{([]{})[(){}]})<{(<>[]){()[]}}{[(){}][[]<>]}>>>}[[([([<>{}][[]{}])[
|
||||
[((<({<({([<(<<>()>)[{()<>}{[]<>}]>[[{{}[]}<{}()}]{{()[]}}]]{[<<[][]><<><>>><(<>)>]<{{()<>}[[]<>]}{(
|
||||
[<{[[[(<(({(<({}<>){<>[]}>){(<()<>>{()()})[{[]()}([]())]}}{[[<()[]){<>{}}]<<<>{}>[(){}]>]<[[()<>]<()[
|
||||
<{<<[[[[{[<<{({}<>){()[]}}([<>()]{<>})><(<<><>)<()<>>)[<[]()>[<>{}]]>>(<<({}{})[[][]]>([<>{}]({}<>))>{<<<
|
||||
{[[{<({<{(<[(([]{}){[]{}})]<[[[][]][{}{}]]{[{}<>}{{}<>}}>><({[()[]]<{}{}>}{({})})<<<()[]>[{}()]
|
||||
<[[{{([({[{(<{()()}>{(()[])(<>[])}){<[<>[]]{[]()}>[<[]<>>[<>()]]}}]})]<{{(([<<{}()>>(([]()){{}()})]<[(<>[]
|
||||
[(<<<({{<([{(((){})[{}[]])[{[]{}}(()<>)]}]{({{<>{}}[<><>]}{(()<>)})[[([]{})<()[]>]{(<>[]]<{}{}>}]})[{[[(()
|
||||
[<[{{[({{{({(<[][]>{[]})[{{}()}<[]{}>]}<[<<>()>({}{})]<{[][]}<[]{}>>>)}<[[{<<>()>(<>{})}<<()<>>[<>{}]>]]<<<
|
||||
<<<(<<<(<[{<(<(){}>((){}))({()<>}{<><>})>}([<({}())[<>()]><{{}()}([])>][{(())}(([])<<>{}>)})]>)>[[{((
|
||||
{[[(((<[{[{[<({}())((){})>[{<>{}}<<>{}>]]{{<<>()>{[]()}}<[()]({}())>}}(([{<><>}][<<>[]><[][]>])<[({}[]]]({
|
||||
{(<(({<({<[([<[][]><()()>]({{}{}}[{}<>]))<<(()[])[<>[]]>{{[]{}}(<>[])}>]((<[{}[]]>{<()()>[<>{
|
||||
<([{[([(<[([{<[]{}>[<>()]}[{<><>}]])[([([]<>){()[]}])[({{}[]})<[<>[]]<[]<>>>]]]>)])[(({<<{{[<>{}]{<>()}}}
|
||||
(<({{[<<{<{<{{()}{[]<>}]{{(){}}[[]()]}>{{{[][]}[()]}[(()<>)(<>[])]}}{{(<[]><(){}>){(<>())([])}}{[(<>)({}<>)
|
||||
{<[({{<<{([([((){})](<{}[]><<>{}>))([<<>[]><()()>])][<{{[]<>}(()<>)}<<()[]>{(){}}>>{(<()[]>[{}])
|
||||
[<[[{[{{[{(<[<()<>>]<[(){}][{}]>>)}([{{{(){}}[<>{}]}((()<>)<[]<>))}][({<[]{}>{[][]}}({{}{}
|
||||
{[[([<((<(([(<<><>>{[]()})]<{[()<>]{{}{}}}{[{}[]]<[]{}>}>)((<<[]<>>>({<>()}))(<[[]{}]>{{[]{}}[(
|
||||
{({[([{(<{{(<[<>[]]<[]<>>>(<{}[]>[<>[]]))<([{}<>]{{}()})<[()()]([]{})>>}(<{<<>{}>([][])}><(({}[])
|
||||
{<<{[([{({[[<<<>[]>[[]{}]>]({{<>[]}(()<>)}{[{}<>]<()[]>})]<({[()()][{}[]]}<<()()><(){}>>)>}[([
|
||||
<{{([<{{{{([(([]<>){<>{}})][<<<><>>[(){}]>])}{<([({}<>)<<>[]>])({{[]()}(()())){{<>[]}({}{})})>(<({<><>})><
|
||||
<({<{[<<({(<{[{}[]]{{}{}}}{[(){}][[]<>)}>[(<(){}><{}{}>){(<>)[[]{}]}])}(<[<(()())><<<>()>{()()}>][
|
||||
{[[<<{<({<[(([{}]<<>[]>))[<{<><>}({}{})>]]{([{()<>}{[]()}][{()()}<(){}>])(<({}{})<[]{}>>[[<>{}]
|
||||
{<[({{{[(<(({{()<>}(()[])}[<<>{}>{{}<>}])<{({}[])<()()>}{(()())<{}()>}>}([<(()())([][])>[([]){()<>}]]
|
||||
[{<[{{(([{{<({<>[]}(<>()))<[[][]]>>(([{}]({}())){[()[]]((){})})}{((<<>[]>[[]<>])<[<>[]}([]())>)
|
||||
[<<<{[((({<[((()<>)<()[]>){{<>{}}({}())}]{{<[]()>[[]()]}{[()]<{}[]>}}>[[{[<>()]}((<><>><()<>>)](([[]()](()))[
|
||||
(([<<{<[<[({{<()<>><[]{}>}{{()()><<><>>}})<[[{(){}}[[][]]][[[]<>]]]{{((){})}[[<>{}]<[][]>]}>][[({({})[[]
|
||||
<<((<({[(({[[[{}{}][<>]](<[]{}>[<>{}])]{(([]())(()<>))<[[]()]([]<>)>}}{[(<{}()>((){}))<{{}{}}[()<>]>]({<
|
||||
{<{(([(([{{{({<>}({}{}))[[[][]]([]<>)]}<<(()[])[[]]>((<><>)<[][]>)>}}[[[(<[]{}>)]{{([]()){{}<>}>[<[
|
||||
([[[{[([{{<{<<()()>(()[])>{({}()){<>[]}}}><(((()())({}[]))([()[]]<<>[]>))[[{{}[]>]{((){})}]>}<[[[[
|
||||
{[[[{(<[{(({<<{}()>{<>{}}>[[[]()]<{}{}>]}{[[{}[]]{{}}]((<>))}))[{{{(()[])<{}{}>}<<[][]>{<>()}>}{[{<>{}}<(){}>
|
||||
<({[<[(<<{[{<{{}<>}(()())>(<(){}>[<><>])}[([{}]{()[]})<{<>{}}{{}}>]]<{([()[]]<()[]>)[([]())([]<>)]}((((){}
|
||||
{[<<[{([((([(({})[{}<>])({[]()}{<>{}})][{{{}<>}}(({}<>))]){<{<<>()>[(){}]}(({}<>)<<>[]>)>[<[()[]]([][])
|
||||
<<([<<{[<<<<<(<>{})<{}{}>>>{([()()]<<><>>)(((){}))}>{<<[[]<>][(){}]>[(()<>){[][]}]>{(<()()){<>{}})<[[]{}
|
||||
[{<{[{{([<({({(){}}<{}<>>){[{}{}]{()<>}}}[{<{}[]><<>[]>}<[[]()][{}<>]>])<{([{}[]][<>[]])<[<>()](()[])>}[(<
|
||||
({([{{<[[[({<(<>())<<>()>>([()[]](<><>))}{<(<>[])<{}()>>({{}[]}{{}<>})})[<{[[][]]({}[])}{{{}[]}}
|
||||
{<[([{<[[<([{<[]<>>(()<>)}[(<>{})<{}{}>]][({<><>}<[]()>)(<()()>[{}{}])])>]]<[[({([<><>]{()})<{<>()}{<>
|
||||
[(<[[{[{{{<({[()()][[]<>]}<(<>[]){[]{}}>)<[{()()}{[]}]<[{}{}]<{}[]>>>>}{([<[()[]]>{([][]){()[]}}])}}}]<{<[
|
||||
{{<{<((<<{[<{[<>()]<<>[]>}<<[]()><{}()>>>({(<>[])<<><>>}{(<>[])(()())})]}>[[[[{<[]()>[()[]]}((<>{})[()<>])
|
||||
[<<[<[{(<({{[{[]<>}<{}()>][[<>[]][{}<>]]}<(({}{})({}[]))({[]<>}[{}{}])>}<[(<[]{}>[()()])][[({}[])(()
|
||||
<{[{[<<(<{([{({}{}){<>{}}}{[()](()<>)}][[({}())({}<>)][<()<>>{<><>}]])}<({{((){})(()[])}}(<[{}[]]<[][]>><{
|
||||
{(<{{[[[<<{{<([]<>)<{}()>)({{}{}}([]{}))}[{({}{})}<[()]>]}{[[<{}<>>](({}{})([][]))]{([{}<>]{()<>
|
||||
<([[{{<(<{(<((<>())[[]{}])>((<{}<>>{()<>})[{()()}(()<>)]))}[{(<{{}{}}[{}()]>)}([[[{}[]]<<><>>][<()()>[
|
||||
[(<[{<({{<<<<{<>{}}(<><>)>[{[][]}[[][]]]>{([(){}](<><>))((()<>))}><<{{{}<>}{{}<>}}({<>}<[]<>>)>>>{{{(([])[{}{
|
||||
((({[[[[<<<{<<[][]><<>()>>[<()[]>(()<>)]}{{<<>{}><<>[]>}{[{}{}][()()]}}><{[[{}[]]<{}[])]{{<>[]}
|
||||
[({[[<<<<{[<(<[]<>>[<>()])[[<><>]<<>()>]>>{[{(<><>)[()()]}{(()())[<>{}]}]{{<<><>><[][]>}({<>[]}(()<>
|
||||
{[[{(<<([[{(<[{}]>{{<>{}}{{}[]}})}]{<({[(){}][()()]}{(<>{})[[]]})>{<{[[]()][<>[]]}(<[]()>[<>{}]]><{[{}()
|
||||
({{[[<<({<<{(<<><>>[{}<>]><((){})>}{{{[]()}([]())}[{{}{}}[<>]]}><<[[{}[]]([][])]<[{}[]]{{}{}}
|
||||
<(((<(<<<[[[<{[][]}>{<[]>{<>[]}]]{<<{}[]>[()()]>}]([([()()]<[]()>){{{}()}}]<(({}{})(()()))
|
||||
[{{<[{[<<<[[<<{}<>>({}<>)>[<[]()>(()())>]<<<()[]><[][]>><[[]{}]([]{})>>]>><[{(<([])<{}()>>(<{}<>><<>{}>)
|
||||
<<<(<[[[<(<<[[[][]][{}{}]]>>(<([[]()))({{}[]}[[][]])>))><[{(<({}<>)(()<>)>{[{}<>]})[({<><>}[<>()])<
|
||||
<{<{[(<<([[(<[[]<>][{}[]]>(<()<>>(<>{}>))]]<{{([{}()]{<>()})<(<>()){()}>}{<<()()>(<>[])>{(())[[]{}]}}}(<{[{}
|
||||
{[[[[[[{[[{[[[()()][()[]]]{{{}()}(())}]}{<([{}()]{<>()})<[()<>>[<>()]>>[(<[][]>{[]<>})[<{}<>>]]}]]((({({(){}
|
||||
(<{(<([([{({{{[][]}{[]<>}}[<{}[]>[<>[]]]})}(<[{{(){}}(<><>)}((()[]))](([{}()]([])){([]<>]{{}()}})>[[[{(
|
||||
<{{{<<[<({([[[()[]]]][{(()[])(()())}])}){<{[{<[]()><[][]>}]([{[]()}<{}[]>][([]())[()[]]])}[<[(<>()){
|
||||
(<{{[<<[<{[[[(<>[])[<>()]][[[]()][{}()]]][([<>])<[<>[]][[]<>]>]][[({{}<>}[{}{}])({[]()}{()<>})]]}(<([
|
||||
{[({([{(((([{<[]()>}<[()()]([])>]{<[()[]]{[]<>}>[<<>{}><()>]}))))<([<[[<()[]><<>()>]][(<()[]><{}<
|
||||
({([<[[{([([<{<>()}>[({}[]){<><>}]][((<>){{}{}})(({}())<{}{}>)])])<((({({}{})[<><>]}(((){}))){((
|
||||
{[(<[[[{{[{<<{()()}(()())>({()()}[{}{}])><[{(){}}[[][]]]>}<[<(<>{})(<>())>({{}[]}{[]<>})]<{<{}<>>}{<{}<
|
||||
<<[({<<{([([{{<>}[<>()]}]{[[[]()]{<>()}](([]{}))})<<[{[]{}}([]())][<[]{}>{[]{}}]>({({}[])}
|
||||
<<({[[{{<[<[{[(){}][()[]]}]({{<>}<<>{}>}((<>())<{}()>))>]{{{({[][]}[[]<>])({<><>}({}))}{[{()()}[<>{}]][<<
|
||||
{<<[<{<{{({{((()[])[[][]])[{[][]}[<>{}]]}{[{<><>}([][])][[[]]((){})]})){{{([{}{}]((){})){{<><>}
|
||||
<[<{{(([(<<[<{{}[]}{[]<>}><([]{})[[]()]>]{{<()<>>([]())}{<()<>><<>[]>}}><[{[<>[]]<[]()>}<<[][]>{[]<>}>][{(()
|
||||
<([{{[({[([{(<()()>[<>[]]){<[]{}><[]{}>}}])]<{[[<[{}<>][[]()]>[{{}()}[<>[]]]]{{([]{}]{()<>}}[[()<
|
||||
({{{<<<[<<{(<([][])(<>{})>((()[])))}{({<()()>[<>{}]}{{{}[]}(()[])})<[{()())<<>[]>]({<>}<[]<>>)
|
||||
<<<{(<({((<({<()>(<><>)})[[<{}()>(<>())]]>(<<({}[])[<>[]])>({{[]()}(<><>)}(([][])<<>()>)))){[[([<>][{}])
|
||||
(<((<([({[<<{<{}()>(<><>)}{[[]{}]({}())}>>(<(<<>[]>)<[<>()]<<>{}>]><{<{}<>>}{{<><>}((){})}>)]{[<(<{
|
||||
{(([<{(<{<<<(([][])(()[])){<()[]><{}()>}>{(([]<>)<<>()>)<<{}<>>(<>())>}>{(<{{}<>}<[][]>>[[<>{}]<<>{}>])([(<
|
||||
{{{{<([([[[[(({}{})<<>>){{<><>}<()[]>}]([[(){}]<{}()>][[<>]<{}{}>])][<([[]<>][{}{}])[<[]<>>[[]<>]]>{<{[
|
||||
[<(({{<(({[({[[]<>]<[]<>>}<{{}<>}<(){}>>)[([()()][()[]])<<()()><<>()>>]](<<[{}[]]{[][]}>({[]}[<>{}])>{[[[][]
|
||||
(({({{([{{({<<(){}>(()[])}}{{([]<>)[{}[]]}})}<(<{[<><>]}>[[<{}>[{}()]]])<{{{<>()}<<>[]>}}[(<<
|
||||
[<((<((<[<{{(<<>[]><()<>>)[<<><>><<><>>]}{([()()][(){}])}}><<<<(<>())[{}]>[([]<>)({}{})]>[[<{
|
||||
{<<(({({{{(<{{()[]}}[({})({}())]>)}}}(<[[[[{()<>}<()<>>]([[]{}][<>{}])](<<{}[]>{{}<>}>(<{}<>>{[]{}})>]][({{
|
||||
<<{([[[<((<{{(<>[])[()[]]}([[]()])}>))>]]]<<({[(({<{[][]}({}[])>{<{}{}>[<>{}]}}<<[<>()][[]()]>{
|
||||
<[[({([<[<{([{[][]}[<><>]]{([]<>)(()<>)})[<(()())<[]<>>>[<[]<>>(()())]]}>(<(<[[]()]<[][]>>{[[]()]{[][]}}
|
||||
[<([[({[{{<{{([][])}(<<><>>(()<>))}[[<{}{}>{{}[]}]{{[]{}}}]>}}[<<[{({}{}){{}()}}({[]{}}([]{}))][{[()()]
|
||||
((({<(<{(<([<{[]{}}<(){}>><{[][]}(()<>)>](<[[]()]({}[])>{[()<>]}))<{(<[]()>[[][]]>[<[][]>[<>()
|
||||
{({<({(<([<([([]{})({}[])])>])<((<[[[]{}]{<>[]}]{{<>[]}<<><>>}>)(<({[]{}}{[]()})[{<>[]}<{}{}>]>{({
|
||||
{{{[<<<{<(<{<[[]<>]<[]()>>}<<(()[])([]())><((){})<{}[]>>>><(<((){})<[]>>(([][]))){<[[]{}][()[]
|
||||
[<({[(({([<(<[{}<>]>(<()[]>{[]{}})){{{[]()}}[<()<>>[<>[]]]}>]({<<{[]}[<>()]>(<<><>>{[]{}})>[<({}{})[()<>]>[[<
|
10
2021/day-10/inputs/sample.txt
Normal file
10
2021/day-10/inputs/sample.txt
Normal file
@ -0,0 +1,10 @@
|
||||
[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]
|
29
2021/day-10/part_one.ml
Normal file
29
2021/day-10/part_one.ml
Normal 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
35
2021/day-10/part_two.ml
Normal 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 ""
|
Loading…
Reference in New Issue
Block a user