From 636e959b30cdf95ecf4c09529821a26c890e8ecb Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Fri, 10 Dec 2021 08:21:59 +0100 Subject: [PATCH] day 10 --- 10.txt | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10a.c | 46 +++++++++++++++++++++++++++++ 10b.c | 55 ++++++++++++++++++++++++++++++++++ 10e.txt | 10 +++++++ Makefile | 2 +- 5 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 10.txt create mode 100644 10a.c create mode 100644 10b.c create mode 100644 10e.txt diff --git a/10.txt b/10.txt new file mode 100644 index 0000000..47dcbd3 --- /dev/null +++ b/10.txt @@ -0,0 +1,90 @@ +[{(<<{(([<<{<<[]()>[<>[]]>[[<>[]]{{}{}}]}<<[<>()][{}]><[<>{}]<<>()>>>>([<([]<>)([][])>(<<>[]>([]()))]<[ +{([[[{{([{{{([[]()][()<>])}[<<(){}><<>()>>{{[]<>}[(){}]}]}<<(<<>[]>{[][]}){<<>()>({}()}}><{ +{[[([{([<(<{{[{}{}]<[]()>}(<<>[]><<>()>)}(<[<><>](<>[])><<(){}>{(){}}>)>{{[({}[]){[][]}]({()<>}<[]<>>)}})>({( +{[<([(<<{<<<[<{}{}>({}[])][<[]{}>{[]{}}]>{[{[][]}[{}<>]]{(<><>){{}[]}}}>([<[<>{}]([][])>{(<>{})}][{({ +<{{[{[<(<[{[(<()<>>([][]))[[{}()]]]}<{<{{}{}}((){})>((()())([]{}))}<((<>{}))>>]<{[[<()[]>]{(<>[]) +<((({<((<([<[<[][]>[[]{}]]<{[][]}({})>>{<<()>({}())>[(<>)<[]()>]}]{[{<<>[]>{{}<>}}<<()<>>([][])>]})[({{(( +[{<[{<({({[(<{{}[]}[[]()]>{<()()><{}()>})({[{}<>](()())}({[]{}}({}{}))>]([[[<>[]]<()[]>]]([[<><>]({} +{(<[<<{([([(<[{}[]]<{}{}>><<()[]>([]<>}>)]{[[{()}({}())]<[<>[]]([]<>)>](<[[]{}](<><>)><[[]{}]{<>[]}>)})] +({([{[({{<([[([]())]{[[]{}]}]{<{(){}}<{}{}>><([]{})>})[(<{<>()}>{(<><>)[[]{}]}){{{{}<>}({}<>)}[<() +<[<[{((<<{[((<[]<>>[[]()]){{()[]](<>)})(({[][]}[[][]])[(<><>)<{}>])][<[{[]()}{[]()}][{<>{}}({}<>) +<({(<(<{[[({({()[]}<{}<>>)[<{}[]>{{}()}]}([[{}](()[])][{[]{}}(()[])])]{[[{()[]}]<[[][]][{}]>]}]<(<<<[]()>[ +[<<<{((<{(<([[{}{}]{()<>}]{{{}<>}[<>()]}){[[[]()][[][]]]{([]())[()<>]}}>{({[<>()]}([[][]]<(){}>))<<<{}< +[[{(([(<{(<({[()()]([]{})}[({}<>)])([<[]{}><()<>>][<<>[]><<><>>])>)[[{[{()[]}{[]}]}]]}>)]{<{{(<[{[[]<>]<()[]> +<{(<[{{([{{[(<[]<>><()<>>){{{}{}}}]}(([[<><>]([][])]<{()}{{}()}>)<{[()<>][<>]}<(<>[])>>)}]) +(<{({([<([<{<(<>()){()()}>([<>()]({}()))}><(([{}{}]{<>{}})<<[]()><[]{}>})<{([]<>)<[]()>}({{ +[{([(({[(<[[{{{}<>}({}())}{{(){}}<<>>}]<<({}())(<><>)]<<<><>><()()>>>]({(<{}<>>{()[]})}<<{{}{}}({})><<<>( +(<[(({<(<{({{<()<>>{{}()}}}[[{()[]}{[]{}}]<({}())([]{})>])}{<{<(()[])([]<>)><<<>{}><{}[]>>}<[[<><>](()[]}]>>{ +({({<({({(<{({{}{}}(()[]))(<{}{}>[{}()])}((<{}<>><()[]>)[(()<>)[{}<>]])>)}[[<<{<[]{}>{<>[]}}{{(){ +({<<([(<<<(<{(()())[{}[]]}(<[]<>>[{}{}])>)<(<[{}[]]{[]{}}>({[][]}[[][]])>>><[{([[]()]{{}() +[({{[[(({[<[([{}[]])<<(){}>(()<>)>]({<[][]>[{}<>]}[<[]<>>])>({(<<>[]>{{}{}}){<[]{}><{}<>>}}([({}< +((<[[{({<<<([({}{})[[]<>]]{(()<>)[[]{}]})({{<>}[<>[]]}[[<>[]]<[][]>])>>>}[[<<{(<<>()>(()()))<{<>{}}(() +{{{[[[([{<(<[([]())[[]()]]<(()())([]{})>>([<[][]>]{({}()){()<>}}))><[([[<>[]][[]<>]][<()[]><{}{ +[<{[(<<{({<({{()[]}[{}<>]}{({}<>)[(){}]})[<([]{})><([]<>){()()}>]>})}>[[<{[<<[<>()]<()<>>>{[{}{}]<<>[]> +<((((((<{(<([[<><>][<>[]]]{<{}<>>})>)(([<[()()]>[(<>[])<[]{}>]][<([]{})([]{})>{{[]}({}())}]){[[[()[ +<([{((({{[({[[()]][{[]}{<>[]}]}<{<(){}>[[][]]}<([]())<[][]>>>)<<{<()()>({}())}{<()()>}>{{[{}()]}}>]} +<[({<[(<<<<{[[<>{}]([]{})]}>>>[<[[{(<>())([][]}}(<[]<>>)]<<{<>()}{[]<>}>{<()>([]())}>]<<[({}{})<[ +[[([({<({{(<(<()()>(()<>))>[{<<>()>([]<>)}{{<><>}{{}[]>}]){([[[]()]<[]<>>](([]())<[][]>))}}}{<{<{<<>() +<<{[[<(({(({<{[][]}(<>{})>{(<>())}}{<[[]<>]<{}()>){{[]<>}[()]}}))}))>]]{{<<{{<<<{([][])<{}()>}[[<>()][()[] +{<{[[(<[((([<{[][]}[{}]>]((([][])([]()))([<><>](<>{}))))){(<[[(){}]<[][]>]>{((()()}({}[]))<[<>{}]{<><>}> +([[[[[([<{({{<[]<>><{}<>>}{{<>()}[[]<>]}}(<[<><>]{()()}>))<[{({}<>){(){}}}<({}())[()[]]>]{([()<>][[][] +({([(({<<{{[(({}[])([][]))[({}{})]]}([<<[]<>>([]{})>])]>>[{[[(<<()()><<>{}>>)(([[][]](<>[])) +{({[<({[({[([[(){}]{(){}}])]<{<<(){}>[{}[]]><{[]{}}[[]<>]>}{{{<>{}}[[][]]}[[[]<>]]}>}([{((<>[])<[][]>)}] +({([<(((({<[<<[]<>>{{}[]}>((<>{})<[]{}>)]>{(<{{}[]}({}{})><<()<>>(<>[])>)[{(())([]())}]}})))[{<(({{{[]{}}[< +<<([{{[[<<(<[{[][]}<{}{}>]<[{}{}]<<>{}>>><[[{}{}][{}]]<<{}<>>[{}()]>>)(<[(<>{})<()[]>]{(()())}><[[[]]<{}>][(< +{[[([<({[{[<[([]<>)<[]{}>][(<>())[[]<>]]>[([<>[]](<>))([()()]({}()))]]{{(<()[]>{{}[]>)[{<>}(()())]}}}{<{{ +([[[(([{{<[{<[<>[]]<<>[]>><(()())<{}[]>>}(([[][]]<{}<>>))]<[[(<><>}{()()}][[{}[]][()[]]]](([{}{}]<[][]>)<[{} +{({{(<([[{({{<[]{}>[[][]]}{{<>()}<[]{}>}})({({[]})[<()()>{[]<>}]})}<{[[<{}>({}())]{{[][]}<[]()>>]([{(){ +{([((<<{[{{[<({}())[()[]]>(<{}<>>({}()))]}}<{<([[][]]([]{})){(<>[]){()[]}}><[({})[<>[]]][( +{(((<[([({(([{<>}<[]{}>]))}({{([<>{}](<>{})){(<>{}){[]{}}}}[[[<>[]]]<{()<>}{<><>})]}({({[][]}<[]{}>)[{[]{}}]} +[{(({<[{(([{<[()[]]<[][]>>([{}<>][()()])}([<<>[]>[[]{}]][<{}[]>({}())])]<({({}[])}[{()()}[{}[] +[[{{<<<[[{<<[<[]{}><()[]>]{[[][]]{<><>}}>>{({[[]{}][<><>]}(([]<>)<<>()>))(([{}<>]<[][]>)<([]<>)(( +[{{(({[(({[[<[(){}]>(<{}<>>)]<(([]<>)<<>()>)[<[]{}>{<>()}]>]({<[[]{}]{<><>>>}<([<>()][[]<>] +[{<<{{(((<[[{(<>{}){[]<>}}((()[])[()<>])]<[[()]](<<>[]>[(){}])>](<({{}[]}<()<>>}<[[][]]<{}<> +<[(<{[[[{[{[[[[]<>)({}())]([()()][(){}])][<{[]()}[(){}]>[[(){}](<>())]]}<[{[()<>]{[]<>}}{<()()>}]( +{[<<[{{([{<[([{}()]([][]))<([]())<<><>>>]<(([]<>)[{}]){<{}()>({}<>)}>>{<<<<><>>(()[])><{{}{}}<[][]>>>{([{}[ +((({[<<<(<[<<{<>{}})[<{}<>>]>{[({}<>){[]}](([]<>)<(){}>)}]>{{<{<{}{}>[[]{}]}({[][]}<()[]>)><{{()}{{}}}[{{}{}} +{(([{[({[([[<([][])(()<>)>(<(){}>[<>[]])]<{[<>[]]({}[])}(<[]{}>{{}{}})>])<<({[()[]]((){})}{{{}{ +([(<{(<<((([<{[]}>[[[][]](<><>)]][<([]){<>{}}>{[<>{}]}])(<[{<>{}}({}<>)]([()[]]([]<>))>))<{<([() +<<{{({<{{{<<({(){}}<[]{}>)><([(){}]({}[]))[[{}()]({}())]>>{<<[{}[]]{()<>}><[[]<>][()<>]>>[{ +[[[{{{[{[(<(([()[]]<(){}>))[(([][])[<>])<([][]){<>()}>]>{[<<()[]><{}[]>>((<>)[(){}]))})(<<[{()()}<() +<{[{((<({<<[<<{}()>[(){}]>[(()()){<>()}]](<[{}<>][{}()]>{<(){}>})>>[<<[<(){}>[<>{}]]>((<()()>{<> +{((<[<([(({[<<{}<>>{<><>}>[<()()><{}[]>]][{[[][]]{()()}}[[<>]{{}()}]]}<(<(<>{})<()<>>>{{() +{(<[(<([{<<<({{}()}({}<>))[<[]><[]<>>]>{<<{}<>>[[]()>>}>[{<<{}()>[[]{}]>}{{{()<>}}}]>}]){(<[({<<<><>><[][]>>} +<[<[(([{<[<{[{[][]}{<>[]}])>][([[[[]{}][{}<>]][[{}{}]]]){(({<>[]}[<>[]]){{[]{}}{()()}})<(<[]<>><[][]>)>}]>}[ +{(<[[[(<([[{{[()[]]{<>()}}{<[]{}>{<>[]}}}(<([]<>)([]())><<()[]>(<>{})>)]]([<([<>](())>{{<><>}<(){}>}>][<( +<<[([[{<[[[[{([]())<<>[]>}]({{{}{}}({}[])}<{()[]}<()<>>>)]{{<({}())[<>[]]><{<>}(()<>)>}([({}{})[<>] +{([({[(({{<[[{{}()>[<>]]({<>[]}(()()))]([[()<>]([]())]{[()[]](<><>)})>{[<<()()><(){}>>[[()()][ +{[[{<{([(([[{({}[]){()()}}{(<>{})[[]<>]}]<[[{}<>]<[][]>]((()[]))>])){(<<([[]{}]([]{})){<{}<>><()()>}>{<{[] +<{({(({{{([<{[()[]]<()<>>}{({}())[{}{}]}><(([]())<<>[]>)<{[]()}[(){}]>>)({(<()>)([{}{}][()<>])}[<[[]<>][()()] +[{{(((([{[{(<(<>())([]{})>[([][])({}[])])<{<(){}>[[][]]}[<[]()>{[][]}]>}((<([]()}{[]{}}>){<({})><{(){}}>}) +{(<{<((((<(<<<[]<>>(<>())>{[{}[])<[]{}>}>)><[[([{}<>]{{}<>})<{()()}{<>{}}>]{[<()()>{()[]}][[< +{(<<[<((<<<<{{<>{}}{<>{}}}[(()())<<>()>]>[[([]())<{}()>]((<><>)<<>[]>)]>{([<()()>[{}]]{[<>[]]<<>()>})<<({}< +<{[{(((<({<<((()<>){[][]})[{[]<>}[[]<>]]>>})>))[<{{{[<{[{}{}]<[]()>}<{()[]>>>[<<{}{}>{<><>}>[[<>{}][{}< +[[{<{<[[<{(([<[]<>>{[]()}]){[<[]<>>[{}]](<(){}>[[][]])})}>]][[<(<<[[[][]][[]()]]<[[]<>]>>>{[({{}()}<{}<>>)]<( +{{((([<((<<[[{()}(()[])]][([(){}]<[]<>>)]>(([[{}()][()[]]](((){}){(){}}])({[<><>]<{}{}>}(([][])<<>{}>)))>)(( +<{([[(<{[<[[[([]())<<>()>][<{}{}>{{}{}}>]{[({}{})({}<>)][(<>{})<{}()>]}]([{{{}()}{[][]}}])>([ +<[<{{<<{{[[(([(){}]({}[]])({<><>}<[]{}>))]{<(({}())){<[][]>}>{{({}<>)[[]<>]}(<<>[]>[[]<>])}}]}}>>[{ +(<<<{({[[[[<{(<><>){()()}}>{[<<>{}>([]{})][[{}<>][[]{}]]}]<{((<>{})[()<>])(({}()){{}()})}>]]{{({[((){})](<[] +<[{<{{[<[{{([(<>())[(){}]]<[<>()]<()<>>>)}}][<[(([()<>]({}())){(()[])<{}()>])<[(<>[])({}())]>]{(( +<{{[{{([<{([((<>{})({}{}))](<({}[]){()<>}>(({}())[[]()]))){([[<>{}]{()<>}])}}<(<[<()[]><()<>>]}<({[]< +{({[[({<{{[[([()])<{()[]}{<>[]}>]]}}>[((<[<([]())({}())>]<<{<>[]}[{}<>]>>>{[[([]{})([])}{<<><>>{<>()} +<<{{<(({[<<(((<><>){{}[]})(<{}[]>))[<<[]{}>([]{})><<[]<>>>]>{{(([]()){<>{}}){{<>[]}((){})} +{<{[({{<{[(([<(){}><[]<>>})[(<()[]>({}<>))({[]{}}[[]<>])])][<{<<[]()>{{}[]}>}{(([]){{}<>}){<{}{}><{} +[[{([<[<[{<([(()<>)}[[{}{}]<[]()>])[[<()[]>]([{}{}][()<>])]>([[<<>{}>]{{[][]}[(){}]}][{[()()](<><>)}])}{<<< +[[[{[<<<<<[(([<><>])<<{}[]><{}()>>)[[<<>[]>[()[]]]{(()<>){<>{}}}]]<<({[]{}}[[]()]){(()<>)[[]{}]}>(({{}{ +[[(([{{<([{[[<()()>({}[])]{<<>[]>((){})}]({{{}}<{}>}((()())({}[])))}(<[{[]}([]<>)]>{{<()<>>(<>{})} +(((({[(<{(<{<[()()][{}<>]>}{<<{}<>>[()[]]>}><<{{()[]}[[][]]}>>)}>>]{([[([({[{}()][<>{}]}){<[{}()]<{} +{[[[{{[[<<{(({[]<>}){{[][]}<<><>>})[[[(){}]<<>{}>]{<<>[]>{{}()}}]}>>]](([([{<[(){}][{}()]>{<[]<>>< +<[[[{<<<{([{(<()()>)<[<>[]]({}())>}{{([]{}){{}}}[[(){}]<<>[]>]}](([<{}()>](({}[])[[]()]))<[[{}()]<{}[]>]>) +(({(({{<{{<((({}()))[<()()>[<>{}]])>}{{{[[{}<>][{}[]]]{<<><>><<>[]>}}}}}>}}[({{({[<[[]()][ +((({[<{[((<[[<<>{}>}{{<>[]}}]>){{({{<>{}}}{{()()}(<>[])})({<[]()>[()()]}{<[]()>{[]{}}})}<[(<()()>{{} +([{{(<<<<[([[{{}{}}<{}<>>]<({}{}){()}>]{{{()<>}[[]{}]}[{[]<>}{<><>}]})][{[[(<><>)[{}<>]][([]( +{<<<[[({<{[({(<><>)[<><>]}{[(){}]{<>[]}})]{<([<>{}]{()[]})((<>()))>}}>[[<[{<{}()>}(({}<>)[{}[]])]<{(<>[] +[({(<[[<[<({({<>[]}{(){}}){([]()>(()[])}})>[[{(<()[]>[<>()])}{[[<>()]({}[])]{(()[])<()<>>}}]]]([{{<<()[] +{[([(([[{<<<{<[]>{<><>}}{{[]}(()())}><[{<>()}[<>[]]]([<><>]([]{}))>>{(({<><>}))({{{}[]}<<>[]>}[{{}()}])} +[([<[{([<<<(<{()<>}<[]()>>{[<><>][<><>]})([{{}}]{{{}[]}(<><>)})>>(({{({}[])<[]>}[(<>())(<>< +[{[{[((<[[[<([<>[]](<><>))({{}()}[()()])>]>]><[{{{((<>[])<()()>)<[()[]]>}<([{}<>]({}())){{()[]}{[]< +{{{[{([<(<<[([()<>]<()()>)(<<>()>(()[]))]<<({}<>)[{}]><<[]<>>{{}[]}>>>>)>(([(<[{()<>}{{}()}]([( +({{{[({[{[({([{}<>]([]<>)){<<>[]>}})<[<[()]<[]>>]<<{{}[])[{}{}]>>>]}(<({{[<>{}]}([(){}]{{}<>}) +{[(({{{([({({(()())[<><>]}{<[][]>(()[])}){[{<>()}[()[]]]<{<>[]}[[]{}]>}}[(<{[])[<>[]]>({{}[]}[()<>]))]){{[{<{ diff --git a/10a.c b/10a.c new file mode 100644 index 0000000..e9ae01e --- /dev/null +++ b/10a.c @@ -0,0 +1,46 @@ +#include + +char cmap[] = { [')']='(', [']']='[', ['}']='{', ['>']='<' }; +int score[] = { [')']=3, [']']=57, ['}']=1197, ['>']=25137 }; + +int parse_line(char *buf) +{ + char stack[100] = {0}; + int sp = 0; + for (char *p = buf; *p != '\0'; p++) { + switch (*p) { + case '(': + case '[': + case '{': + case '<': + stack[sp++] = *p; + break; + case ')': + case ']': + case '}': + case '>': + //incomplete + if (sp == 0) + return 0; + //corrupted + if (stack[--sp] != cmap[(int)*p]) { + return score[(int)*p]; + } + stack[sp] = '\0'; + break; + case '\n': + break; + } + } + return 0; +} + +int main() +{ + char *buf = NULL; + size_t len = 0; + int r = 0; + while (getline(&buf, &len, stdin) != -1) + r += parse_line(buf); + printf("%d\n", r); +} diff --git a/10b.c b/10b.c new file mode 100644 index 0000000..e8fef98 --- /dev/null +++ b/10b.c @@ -0,0 +1,55 @@ +#include +#include + +char cmap[] = { [')']='(', [']']='[', ['}']='{', ['>']='<' }; +long rmap[] = { ['(']=1, ['[']=2, ['{']=3, ['<']=4 }; + +void parse_line(const char *buf, long scores[], int *nscores) +{ + char stack[200] = {0}; + int sp = 0; + long score = 0; + for (const char *p = buf; ; p++) { + switch (*p) { + case '(': + case '[': + case '{': + case '<': + stack[sp++] = *p; + break; + case ')': + case ']': + case '}': + case '>': + //corrupt + if (stack[--sp] != cmap[(int)*p]) + return; + stack[sp] = '\0'; + break; + case '\0': + case '\n': + score = 0; + while (sp>0) + score = score*5 + rmap[(int)stack[--sp]]; + scores[(*nscores)++] = score; + return; + } + } +} + +int longcmp(const void *l, const void *r) +{ + return *(const long *)l-*(const long *)r < 0 ? -1 : 1; +} + +int main() +{ + char *buf = NULL; + size_t len = 0; + long scores[100] = {0}; + int nscores = 0; + while (getline(&buf, &len, stdin) != -1) + parse_line(buf, scores, &nscores); + qsort(scores, nscores, sizeof(long), &longcmp); + printf("%ld\n", scores[nscores/2]); +} diff --git a/10e.txt b/10e.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/10e.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/Makefile b/Makefile index c1bccfe..75a0a9d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS:=-Wall -Wextra -O3 LFLAGS:=-f -BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 9),$(num)a $(num)b) +BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 10),$(num)a $(num)b) all: $(BINARIES) -- 2.20.1