From 092a1b216218fef3fb71be1fbe2c7f3a10ec699b Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Sun, 9 Jan 2022 13:29:18 +0100 Subject: [PATCH] Solve 2021/10 --- 2021/day10/Cargo.lock | 7 +++ 2021/day10/Cargo.toml | 8 +++ 2021/day10/README.md | 88 ++++++++++++++++++++++++++++++++ 2021/day10/input/example0 | 1 + 2021/day10/input/example1 | 10 ++++ 2021/day10/input/input | 102 ++++++++++++++++++++++++++++++++++++++ 2021/day10/src/main.rs | 92 ++++++++++++++++++++++++++++++++++ 7 files changed, 308 insertions(+) create mode 100644 2021/day10/Cargo.lock create mode 100644 2021/day10/Cargo.toml create mode 100644 2021/day10/README.md create mode 100644 2021/day10/input/example0 create mode 100644 2021/day10/input/example1 create mode 100644 2021/day10/input/input create mode 100644 2021/day10/src/main.rs diff --git a/2021/day10/Cargo.lock b/2021/day10/Cargo.lock new file mode 100644 index 0000000..5f8ea61 --- /dev/null +++ b/2021/day10/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day10" +version = "0.1.0" diff --git a/2021/day10/Cargo.toml b/2021/day10/Cargo.toml new file mode 100644 index 0000000..40d2066 --- /dev/null +++ b/2021/day10/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/day10/README.md b/2021/day10/README.md new file mode 100644 index 0000000..9b9ab22 --- /dev/null +++ b/2021/day10/README.md @@ -0,0 +1,88 @@ +https://adventofcode.com/2021/day/10 + +## \--- Day 10: Syntax Scoring --- + +You ask the submarine to determine the best route out of the deep-sea cave, +but it only replies: + +[code] + + Syntax error in navigation subsystem on line: all of them +[/code] + +_All of them?!_ The damage is worse than you thought. You bring up a copy of +the navigation subsystem (your puzzle input). + +The navigation subsystem syntax is made of several lines containing _chunks_. +There are one or more chunks on each line, and chunks contain zero or more +other chunks. Adjacent chunks are not separated by any delimiter; if one chunk +stops, the next chunk (if any) can immediately start. Every chunk must _open_ +and _close_ with one of four legal pairs of matching characters: + + * If a chunk opens with `(`, it must close with `)`. + * If a chunk opens with `[`, it must close with `]`. + * If a chunk opens with `{`, it must close with `}`. + * If a chunk opens with `<`, it must close with `>`. + +So, `()` is a legal chunk that contains no other chunks, as is `[]`. More +complex but valid chunks include `([])`, `{()()()}`, `<([{}])>`, +`[<>({}){}[([])<>]]`, and even `(((((((((())))))))))`. + +Some lines are _incomplete_ , but others are _corrupted_. Find and discard the +corrupted lines first. + +A corrupted line is one where a chunk _closes with the wrong character_ \- +that is, where the characters it opens and closes with do not form one of the +four legal pairs listed above. + +Examples of corrupted chunks include `(]`, `{()()()>`, `(((()))}`, and +`<([]){()}[{}])`. Such a chunk can appear anywhere within a line, and its +presence causes the whole line to be considered corrupted. + +For example, consider the following navigation subsystem: + +[code] + + [({(<(())[]>[[{[]{<()<>> + [(()[<>])]({[<{<<[]>>( + {([(<{}[<>[]}>{[]{[(<()> + (((({<>}<{<{<>}{[]{[]{} + [[<[([]))<([[{}[[()]]] + [{[{({}]{}}([{[{{{}}([] + {<[[]]>}<{[{[{[]{()[[[] + [<(<(<(<{}))><([]([]() + <{([([[(<>()){}]>(<<{{ + <{([{{}}[<[[[<>{}]]]>[]] + +[/code] + +Some of the lines aren't corrupted, just incomplete; you can ignore these +lines for now. The remaining five lines are corrupted: + + * `{([(<{}[<>[]}>{[]{[(<()>` \- Expected `]`, but found `}` instead. + * `[[<[([]))<([[{}[[()]]]` \- Expected `]`, but found `)` instead. + * `[{[{({}]{}}([{[{{{}}([]` \- Expected `)`, but found `]` instead. + * `[<(<(<(<{}))><([]([]()` \- Expected `>`, but found `)` instead. + * `<{([([[(<>()){}]>(<<{{` \- Expected `]`, but found `>` instead. + +Stop at the first incorrect closing character on each corrupted line. + +Did you know that syntax checkers actually have contests to see who can get +the high score for syntax errors in a file? It's true! To calculate the syntax +error score for a line, take the _first illegal character_ on the line and +look it up in the following table: + + * `)`: `3` points. + * `]`: `57` points. + * `}`: `1197` points. + * `>`: `25137` points. + +In the above example, an illegal `)` was found twice (`2*3 = _6_` points), an +illegal `]` was found once (` _57_` points), an illegal `}` was found once (` +_1197_` points), and an illegal `>` was found once (` _25137_` points). So, +the total syntax error score for this file is `6+57+1197+25137 = _26397_` +points! + +Find the first illegal character in each corrupted line of the navigation +subsystem. _What is the total syntax error score for those errors?_ + diff --git a/2021/day10/input/example0 b/2021/day10/input/example0 new file mode 100644 index 0000000..0cdbcdf --- /dev/null +++ b/2021/day10/input/example0 @@ -0,0 +1 @@ +Syntax error in navigation subsystem on line: all of them diff --git a/2021/day10/input/example1 b/2021/day10/input/example1 new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/2021/day10/input/example1 @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/2021/day10/input/input b/2021/day10/input/input new file mode 100644 index 0000000..8914aed --- /dev/null +++ b/2021/day10/input/input @@ -0,0 +1,102 @@ +([({<(({<[(<([{}<>])<(()<>){<>[]}>>[[{<>{}}[<>{}]][(<>[])<{}<>>]]){[{{[][]}[[]{}]}({{}[]}[{}<>])]({({}{}) +([{{[(<{[{{<[{{}[]}{(){}}]{[{}{})([]())}>}(({{{}}{<>{}}}{(()[])(()<>)}){<<{}()><[][]>>})}(<<<(< +((<[[<([[({{[([]{})([][])]{({}())(()}}}({[[][]](()<>)}(<[]{}>[[]{}]))}<[[{()<>}<[][]>]]{{{ +{{[{{<[{[[(<((<>())(<><>))<<()[]>>>)<<<(<><>)[()()]><([])<[]>>>{{({}()){[][]}}<[[]]>}>]]<[ +[<{(<{<<[[[[(<{}()>{{}()})[[()()]<{}()>]]{{<<><>>({}<>)}(([]{})[{}<>]}}]{{({[]<>}{[][]})<[<>[]]<{}()>>}[{ +<<{<{<<{<<[(([<>()][<>{}])[<()<>>{()[]}])[<({})<<>[]>><<[][]>[[]<>]>]]>>((<<<(<>[])[{}{}]>[<[]{}>({})]>>)< +<[[[(<{[{((<{(()[])[{}<>]}([[][]][()<>])>[(<[]())<<><>>){({}{}){<>[]}}]))}]}>{<([<<[<(()<>) +({{{<<<{({[{[<()[]>({}())]{<{}()>({}{})}}]{([(<>)<[]<>>]{([]<>)(()())})(<<<><>>{{}{}}>[({}{})<() +{{<<[<[{<(({{({}<>)<<>()>}{{{}<>}}}{<({}())({}<>)>([<><>]{()[]})}))<[<{<[]{}>([][])}((<>())({}[] +[[<([({<<<<{<{<><>}{()<>}>}>{(([[]{}>[{}()])[[[][]]])(<[[]<>](())>(<()[]>({}<>)))}>{<[<{{}{}} +<<<[[(<[<<<{([{}[]]<[][]>){{(){}}[<>{}]}}><([{<><>}<[]()>]<[[]()]<{}{}>>)>>{(<[[(){}]](<<>()>[<><>] +<<[<[<{[<[(<[<()[]>]{[{}[]][[]<>]}>)[{{[()()]{{}()}}{{<>}{{}<>}}}<{(()[]]{[]<>}}([<>()]<[]()>)>]]><{{< +([([(<([((<{<[[][]](()[])}{<[]()>[<>[]]}}{({(){}}[{}[]]){{<>[]}[()[]]}}>[<({[]()}<()<>>)(<[]{}>([]) +<<{{([<<[{({[<{}[]>{()()}](<<>()>)}[[<()<>>{{}{}}]((<>[]){[][]})])([{((){}){[]{}}>{[{}{}]<()<>>} +[(<[[<{({{{{{{<>()}([]{})}([()()])}([[()()]([])](<<>()>({}[])))}[{[[()[]][()()]]{[{}<>]}}<{({}())<[]{}>}<<<>[ +[({{{[(<{[[(<[<>[]]<[]()>>{{[]<>}})]<{([[]<>]([][])){{()()}{{}{}}}}>]}{{{[<({}())>(([]()){ +<({([<[([<<(<<<>{}>>[{<>[]}])>><<<(((){})[<>{}])<(()[])<{}<>>>>>([[[()][()]]])>]{<([<{[]<>}[{}<>]>]<{[() +[([[{<({(([[{{{}<>)[[]()]}(({}<>)[{}{}])]]<<{({}<>)(()())}>{[[(){}]({}[])]<{{}{}}>}>)<{([[ +<(<<[{<<((<(<{<>[]}>[([]{}){<><>}])[{([]<>)(<>[])}(<()[]><[]<>>)]>))><[(<[([<>[]]<<>[]>){<()()}<{}>} +<{{<([<{[{({({<><>}({}()))<({}<>)<(){}>>}[(<(){}>[{}<>])<(<>[])([]())>])}[[{({()<>}({}<>)){(<>())<{} +[[(<({{<(({[{<()[]>{()[]}}]<[<()[]>{<>[]}]{{()[]}[{}{}]}>}(({({}<>)[[]{}]}[[[]()](()<>}])({{{}{}}} +({<<[[[<<[(<([()()]{(){}})>)]((([<()><(){}>]<{[]<>}[[]]>)<(<[]<>>(<>[]))>)({[(<><>)[<>]]{<() +{[((<{{{<(<<{{()()}}<({}<>)<[]<>>>}{{<(){}><<>[]>}{[<>{}]<()<>>}}>{<[([])<(){}>]><{[()<>](<>{}) +({([{<<(<{[[{[{}()][()[]]}([()[]]((){}))]<([<><>}{{}<>}){<[]<>>(<>[])}>]}[<[({(){}}(<>()))]>]><<({({<>{} +[<{([<{({{<[<<()[]>[(){}]>{{{}()}(<>[])}]>[{((<>{})<{}{}>){<{}[]>}}[(([]<>)(<>[])){({}())[{}<>]}]]}<[[[{{}()} +([{{({{{[(<(({<>{}}<[]<>>))((<<><>>){<{}>{<><>}})><<[([][]){{}()}]([[]<>])><({()<>}[[]{}])<({ +(([[<[[[([<<(<<>()>{(){}})<([]())<<>[]>>>{[<<><>>[[]]]}>])({({[<[]{}>]<[(){}][[]()]>}([<[]()>[() +(<[[<[({(({[{[[]()][[]()]}]}(<[(()<>)[<>]]{(()())[<>{}]}><{([]{}){{}()}}>)))})({[[<[[<<>()>(()())]{<()[] +[[([<<[[<<<((((){}){{}[]}){<{}<>><{}{}>}){{<{}[]>[<>[]]}(<{}>(()()))}>[(<(<>{}){{}()}>(([])<()>)}<(<<>()>)<{[ +{({{[[({{{{<({[]()}<[][]>)>}{(({[][]}[()<>])<<[]{}><[]()>>)<([[]](()()))[{[]<>}(()[])]>}}}}(( +{[(([({[[(<{([[]][[]<>])<<<><>>[{}{}]>}{<({}{}>{{}()}>{(<>{})[[]<>]}}>)((([{{}()}({}[])])[[{<>{}}]([()()])] +[[{(((<(<<<((((){})[()()])<[()()]{[]<>}>)>><<{([[]{}])}>[{({<>{}})((<>{})<[][]>)}({([][])[< +{({{<[<{(([[<<<>{}><()<>>>{(()())([][])}]{{{(){}}[()<>]}(<<><>>{[][]})}]{[<[[]{}]<{}()>>((<><>)(()()))]<(<()< +<[({(<[{(<([<<<><>>>{{()[]}<[]>}]<[(<>{})({}<>)]((()[]){[]{}})>)<<[[()()]]{<[]<>>(()())}>>}<[{([<>()]<( +<([<{[(((<((<{[]<>}<[]<>>>{<(){}>}))([[{<><>}{<>{}}]])>)(([[[({}())<<><>>][<<>[]>(<>())]]<<<<>{})[[] +<{<{[(<<[<<{{{()<>}(()[])}((()[]){(){}})}>[{({<>()}[[]<>])[{{}()}<()[]>]}{([()<>][[]{}]][[{}<>][(){}] +{([[{<({{[[{<<{}{}>{[]}}[(()<>){{}<>}]}(({[][]}{[]<>})[(<>)[()()]])]](<(<{()()}[<>]><[<>()]> +({<[[([{[{({[<()()>[<>{}]]{[<>[]]<{}<>>}})}[([<{[]}<<>{}>>[([]<>)]][[([]())<<><>>]])]]}])]{<([({[( +{{{{[[[{[[{(({[]()})){{(()[])<[]<>>}{<()[]>(<>())}}}]<{{<[<><>][{}()]>}[<{[][]}(<>[])>({[]()} +[<{{([(<<{[<[[{}()]]<{{}<>}<[]()>>>[<<()<>>[<>[]]>]][[{<<>()>[(){}]}[<{}>{{}{}}]](((()>{{}}){[[]<>][ +[(<((({(<{<(({[]{}}(<><>))({{}()}{{}<>}))[<[<>()][[]()]><{{}[]}([][])>]>((<[[][]]>({{}()}<[] +<[{{<[({{<(<<<{}<>>{(){}}>{{<><>}{{}()}}){([{}[]](()[]))[{{}<>}[<><>]]})(<[[{}<>]{(){}}]<{()[]}[<><>] +<<({{[(({<{{{((){})<()()>}{[<>{}]{{}<>>}}{({()<>})[([][])<{}[]>]}}[{{{()<>}<[]()>}[<[]{}>{[]{}}]}[[[()[]]][({ +{{[[([([[{<<{[[][]]}>{<<<><>>{()[]}>{[{}()][{}{}]}}>}][[<([{<>[]}{<>()}]{{()<>}({}[])>)(<<[]{}>{[]<>} +[[(<<{<[<[[<({<>[]}([]<>)}{<()()>}>]]{{<([(){}]{<>()})<[[]()][(){}]>><{<[]()>[<><>]}{(<>[])(()[])}>}}>[{ +<([{{<<{<({[<[{}<>][{}()]>[[()[]]{{}{}}]]<({[]<>}([][]))([(){}](()<>))>}<<({<><>}<<>()>)>>){[[([()<>]<{} +{<<[[[[{<(<[[(()())<(){}>](<()[]><[]<>>)]<(<()[]>[<>[]])(<{}[]>({}{}))>><{(([]{})[<>()])<<{}[]>>}>)<({<{{}{}} +<(({{([{{<([({()()}{{}{}})({[]<>}{<>[]})][(({}()){[]{}})({[]{}})]){<([<>]{<><>})[{{}[]}]><[ +[[{<<<<[<(<<<<[][]>(<>()]>{[<>()]{(){}}}>>{[<<{}()><<>()>><{[]()}{{}[]}>]<(<{}[]><<>()>)({[][]}([]{ +{[([(([[{{{[{{<>[]}}(<[]{}>)]>(((<{}{}><(){}>)([[]{}]))(<[{}{}]{{}()}>{[[][]][(){}]}))}}]](<(((<{<< +<{{([[((({{[[[[]<>]<<>{}>][{()<>}{(){}}]]}[<<<[]<>><<>[]>>>]}<{[(<<>[]>(<>{}))[<{}()>(<><>)]]([{{}[]}{{}[ +[<[[{{([<<<{{[(){}](<>[])]<<(){}>{{}[]}>}<[{()<>}{<>[]}][<{}>[[]]]>>{[[({}())]<<<><>>>][({{}[]} +[[(<[{[(({{[<<<><>><{}()>>{({}[])<<>{}>}}}}[[[<[()()]<{}()>><((){})(()[])>]]{[{{<><>}<()[]>}[({}<>)<[][]> +({{[[<([<[{<[([]<>){[][]}]<((){})<<>[]>>><<<{}<>>[()<>]><{<>{}}{<>[]}>>]](([[(()<>){(){}}]<[<><>][{}{ +{([([<{([{[[[{[][]}<()[]>]](<<<><>>>{<<>()>({})})]}[(<<[{}[]][[]()]>[<[]<>>([]<>)]>)<{[<()( +<(({[[<{{{[({(<>){{}[]}}[({}()){[][]}])[[{()()}<[][]>][<()<>>([]<>)]])}{[<[(<>{}){()}][(()()){[]<>}]>(([ +[<<[[<([([[[([[]<>]<<>{}}){<[]{}>[{}<>]}]((({}{})){{[]<>}([][])})](([((){}){{}{}}]){({{}[]}<{ +[[{{<{[<<[({<<()()>[[]<>]>[(()<>){()()}]}[<<{}<>>([]())>{<()[]>[{}<>]}])]><[{{{{[]<>}(<>())}[(()())([ +{<([{{(<<{([{<()()>{<>[]}}]{((()[])([]()))})}>{(<({<{}<>>}{[[][]](<>[])})><{<[<>]>[{()[]}{()[]}]}{{ +({(<{<{[{<{{([()[]]([]{}))[(()){[]{}}]}({{[][]}([][])})}<<<(<>[])[[]{}]>{[<>()](()())}><<[ +{[[<[<<[[{({{{[][]}<[]()>}}([{[][]}(()<>)][[<>()](()[])]))}([[({{}<>}(()[])){{[]()}(<>{})}][{ +<{[({{[{[[[<<({}[])<()>>(({}{})[[]()])>]<({(()<>)]<[()[]]>)[(((){})){((){}){<>[]}}]>]]}][{[[[(< +(<(<{(<{{[{{{[<><>][[]]}}{(((){})[<>{}])}}{{{<[]()><(){}>}(<[]>{()()})}[<([]<>)>]}]}[<((({(){}}< +{<(((([[{{[<{[()[]]{<>[]}}[<{}<>>{()()}]>](([<[]{}>[{}{}]]{{()[]}{(){}}})<{({}[])[{}<>]}({<>[]}({}()))> +[<<(<({[[<{([[<>{}]<{}<>>]{{<>{}}[[][]]})<(<(){}>{[]{}}){([]())[()()]}>}[<({<><>}[<>{}])[(<>())[<>{}]] +(<{<{[[<(((<[{<>()}<{}()>]><[[[]{}]]({(){}}{<>()})>){<<[[]()]{<>[]}>>[[<<>[]><{}[]>][{[]{}} +({[{(([[([[[[{()[]}]][{[()]}({[][]}([][]))]](<{<{}[]>((){})}{[()<>]<{}[]>}>[(([][]){[]<>})[(<>() +{[{((<{<{[<[{[<><>](()())}]>(<(<()()>)>{(([])<[]()>)<<()<>>[<>[]]>})]([<([<>[]](<><>))((<><>))>])} +<[<[[<<<{[[{((<>{})[<>()]){[<>[]]<{}<>>}}(<[(){}]{{}()}>{({}{}){{}{}}})]]([[[[()()](()[])][{(){}}(()() +([{<[{<{<{<<<(())[<>()]>[<<><>>]>(<[<><>]<{}<>>><{{}{}}[()[]>>)>[<[[<>()][[]{}]]<[()()][<>[]]>>(<({}[])<{ +{[[{<{<<<(<[<(()())[<>[]]><[()[]]>>[<[(){}]<<>[]>>{<{}{}>[()<>]}]>)>>><<<[(<[<{}()>[<>()]] +(<[[(({<<[((<(<>())>[[[]()]([])])<{[<>()]{<>[]}}[<<>[]>]>)(<<[<>{}]<[]()>>>[{{[]{}}<()<>>}{( +{{<<[<<<[[{{{<<>()>(<>())}[<<>()><[]<>>]}<(<<>()>{(){}}){<<>()>}>}[<{(<>[]){{}<>}}[{(){}}[(){}]]>{ +([{<(<[{[([(<{{}[]}((){})>[<<>()>[()[]]])]({<([][]){{}<>}>})){[{({[]{}}([]<>))<[[]<>]>}]}]}[<[{[<[<> +(<{{([<<<{([[<()>([]<>)]<[[]{}]>]){({<<>()><<>()>}[[[]<>><[]>])<{([])[[]<>]}<<<>{}>{[]()}>>}}><[(({{{}()} +<{<<[[([(<(({[<>{}]<<>[]>}[([][])[[]()]])[{<<>[]}{[]()}}{[()()]([][])}])>[(<({<>[]}<(){}>)<<()<>>[[][]] +((<([[[<<{{[<(()())>]}}[{<<{{}{}}{()[]}>{{<>{}}}>{([[]{}][()<>])({(){}}<<>[]>)]}{<<<<>()>><<(){}>{{}[]} +[<<<{<{[[{{<<[[]](()[])>{({}{})[{}()]}>[([{}]([]())){(()[])<{}<>>}]){[{{[]}[[]]}<{[]{}}<()()>>]} +[[(<((<{[<<[[[(){}]<()()>]{(<><>)[<>[]]}]>((<{()<>}[<>[]]>((()())<{}[]>)))>[([<<{}<>>({}[])>((()<>){[][ +[<(<(({[{<<<[({}[])[[]()]]>{<<<><>>[()()]><[[]()]{<><>})}>[[<([])(<>)>{[(){}](<>{})}][{[[]()][{}<>]}< +([<{[{((<[([({{}[]}[{}[]])<{[]{}}<<><>>>]{<(<>{})[<><>]>({[][]}<()<>>)})[<(<{}[]>({}{})){{[]()}}>( +[<[<({<({((<[{[]<>}({}{})](<{}()>{[]()})>{{[[][]][()()]}([[]()]([]{}))}){[<[[]()]>[<[]()>{()<>}]]})}<[[{{<[] +<{[[[[[{(<((<{[]{}}({}[]]>(<()[]>))){[({<>[]}{{}()})<[[][]]<[]<>>>][{{()[]}(()<>)}{(<>{})<[]<>>}]}>({[([ +[(([[<[{<[[<{{()<>}[<>()]}(({}()){()<>})>]]<[{{{()[]}{<><>})[{[][]}]}]>>[[[{[{()<>}[<>()]]({ +([[([([<{{((([<><>]){[<>{}](()[])}){<<<>()>[{}()]>})[[<[{}()]([]{})>]]}{[{{([]()){(){}}}(<{}{}>)}{[< +{{{{(<[(<(<[{([][])[<>()]}<{{}()}>][<(<><>)<[]<>>>{{<><>}[()()]>]>([({[]()}(()[]))[({}[])((){} +<[<<<<({<[[{(<{}()>[[]])}]{<([{}()]>{<{}<>>[[]<>]}><(([]()){[][]})[(<>()){<>[]}]>}]({{<<()<>>(<>())>[({} +[{(<<([[{(<{<[()[]][<>]>[{<>[]}<()[]>]}>[([{[]<>}{()<>}])({(<>())}{<{}[]>{[]()}})]){([[<[]<>>{{}{}}]([[]{}][[ +<[(<<<<<<{{([[<>()]{{}()}]<({}[])({}<>)>){{[[]<>]({}[])}}}({<(<>())(<>())><([]){[]<>}>})}{{(<({}{} +[[<[{{<<<([[<{<><>}<()<>>>{{()}({}{})}]{[<{}[]>{[]{}}][{<><>}(<><>)]}](({<<>()>})[<<[][]>[<>() +[[{<{((<((<([<[]<>>(<>())])(<[{}[]][()<>}>(((){}){[]()}))>[{(<<>[]>{<><>}){<(){}>}}[<(()[]){()}>]]){(({{{}[]} +(({{{[{([<[<[{()()}][(()[])(<>())]>{{<[]<>>{[]()}}{[<>[]]<{}()>}}]<([{()()}<(){}>]{[<>[]}<()<>>})>>([<<{{}[] +{<<({<[({{{(<({}{})<[]()>><({}[]){()[]}>)(((()<>)<{}{}>)({<>()}({}[])>)}{({<{}{}>{(){}}}(<[]{}>{< +<<[<{<<[<{([([{}[]][(){}]){([]())[{}()]}])}>{{([{[()()]<[][]>}[({}[])((){})]]{({[]{}}[[]<>]){(()())({}[] +[[({<{[(({<({<<>{}><[]{}>}[({}()){<>[]}])>})[[<([<[]>[{}<>]][([]{}){[]}])>(<[[<>()}{<>{}}][{()[] +{[<[[[{[<[{{<{{}<>}{<>[]}>{([]<>){{}{}}]}}<{((<>{})[()()]){{<>()}<{}[]>}}<<<<>[]>>{[()[]][{}{}]}>>]{[({{<>()} +[([[<<[([[<[{[<><>][()<>]}[[[]<>]<{}>]](([[]<>]<<>{}>))>(<{<[][]>{{}()}}{[[]()]{<>[]}}>[[<()<>>({}<>)]] +[{<(({<[<[[[[{<>[]}]]([{()<>}<()[]>]([[]<>]<<><>>))][{<{[]}{[][]}>[[<>{}]<[][]]]}<[(<><>)[< +<[[<{<<<(([([[{}{}](()<>)][([]<>)[<>()]])][<<({}())[[][]]>>])<<<[({}<>){()<>}]>[[[(){}][[]]]<<()<>>}]>[<(<<>{ +(({{((<{[{({(([]{})<()>>{{{}()}([]<>)}}){<((<>())(())){<()()>([]())}>}}[{(<<<>{}>>[<(){}>{()<>}])} +<[<[(([{<[{(<<<>()>[{}<>]><<<>()>[{}{}]>)<({[]{}}[{}[]])[[()<>]({}<>)]>}<[<{[][]}<[][]>>[<<><>>]][{({}[]) +{[{<[([[[[({{[{}{}]{[]()}}<([][])[[][]]>}{({<>()}(<>[]))[({}<>)[<>{}]]})]]]{{{{[[[{}[]](()<>)][ diff --git a/2021/day10/src/main.rs b/2021/day10/src/main.rs new file mode 100644 index 0000000..0a59f7b --- /dev/null +++ b/2021/day10/src/main.rs @@ -0,0 +1,92 @@ +use std::collections::HashMap; +use std::collections::HashSet; +use std::io::stdin; +use std::io::BufRead; + +fn main() { + let arg = std::env::args().nth(1).unwrap_or("part2".to_string()); + match arg.as_str() { + "part1" => part1(), + _ => part2(), + }; +} + +fn part1() { + println!("Part 1"); + + let opening_brackets = HashSet::from(['(', '[', '{', '<']); + let closing_brackets = HashSet::from([')', ']', '}', '>']); + let bracket_map = HashMap::from([('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]); + let score_map = HashMap::from([(')', 3), (']', 57), ('}', 1197), ('>', 25137)]); + + let mut score = 0; + + for line in stdin().lock().lines() { + let line = line.unwrap(); + println!("{}", line); + let mut bracket_stack = Vec::new(); + for bracket in line.chars() { + if opening_brackets.contains(&bracket) { + bracket_stack.push(bracket); + } + if closing_brackets.contains(&bracket) { + let opening = bracket_stack.pop().unwrap(); + let expected = bracket_map.get(&opening).unwrap(); + if bracket != *expected { + println!("Error! Expected {}, but found {}", expected, bracket); + score += score_map.get(&bracket).unwrap(); + } + } + } + } + dbg!(score); +} + +fn part2() { + println!("Part 2"); + + let opening_brackets = HashSet::from(['(', '[', '{', '<']); + let closing_brackets = HashSet::from([')', ']', '}', '>']); + let bracket_map = HashMap::from([('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]); + let score_map = HashMap::from([(')', 1), (']', 2), ('}', 3), ('>', 4)]); + + let mut scores = Vec::new(); + + 'outer: for line in stdin().lock().lines() { + let line = line.unwrap(); + print!("{}", line); + let mut bracket_stack = Vec::new(); + for bracket in line.chars() { + if opening_brackets.contains(&bracket) { + bracket_stack.push(bracket); + } + if closing_brackets.contains(&bracket) { + let opening = bracket_stack.pop().unwrap(); + let expected = bracket_map.get(&opening).unwrap(); + if bracket != *expected { + println!(" Error!"); + continue 'outer; + } + } + } + bracket_stack = bracket_stack + .iter() + .rev() + .map(|bracket| *bracket_map.get(bracket).unwrap()) + .collect(); + print!(" Incomplete: add {:?}", bracket_stack); + let mut score: i64 = 0; + + for bracket in bracket_stack { + score *= 5; + score += score_map.get(&bracket).unwrap(); + } + println!(" Score {}", score); + scores.push(score); + } + + scores.sort(); + let median_idx = scores.len() / 2; + let final_score = scores.get(median_idx).unwrap(); + dbg!(final_score); +}