diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-07 12:15:26 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-07 12:15:26 +0100 |
commit | 0cadb2319cf7b937918df7071a810d28d769a465 (patch) | |
tree | 66f8c49f584954eab951d4a9e2b5d9e0424da9d4 /2021/day10 | |
parent | 6f96b2014b053a2e6e5a37464ca6fc578607fda7 (diff) |
day09 & day10
Diffstat (limited to '2021/day10')
-rwxr-xr-x | 2021/day10/day10 | bin | 0 -> 97680 bytes | |||
-rw-r--r-- | 2021/day10/day10.cpp | 88 | ||||
-rw-r--r-- | 2021/day10/day10_input.txt | 94 | ||||
-rw-r--r-- | 2021/day10/day10_input_ez.txt | 10 | ||||
-rw-r--r-- | 2021/day10/makefile | 2 |
5 files changed, 194 insertions, 0 deletions
diff --git a/2021/day10/day10 b/2021/day10/day10 Binary files differnew file mode 100755 index 0000000..e3b93c6 --- /dev/null +++ b/2021/day10/day10 diff --git a/2021/day10/day10.cpp b/2021/day10/day10.cpp new file mode 100644 index 0000000..4fad4ca --- /dev/null +++ b/2021/day10/day10.cpp @@ -0,0 +1,88 @@ +#include <iostream> +#include <fstream> +#include <vector> +#include <stack> +#include <string> +#include <algorithm> + +#include "../utils.h" + +char map (char a) { + if (a == '(') return ')'; + if (a == '[') return ']'; + if (a == '{') return '}'; + if (a == '<') return '>'; + return 'A'; +} + +bool match (char a, char b) { + if (a == '(' && b == ')') return true; + if (a == '[' && b == ']') return true; + if (a == '{' && b == '}') return true; + if (a == '<' && b == '>') return true; + return false; +} + +int score (char a) { + if (a == ')') return 3; + if (a == ']') return 57; + if (a == '}') return 1197; + if (a == '>') return 25137; + return 0; +} + +long score (std::string str) { + long s = 0; + for (char c : str) { + s *= 5; + s += (c == ')' ? 1 : 0); + s += (c == ']' ? 2 : 0); + s += (c == '}' ? 3 : 0); + s += (c == '>' ? 4 : 0); + } + return s; +} + +bool check (std::string line, std::string &out) { + std::stack<char> stack; + for (char c : line) { + if (c == '(' || c == '[' || c == '{' || c == '<') { + stack.push(c); + } else { + if (!match(stack.top(), c)) { + out.push_back(c); + return true; + } else stack.pop(); + } + } + while (!stack.empty()) { + out.push_back(map(stack.top())); + stack.pop(); + } + return false; +} + +int main (int argc, char *argv[]) { + std::string raw; + std::getline(std::ifstream(argv[1]), raw, '\0'); + std::vector<std::string> lines; + split(lines, raw, "\n"); + + std::vector<long> repairs; + int sum = 0; + for (auto line : lines) { + if (line.size() == 0) continue; + std::string rep; + bool corrupt = check(line, rep); + if (corrupt) sum += score(rep[0]); + else { + repairs.push_back(score(rep)); + std::cout << score(rep) << " " << rep << std::endl; + } + } + std::cout << "corrupt score: " << sum << std::endl; + std::sort(std::begin(repairs), std::end(repairs)); + std::cout << "repairs score: " << repairs[(repairs.size()-1)/2] << std::endl; + + return 0; +} diff --git a/2021/day10/day10_input.txt b/2021/day10/day10_input.txt new file mode 100644 index 0000000..eec53e5 --- /dev/null +++ b/2021/day10/day10_input.txt @@ -0,0 +1,94 @@ +({<{({([<<{<[<<><>><<>>](<()()>)><{{{}<>}}<<<>{}>[<>[]]>>}{[<<[]{}>[<>()]>[(()[])]]<{{<><>}[[]<>]}{< +[(<[[([[(([<{{[]{}}([][])}<<{}[]>([]{})>><{<()()><[][]>}{[()]<()[]>}>]<{({()()}){[{}[]][<>()]}}[{[{}<>] +{{(<[[(<<<(<<[{}{}]{<>()}><{[]{}}([]{})>>([([][]){{}<>}][<()<>>[(){}]]))<{<(<>())[{}<>]>{{[]{}}<<>{}>}}({< +<([({[<[(([[<(()<>){[]<>}><<(){}>{()()}>]](<(({}[]){[][]})<{<>())[<>[]]>>{{{()<>}{(){}}}{[<>()]<{}[]>} +<(((<{<(<{{[[[()<>]{[]()}]][{<()[]><<>{}>}]}}[<{[{[][]}[<><>]]}{{{(){}}[<>()]}{[[]()]{<>[]}}}>{[({{} +{{({[({(({{[<[{}()]([]<>)><<{}()>[<>[]]>]<<{[]{}}([]<>)>{{<>}{<>{}}}}}{{<<[]<>>{<>()}>}<({[]<>} +(([{[(<(<[[(([()[]][{}()])(([][])<{}<>>))]]>)[<(<{{<<><>>(<>[])}<([]<>)[[]{}]>}((<{}[]>[<><>]){{[ +{{[<<{<<(<([<<[]()>[[][]]>{[()[]]<()[]>}]){(<(<>{})([])>{<(){}>{<>()]}){[<[]()><<>()>](([][ +((<{[{{(<<[(<{<>{}}[<>[]]>{<{}<>>{{}[]}})]>><{([<(<>())[{}()]>{<<>{}>{[]{}}}><([[]()]<[]<>>)(<[]>{()()})>)}>) +{<({{{(<[([<{(<>[])<()[]>}([[]]([]{}))>])[{{[([]){[]{}}]}}{[[{(){}}{<>{}}][[(){}]]]<([<>{}]{{}<>})( +([([{{{[[<<[[(()[])<<><>>]{({}[])}]((([]{})<<>>)({[]<>}[<>[]]))>{(({<>()})([{}[]]<()<>>)){[ +{[<(({[<<{{{[({}[]){<>()}]{[()()]{[]()}}}[{<<><>>{<>[]}}<{()()}<()>>]}({<(<><>)[()[]]>([{}()])}{[(<>())(() +<{{[[<{{(<<{<[{}{}]<{}[]>>[{{}[]}[{}()]]}((<()()>{(){}})<((){})>)>[{<({})[[]{}]>([{}<>])}{{[{}()]}{<[ +[{[<{<[[({([(<{}<>>[<>()])]<<<{}{}>({}<>)>>)<{(({}[])({}<>))}<{<[]>({}<>)}{{()()}([]())}>>}){<[<[[{}<>]< +{(<{{<{<<(<<<[{}{}]((){})>>{<<{}{}>{()[]}>}>)([({(<>())<<>()>>)(<<()[]>(<>())><<()()>({}())>)]{(<[<>< +[{<{[<{([{[({<(){}>([]())}[<<>[]>([]())]){{<(){}>{{}[]}}<[()[]]({})]}](((<{}()>{{}()})))}[[( +<<[{{[<<(<{({{[]{}}{()()}}){<<{}()>({}<>)>(({}<>)[()<>])}}<[[([]{})[<><>]]]((<()()>{<>[]}){{[]{ +[[({[({[([({{<[]{}>{(){}}}{[[][]][()()]}}[{<{}><{}()>}({()[]})]){{[[()<>]<[][]>][({}<>)]}}](<({<{}( +[[[((([((<{{[(<>())[{}[]]]{{<>{}}}}}>))<[[[({{{}{}}(()())})({([]{})[<><>]}[([]{}){()[]}])]<{<[{ +[{[<[{{([[{(({()()}<{}[]>){{()<>}{{}()}})<<<{}[]>>{<<>[]><{}{}>}>}]{{{[{()()}[<>()}]{{{}}[(){}]}}} +[[{[(({[[[[{(<<>()>(<>()))[<{}[]>{[]<>}]}{{{()<>}<()()>}}]<[<([]<>}{<>[]}>]>](((<{()()}<()()>><<<>[]><<> +({[{({(<{((<<[()[]]<()<>>]<([][])>>[([{}()]<{}{}>){<{}{}>(()[])}])[{([<>()]{()[]}){[()[]]}}([{()<>} +[(({(([[{(<{{<[][]>(()()}}[({}[])]}[(<<>[]><()[]>)]>)<{({{[]{}}[()[]]}[<<>()>(()())])}{(((()[])<[]>) +<(<([[({[((((<{}[]>[{}{}]){[{}<>](<><>)})(([()<>]({})){[{}{}]{[]<>}}))[({(()[])<<>[]>}[[()<>]<() +{[(<<({([{[(<<[][]>(<>())>){<[<>()][[]{}]>(({}[])[(){}])}][(({{}<>}))(<[()()]<()()>>}]}{[{<(<> +[{{({<({<(<[[<{}<>>{()[]}][{()()}]]>){[<{{()[]}[{}()]}>([<()[]><{}<>>](<()[]>))]{[<[<><>]{<>[]}>[<() +{[<<[{<<[[(<(<[][]>[()()])>){<(([]()){{}[]})><<[{}()]<<>>>>}}[[([<{}[]><(){}>][({}())[()<>]])[[{<>[] +{((<[{{<{[<[((()<>)<()()>)]>]}>}((([{[[<<>[]>[[][]]]<[[]()]({}[])]]{[([]{}){()()}]}}<((<[]< +{[[({<{<<{<{{{[]{}}}}>}[[{(([][]){<>[]}){{()<>}}}<<<()[]>[{}[]]>>]<[{[()[]]({}{})}{[{}()]}]>]>>}>}< +[{[(<({{(<(([{()<>}{()<>}]{{[]}(()())]))>(<<{[<><>]<()<>>}[(<>{})<<>{}>]>[{{<>{}}{{}[]}}<[< +{{((<[(([<{{([()][(){}])({<>()}[()<>])}[{[{}<>]<()()>}<{{}}([]<>)>>}(<<[[]{}]<[]<>>>[([]()){[]{}}]>((<<>( +(<<<(<([<<({<<<><>>[{}<>]>{<()>([]<>)}}{{(<><>)}<{<>()}{{}()}>})<<<({}{})<[]<>>>>>]{({({[][]}[{}()])([<>()]( +([(<<<(<(([([{<>{}}<(){}>](<{}<>>[{}{}]})][([<()>{{}[]}](<<>()><()[]>))[[{<>()}[<>[]]]{<<>[]> +(<{[[<<<({[<<([]())[()()]>([()[]]{<>{}})>]})[[{<[{<>()}({}{})><(()<>)([]{})>><<<<>()><<>() +[[[{(<[[((<[{<<>[]>({}[])}<[{}{}]{[]<>}>]<{({}{})}{[{}<>]{<><>}}>>([{[[]<>](()<>)}{{<>{}}(<>[]))]{[{{}{}}]}) +[(<([[[{<[[<((()()){[][]})>((<{}[]>[()<>])<([]()){<>[]}>)]<<{<<><>>}{{(){}}}>>]<{[<<(){}>({}<>)><<<>()>([]<> +[<((<<[<{[{[{[()[]]{()<>}}(<<>()>[<>()])]}{<[[(){}]{(){}}]{[()]{<>[]}}>{{<[][]>[()()]}[(<>{ +[({{([([{[[([<<>{}>[[]()]]({{}()}))][<{{()[]}}<<{}{}>>><[[{}[]][<>()]][{{}[]}(<>{}))>]][{((((){ +((<<{(([[[{{<[()[]]({}())>}<[{(){}}]<({}{})<()[]>>>}{((<()<>>([]<>)){[()][()[]]})([{()}<[]{}>]<(<><> +<<<<[([<<(({<[[]()]<<><>>>({[][]})}{([<>{}]<<>()>){{[]{}}}})){[[[[[]{}][(){})]{(()[])(()<>)}]{{[[]<>][{}( +[({{([<{({{[{(()[])<<><>>}(((){})[<>()])>}{{{{{}<>}<{}[]>}{[(){}](()[])}}}}([(([<>{}](<>[])) +([(<({{(<<[([[<>][<>()]])<[{()[]}([]{})]{[()<>][[]{}]}>]><<{[{<><>}<()()>]{{<>[]}>}>>><[<{{ +({{(([({{<[<[{{}{}}{{}<>}]((<>{})[[]<>])>(((<><>)<{}()>){{{}{}}{{}{}}})]({<{{}}([][])><<[]{}> +[(<{({{(<<<(<[{}[]]{(){}}>(([]()){<>[])))[{{[]{}}{()()}}{[<>{}][<>]}]>[((<{}<>>[<>[]])[({}[])<[]{}>])[<[{ +[(<<<[<<({{{[[(){}]]{<<>{}>{()[]}}}{({<>{}}{[][]})}}{<([()[]]<{}<>>)([{}()]({}[]))>[(<<>()>{[]()})]}}{([{{( +{[{{(({[{<{{[[()<>]<[]<>>]({{}{}}[{}{}])}{(<<>[]>{[]})[[[]{}]{()<>}>}}{<<{{}{}}(()[])>>{{[[]()] +[<[[{(([<<[<[([][])<[]{}}]{<(){}>}>(<<()<>><[][]>><<{}[]>[{}()]>)]><([([[]<>](()[]))[(()<>)<(){}>]][({ +<{{<([<([{{{(<[]{}>[[]{}])[{<>{}}([])]}}[<[<<><>>({}<>)]{((){})[[]<>]}><[{()[]}<<>{}>]{{()() +{[<<<([{([{<<(()[]){{}{}}>[(<>{}){{}()}]><[[{}<>][[]()]](<{}()>([]()))>}]){{<{<[()<>][<>()]>< +<{[<<[([[{[({[[]<>](<>[])})({([]<>)<[]()>}<([]<>)[<>[]]>)]}[[{[{()<>}{<>{}}][<[]{}>(<>[])]}<({[]<> +[[[[([({(((<<[()<>]>[(()())]>{[{{}()}[[]()]]{[<>()]([][])}})))}(<({(({()[]}[<>()]))<[[()<>]]<{{}()}[()<>] +{[([([<[(<<[<[[]()]<[]<>>>][{<[][]>[<>()]}{({}<>)<()[]>}]><{({<>()}{<><>})<{(){}}({}{})>}<{( +<<({[<<{(<[<({{}[]}{<>[]})[<()()>]>(([<>()][<><>])(<<>{}><()[]>))](<[<{}<>>[{}[]]]({{}{}}( +[<<{<(([<[{(([()<>]({}{})){<<>[]>[()()]})({([])(<>())}[{[][]}[{}[]]])}{({(()())[()[]]}({[]<>}([][]))) +((({<[{([(<[(<[]()>)](<[{}()]<[]{}>>(({}())<()[]>))>)[(<[([]<>)(<>{})]>)]][({({[()[]]{<>}}[[[]{}]{()[]}])([<< +[[[(({[<[((({([]){<>{}}})[[{()[]}[{}[]]][{[]<>}]])<<(<[]{}>[<>[]])[<[][]>[{}[]]]>>)[([{{<><>}[[][]] +({<(({{[{{<[((<>())(<>[]))<<<><>>((){})>]([<[][]><{}{}>][[{}()]<<>()>])>((<[[]()]([][])>[<{}[]>{ +(({<<([[<[[{<[()()]<[][]}>[{(){}}[{}<>]]}(<{{}<>}{[]<>}>{<(){}>{<><>}})]<<{<<>[]>[<>[]]}{{ +<[{([<[({<((<[{}()]([]<>))[(()[])])((({}{})<()<>>){(()<>)}))>{[<<[<>{}][(){}]>{<<><>>(<>[])}>{[ +{[([[[{{<<[<<<()()>(()<>)>[({}{})[<>{}]]>([[[]<>]]{<<>>[[][]]})]><<[{(()[])][<()>[{}[]]]]<([{}{}][()<>])( +{(({{<<((<{<<<[]{}>{{}()}><<<>()><()[]>>><<[<>[]]](({}[])[[]{}])>}[[({<>()}{()}){[[]{}]({}())} +<[[{<[({<([<<<()<>>>>([[[]()]{<><>}](<<>[]>[<>]))])(({({[]{}}){{{}()}{(){}}}}[{(()<>)[[]()]}[{<>()}{[][]}]])) +{<[[([{<<<{{[<[]{}>[[]<>]]{({}())<[][]>}}<<<{}{}>>({(){}})>}([{({}())}<([]())[()()]>])>({{[[<>()]{{}{ +<{(<[{[[{[<<<{(){}}[<>{}]>{{{}[]}<<>()>}>{[{[]()}<<>>]{<[]{}>[()()]}}>][[[{<<>())[[]<>]}<(<> +[{{<<{{([(<<{{{}}[()]}<(()())(<>())>>(<{[]()}[[]<>]>)>[((<()()><[]{}>}({[]<>}))(({[][]}{[]<>})<(()[])(()[] +([(<<[<{({[[[(()<>)<{}()>]{[{}()]{<>{}}}]{(<(){}>{{}[]}){<<><>><{}[]>}}]}<{[[[()()]{{}{}}]] +{<(((<({<((([<(){}>]){{{<><>}{{}()}}})({<[[]{}]>{({}())}}))>})([[<<<{[{}[]]{[]{}}}<{()<>}({}[])>>>{[ +{([[{[[[<<<({{()<>}}(<[]<>>[<><>]))[[[[]<>][{}()]]]>{<<[<>()][[][]]](<{}{}>{[]{}})>[<(<>{}){<><>}>([{}[]] +[[{(<{{{<(<{[[{}()](()[])]}<<{(){}}[()[]]>{[()()]([]())}>>)>[(<<<{[][]}>>[{([][])[(){}]}[(< +<{[(<{<{(<<{<{()[]}(()[])>(([]<>)([]()))}>{((({}[]))<{[]()}{{}()>>)<{<(){}>}<{<>()}<()()>> +<(<[{{[[{<{[{[<>][<>[]]}]{[(<>[])][<{}{}>{<>}]}}(({[()<>]{[]<>}}<{(){}}[[]{}]>)([[<><>]<()()>]<({}[])([]()) +{(<{[{[{({{(<<<>{}>><({}())(<>())>)}}{(<<<(){}>{(){}}>(<{}()>(<>()))><[{<>()}{{}()}][{[]<>}(()())]>){<[{{}[] +{[((((({<(([(<[]<>><[]<>>)<<{}()>>](<{()[]}(()<>)>{{{}{}}([]{})})))[<[{([])[<>]}(([]())<{}{}>]]{ +(<{[((([{(<<{(()<>){{}[]}}>><<<({}<>)[[]()]>{(<>[])[()()]}>{{{<><>}([]())}}>)[{(([[]()][<>( +<[<<<[(<[[(<<([]{})>([[][]][{}{}])><[<<>[]>[{}[]]]<[<>][()()]>>)][[<<{()[]}(<><>)><<()()>[()<>]>>][{[{[]() +<<{([[<[[<[({[<>[]](()<>)}{({}[]){()[]}}){({{}[]}(<>()))}]<[[<[][]>[[]<>]]<[{}<>]<[]{}>>)>>]< +[((([[{<<[[<{{{}[]}[()<>]}>(<<{}{}>[(){}]>({<>[]}<{}<>>))][<{[{}<>]{[]}}>({[{}{}}[[][]]})]](({[{[][]}]{[{} +[([{<[[{<((<<{[]<>}(()<>)>>{(<<>{}>(()<>))}))[(<[[[][]][<>{}]]{[()[]](()())}><({{}{}}}[({}())]> +{{[({<(({{[({{[]()}<()<>>}{([]()){[]{}}}){[<<>>[<><>]][<{}<>><()[]>]}][{<({}{}><[][]>>{{{}()}<(){}>}}]}}))>}< +{[{[<{{({[({[[{}()][(){}]]{{[][]}<<>()>}}[[(<>[])<[]{}>]((<>[])[{}()])])]})}}>{({{<([{<([]())[{}{}]>{ +<{{[{<{<<{{({<{}[]>[(){}]}[[<>()]{(){}}])}}[{{[(()<>)(<><>)]<{()<>}{<>()}>}{({[]()}{{}()})<{{ +((((([{{<<[<({()<>}<<>{}))<{{}<>}(<>[])>>{<{(){}}[{}[]]>}]{<<[{}{}]>{{()()}[{}<>]}>}>>([[({(()())<() +(({{[[<[[<<<<<[]()>[{}<>]]>>({[<<><>><{}()>]{{(){}}[<>{}]}}[[([]<>)({}<>)]])>{<[[<{}[]><{}<>>][<<>[]>]](<<[] +<[<([<<[([[[[<{}[]><()>][(<>{}){[]}]]{({()[]}[<>{}])}]([((<>[])(<>[]))[{<>[]}{[]<>}]][(([]{})({}<> +<((<{{({{([<([<>{}](()()))<<(){}>[<>[]>>>({<<>>([]{})})]<[[([]())<<>>]{{<>{}}(()<>)}]>)<[(((<>)<{}<>>)({<>( +{({<({<<[[{[((<>())<()<>>)<(<>[])((){})>]({{[]()}[{}()]}[[{}{}]<{}[])])}({<[(){}](<><>)><{()<>}[{}[] +<[(<<[((([(([[[]<>][<><>]]{{{}{}}[{}[]]}){(({}())<[]{}>)})<(<(()[]){()()}>{<()>[[]{}]}){{[[]{}]([]< +{(<(([{[[<[[[{<>{}}{[]{}}]({(){}})]<[[()()]][[{}[]]]>]{<<{{}{}}[{}{}]>>[(<{}()>([]{}))(<(){}>{<><>} +((<<({({<{<<[<[]{}><()[]>]<(<>()){()[]}>>>}{(((({}<>)({}()))<{[]{}}<<>[]>>)<({[][]}<(){}>){ +[[<{((({{{(<{[[]{}}{{}}}<{[]{}}<()>>>{<{{}[]}<[]()>>{[()[]]{[][]}}})([{[[][]]}(([]{})<<><>>)][(<[][]>{{} +(<({[{[(<<(((([]())[<><>]}([<><>]))<[[()<>]<<><>>]{<[]()>{()<>}}>)[<<{[]()}(())>><[<(){}><<>{} +(<<[<(<{<({<[{(){}}{<><>}][<<>()>]>}<[<[()[]]<{}[]>>]<<({}[]]><[[][]]<[]()>>>>)>}><[((<[<<[]<>>>[[<>()][{}( +[((<[{[[([{[[<<>>{<>()}]<[[]{}]<[]<>>>]}<({(()<>))<<<>()><<>[]>>)<{<[]{}>((){})}[({}[]){()()}]> +<<{{({{([{{<<{{}{}}(<>{})><{()()}[{}<>]>><{<[]<>>[[]()]}[<<><>>({})]>}(((<<>{}><{}{}>)([<>{}][{}]))[{ diff --git a/2021/day10/day10_input_ez.txt b/2021/day10/day10_input_ez.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/2021/day10/day10_input_ez.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/2021/day10/makefile b/2021/day10/makefile new file mode 100644 index 0000000..038dc5d --- /dev/null +++ b/2021/day10/makefile @@ -0,0 +1,2 @@ +all day10.cpp: + g++ -std=c++20 -o day10 day10.cpp |