day 10
authorMart Lubbers <mart@martlubbers.net>
Fri, 10 Dec 2021 07:21:59 +0000 (08:21 +0100)
committerMart Lubbers <mart@martlubbers.net>
Fri, 10 Dec 2021 07:22:33 +0000 (08:22 +0100)
10.txt [new file with mode: 0644]
10a.c [new file with mode: 0644]
10b.c [new file with mode: 0644]
10e.txt [new file with mode: 0644]
Makefile

diff --git a/10.txt b/10.txt
new file mode 100644 (file)
index 0000000..47dcbd3
--- /dev/null
+++ b/10.txt
@@ -0,0 +1,90 @@
+[{(<<{(([<<{<<[]()>[<>[]]>[[<>[]]{{}{}}]}<<[<>()][{}]><[<>{}]<<>()>>>>([<([]<>)([][])>(<<>[]>([]()))]<[
+{([[[{{([{{{([[]()][()<>])}[<<(){}><<>()>>{{[]<>}[(){}]}]}<<(<<>[]>{[][]}){<<>()>({}()}}><{
+{[[([{([<(<{{[{}{}]<[]()>}(<<>[]><<>()>)}(<[<><>](<>[])><<(){}>{(){}}>)>{{[({}[]){[][]}]({()<>}<[]<>>)}})>({(
+{[<([(<<{<<<[<{}{}>({}[])][<[]{}>{[]{}}]>{[{[][]}[{}<>]]{(<><>){{}[]}}}>([<[<>{}]([][])>{(<>{})}][{({
+<{{[{[<(<[{[(<()<>>([][]))[[{}()]]]}<{<{{}{}}((){})>((()())([]{}))}<((<>{}))>>]<{[[<()[]>]{(<>[])
+<((({<((<([<[<[][]>[[]{}]]<{[][]}({})>>{<<()>({}())>[(<>)<[]()>]}]{[{<<>[]>{{}<>}}<<()<>>([][])>]})[({{((
+[{<[{<({({[(<{{}[]}[[]()]>{<()()><{}()>})({[{}<>](()())}({[]{}}({}{}))>]([[[<>[]]<()[]>]]([[<><>]({}
+{(<[<<{([([(<[{}[]]<{}{}>><<()[]>([]<>}>)]{[[{()}({}())]<[<>[]]([]<>)>](<[[]{}](<><>)><[[]{}]{<>[]}>)})]
+({([{[({{<([[([]())]{[[]{}]}]{<{(){}}<{}{}>><([]{})>})[(<{<>()}>{(<><>)[[]{}]}){{{{}<>}({}<>)}[<()
+<[<[{((<<{[((<[]<>>[[]()]){{()[]](<>)})(({[][]}[[][]])[(<><>)<{}>])][<[{[]()}{[]()}][{<>{}}({}<>)
+<({(<(<{[[({({()[]}<{}<>>)[<{}[]>{{}()}]}([[{}](()[])][{[]{}}(()[])])]{[[{()[]}]<[[][]][{}]>]}]<(<<<[]()>[
+[<<<{((<{(<([[{}{}]{()<>}]{{{}<>}[<>()]}){[[[]()][[][]]]{([]())[()<>]}}>{({[<>()]}([[][]]<(){}>))<<<{}<
+[[{(([(<{(<({[()()]([]{})}[({}<>)])([<[]{}><()<>>][<<>[]><<><>>])>)[[{[{()[]}{[]}]}]]}>)]{<{{(<[{[[]<>]<()[]>
+<{(<[{{([{{[(<[]<>><()<>>){{{}{}}}]}(([[<><>]([][])]<{()}{{}()}>)<{[()<>][<>]}<(<>[])>>)}])
+(<{({([<([<{<(<>()){()()}>([<>()]({}()))}><(([{}{}]{<>{}})<<[]()><[]{}>})<{([]<>)<[]()>}({{
+[{([(({[(<[[{{{}<>}({}())}{{(){}}<<>>}]<<({}())(<><>)]<<<><>><()()>>>]({(<{}<>>{()[]})}<<{{}{}}({})><<<>(
+(<[(({<(<{({{<()<>>{{}()}}}[[{()[]}{[]{}}]<({}())([]{})>])}{<{<(()[])([]<>)><<<>{}><{}[]>>}<[[<><>](()[]}]>>{
+({({<({({(<{({{}{}}(()[]))(<{}{}>[{}()])}((<{}<>><()[]>)[(()<>)[{}<>]])>)}[[<<{<[]{}>{<>[]}}{{(){
+({<<([(<<<(<{(()())[{}[]]}(<[]<>>[{}{}])>)<(<[{}[]]{[]{}}>({[][]}[[][]])>>><[{([[]()]{{}()
+[({{[[(({[<[([{}[]])<<(){}>(()<>)>]({<[][]>[{}<>]}[<[]<>>])>({(<<>[]>{{}{}}){<[]{}><{}<>>}}([({}<
+((<[[{({<<<([({}{})[[]<>]]{(()<>)[[]{}]})({{<>}[<>[]]}[[<>[]]<[][]>])>>>}[[<<{(<<>()>(()()))<{<>{}}(()
+{{{[[[([{<(<[([]())[[]()]]<(()())([]{})>>([<[][]>]{({}()){()<>}}))><[([[<>[]][[]<>]][<()[]><{}{
+[<{[(<<{({<({{()[]}[{}<>]}{({}<>)[(){}]})[<([]{})><([]<>){()()}>]>})}>[[<{[<<[<>()]<()<>>>{[{}{}]<<>[]>
+<((((((<{(<([[<><>][<>[]]]{<{}<>>})>)(([<[()()]>[(<>[])<[]{}>]][<([]{})([]{})>{{[]}({}())}]){[[[()[
+<([{((({{[({[[()]][{[]}{<>[]}]}<{<(){}>[[][]]}<([]())<[][]>>>)<<{<()()>({}())}{<()()>}>{{[{}()]}}>]}
+<[({<[(<<<<{[[<>{}]([]{})]}>>>[<[[{(<>())([][]}}(<[]<>>)]<<{<>()}{[]<>}>{<()>([]())}>]<<[({}{})<[
+[[([({<({{(<(<()()>(()<>))>[{<<>()>([]<>)}{{<><>}{{}[]>}]){([[[]()]<[]<>>](([]())<[][]>))}}}{<{<{<<>()
+<<{[[<(({(({<{[][]}(<>{})>{(<>())}}{<[[]<>]<{}()>){{[]<>}[()]}}))}))>]]{{<<{{<<<{([][])<{}()>}[[<>()][()[]
+{<{[[(<[((([<{[][]}[{}]>]((([][])([]()))([<><>](<>{}))))){(<[[(){}]<[][]>]>{((()()}({}[]))<[<>{}]{<><>}>
+([[[[[([<{({{<[]<>><{}<>>}{{<>()}[[]<>]}}(<[<><>]{()()}>))<[{({}<>){(){}}}<({}())[()[]]>]{([()<>][[][]
+({([(({<<{{[(({}[])([][]))[({}{})]]}([<<[]<>>([]{})>])]>>[{[[(<<()()><<>{}>>)(([[][]](<>[]))
+{({[<({[({[([[(){}]{(){}}])]<{<<(){}>[{}[]]><{[]{}}[[]<>]>}{{{<>{}}[[][]]}[[[]<>]]}>}([{((<>[])<[][]>)}]
+({([<(((({<[<<[]<>>{{}[]}>((<>{})<[]{}>)]>{(<{{}[]}({}{})><<()<>>(<>[])>)[{(())([]())}]}})))[{<(({{{[]{}}[<
+<<([{{[[<<(<[{[][]}<{}{}>]<[{}{}]<<>{}>>><[[{}{}][{}]]<<{}<>>[{}()]>>)(<[(<>{})<()[]>]{(()())}><[[[]]<{}>][(<
+{[[([<({[{[<[([]<>)<[]{}>][(<>())[[]<>]]>[([<>[]](<>))([()()]({}()))]]{{(<()[]>{{}[]>)[{<>}(()())]}}}{<{{
+([[[(([{{<[{<[<>[]]<<>[]>><(()())<{}[]>>}(([[][]]<{}<>>))]<[[(<><>}{()()}][[{}[]][()[]]]](([{}{}]<[][]>)<[{}
+{({{(<([[{({{<[]{}>[[][]]}{{<>()}<[]{}>}})({({[]})[<()()>{[]<>}]})}<{[[<{}>({}())]{{[][]}<[]()>>]([{(){
+{([((<<{[{{[<({}())[()[]]>(<{}<>>({}()))]}}<{<([[][]]([]{})){(<>[]){()[]}}><[({})[<>[]]][(
+{(((<[([({(([{<>}<[]{}>]))}({{([<>{}](<>{})){(<>{}){[]{}}}}[[[<>[]]]<{()<>}{<><>})]}({({[][]}<[]{}>)[{[]{}}]}
+[{(({<[{(([{<[()[]]<[][]>>([{}<>][()()])}([<<>[]>[[]{}]][<{}[]>({}())])]<({({}[])}[{()()}[{}[]
+[[{{<<<[[{<<[<[]{}><()[]>]{[[][]]{<><>}}>>{({[[]{}][<><>]}(([]<>)<<>()>))(([{}<>]<[][]>)<([]<>)((
+[{{(({[(({[[<[(){}]>(<{}<>>)]<(([]<>)<<>()>)[<[]{}>{<>()}]>]({<[[]{}]{<><>>>}<([<>()][[]<>]
+[{<<{{(((<[[{(<>{}){[]<>}}((()[])[()<>])]<[[()]](<<>[]>[(){}])>](<({{}[]}<()<>>}<[[][]]<{}<>
+<[(<{[[[{[{[[[[]<>)({}())]([()()][(){}])][<{[]()}[(){}]>[[(){}](<>())]]}<[{[()<>]{[]<>}}{<()()>}](
+{[<<[{{([{<[([{}()]([][]))<([]())<<><>>>]<(([]<>)[{}]){<{}()>({}<>)}>>{<<<<><>>(()[])><{{}{}}<[][]>>>{([{}[
+((({[<<<(<[<<{<>{}})[<{}<>>]>{[({}<>){[]}](([]<>)<(){}>)}]>{{<{<{}{}>[[]{}]}({[][]}<()[]>)><{{()}{{}}}[{{}{}}
+{(([{[({[([[<([][])(()<>)>(<(){}>[<>[]])]<{[<>[]]({}[])}(<[]{}>{{}{}})>])<<({[()[]]((){})}{{{}{
+([(<{(<<((([<{[]}>[[[][]](<><>)]][<([]){<>{}}>{[<>{}]}])(<[{<>{}}({}<>)]([()[]]([]<>))>))<{<([()
+<<{{({<{{{<<({(){}}<[]{}>)><([(){}]({}[]))[[{}()]({}())]>>{<<[{}[]]{()<>}><[[]<>][()<>]>>[{
+[[[{{{[{[(<(([()[]]<(){}>))[(([][])[<>])<([][]){<>()}>]>{[<<()[]><{}[]>>((<>)[(){}]))})(<<[{()()}<()
+<{[{((<({<<[<<{}()>[(){}]>[(()()){<>()}]](<[{}<>][{}()]>{<(){}>})>>[<<[<(){}>[<>{}]]>((<()()>{<>
+{((<[<([(({[<<{}<>>{<><>}>[<()()><{}[]>]][{[[][]]{()()}}[[<>]{{}()}]]}<(<(<>{})<()<>>>{{()
+{(<[(<([{<<<({{}()}({}<>))[<[]><[]<>>]>{<<{}<>>[[]()>>}>[{<<{}()>[[]{}]>}{{{()<>}}}]>}]){(<[({<<<><>><[][]>>}
+<[<[(([{<[<{[{[][]}{<>[]}])>][([[[[]{}][{}<>]][[{}{}]]]){(({<>[]}[<>[]]){{[]{}}{()()}})<(<[]<>><[][]>)>}]>}[
+{(<[[[(<([[{{[()[]]{<>()}}{<[]{}>{<>[]}}}(<([]<>)([]())><<()[]>(<>{})>)]]([<([<>](())>{{<><>}<(){}>}>][<(
+<<[([[{<[[[[{([]())<<>[]>}]({{{}{}}({}[])}<{()[]}<()<>>>)]{{<({}())[<>[]]><{<>}(()<>)>}([({}{})[<>]
+{([({[(({{<[[{{}()>[<>]]({<>[]}(()()))]([[()<>]([]())]{[()[]](<><>)})>{[<<()()><(){}>>[[()()][
+{[[{<{([(([[{({}[]){()()}}{(<>{})[[]<>]}]<[[{}<>]<[][]>]((()[]))>])){(<<([[]{}]([]{})){<{}<>><()()>}>{<{[]
+<{({(({{{([<{[()[]]<()<>>}{({}())[{}{}]}><(([]())<<>[]>)<{[]()}[(){}]>>)({(<()>)([{}{}][()<>])}[<[[]<>][()()]
+[{{(((([{[{(<(<>())([]{})>[([][])({}[])])<{<(){}>[[][]]}[<[]()>{[][]}]>}((<([]()}{[]{}}>){<({})><{(){}}>})
+{(<{<((((<(<<<[]<>>(<>())>{[{}[])<[]{}>}>)><[[([{}<>]{{}<>})<{()()}{<>{}}>]{[<()()>{()[]}][[<
+{(<<[<((<<<<{{<>{}}{<>{}}}[(()())<<>()>]>[[([]())<{}()>]((<><>)<<>[]>)]>{([<()()>[{}]]{[<>[]]<<>()>})<<({}<
+<{[{(((<({<<((()<>){[][]})[{[]<>}[[]<>]]>>})>))[<{{{[<{[{}{}]<[]()>}<{()[]>>>[<<{}{}>{<><>}>[[<>{}][{}<
+[[{<{<[[<{(([<[]<>>{[]()}]){[<[]<>>[{}]](<(){}>[[][]])})}>]][[<(<<[[[][]][[]()]]<[[]<>]>>>{[({{}()}<{}<>>)]<(
+{{((([<((<<[[{()}(()[])]][([(){}]<[]<>>)]>(([[{}()][()[]]](((){}){(){}}])({[<><>]<{}{}>}(([][])<<>{}>)))>)((
+<{([[(<{[<[[[([]())<<>()>][<{}{}>{{}{}}>]{[({}{})({}<>)][(<>{})<{}()>]}]([{{{}()}{[][]}}])>([
+<[<{{<<{{[[(([(){}]({}[]])({<><>}<[]{}>))]{<(({}())){<[][]>}>{{({}<>)[[]<>]}(<<>[]>[[]<>])}}]}}>>[{
+(<<<{({[[[[<{(<><>){()()}}>{[<<>{}>([]{})][[{}<>][[]{}]]}]<{((<>{})[()<>])(({}()){{}()})}>]]{{({[((){})](<[]
+<[{<{{[<[{{([(<>())[(){}]]<[<>()]<()<>>>)}}][<[(([()<>]({}())){(()[])<{}()>])<[(<>[])({}())]>]{((
+<{{[{{([<{([((<>{})({}{}))](<({}[]){()<>}>(({}())[[]()]))){([[<>{}]{()<>}])}}<(<[<()[]><()<>>]}<({[]<
+{({[[({<{{[[([()])<{()[]}{<>[]}>]]}}>[((<[<([]())({}())>]<<{<>[]}[{}<>]>>>{[[([]{})([])}{<<><>>{<>()}
+<<{{<(({[<<(((<><>){{}[]})(<{}[]>))[<<[]{}>([]{})><<[]<>>>]>{{(([]()){<>{}}){{<>[]}((){})}
+{<{[({{<{[(([<(){}><[]<>>})[(<()[]>({}<>))({[]{}}[[]<>])])][<{<<[]()>{{}[]}>}{(([]){{}<>}){<{}{}><{}
+[[{([<[<[{<([(()<>)}[[{}{}]<[]()>])[[<()[]>]([{}{}][()<>])]>([[<<>{}>]{{[][]}[(){}]}][{[()()](<><>)}])}{<<<
+[[[{[<<<<<[(([<><>])<<{}[]><{}()>>)[[<<>[]>[()[]]]{(()<>){<>{}}}]]<<({[]{}}[[]()]){(()<>)[[]{}]}>(({{}{
+[[(([{{<([{[[<()()>({}[])]{<<>[]>((){})}]({{{}}<{}>}((()())({}[])))}(<[{[]}([]<>)]>{{<()<>>(<>{})}
+(((({[(<{(<{<[()()][{}<>]>}{<<{}<>>[()[]]>}><<{{()[]}[[][]]}>>)}>>]{([[([({[{}()][<>{}]}){<[{}()]<{}
+{[[[{{[[<<{(({[]<>}){{[][]}<<><>>})[[[(){}]<<>{}>]{<<>[]>{{}()}}]}>>]](([([{<[(){}][{}()]>{<[]<>><
+<[[[{<<<{([{(<()()>)<[<>[]]({}())>}{{([]{}){{}}}[[(){}]<<>[]>]}](([<{}()>](({}[])[[]()]))<[[{}()]<{}[]>]>)
+(({(({{<{{<((({}()))[<()()>[<>{}]])>}{{{[[{}<>][{}[]]]{<<><>><<>[]>}}}}}>}}[({{({[<[[]()][
+((({[<{[((<[[<<>{}>}{{<>[]}}]>){{({{<>{}}}{{()()}(<>[])})({<[]()>[()()]}{<[]()>{[]{}}})}<[(<()()>{{}
+([{{(<<<<[([[{{}{}}<{}<>>]<({}{}){()}>]{{{()<>}[[]{}]}[{[]<>}{<><>}]})][{[[(<><>)[{}<>]][([](
+{<<<[[({<{[({(<><>)[<><>]}{[(){}]{<>[]}})]{<([<>{}]{()[]})((<>()))>}}>[[<[{<{}()>}(({}<>)[{}[]])]<{(<>[]
+[({(<[[<[<({({<>[]}{(){}}){([]()>(()[])}})>[[{(<()[]>[<>()])}{[[<>()]({}[])]{(()[])<()<>>}}]]]([{{<<()[]
+{[([(([[{<<<{<[]>{<><>}}{{[]}(()())}><[{<>()}[<>[]]]([<><>]([]{}))>>{(({<><>}))({{{}[]}<<>[]>}[{{}()}])}
+[([<[{([<<<(<{()<>}<[]()>>{[<><>][<><>]})([{{}}]{{{}[]}(<><>)})>>(({{({}[])<[]>}[(<>())(<><
+[{[{[((<[[[<([<>[]](<><>))({{}()}[()()])>]>]><[{{{((<>[])<()()>)<[()[]]>}<([{}<>]({}())){{()[]}{[]<
+{{{[{([<(<<[([()<>]<()()>)(<<>()>(()[]))]<<({}<>)[{}]><<[]<>>{{}[]}>>>>)>(([(<[{()<>}{{}()}]([(
+({{{[({[{[({([{}<>]([]<>)){<<>[]>}})<[<[()]<[]>>]<<{{}[])[{}{}]>>>]}(<({{[<>{}]}([(){}]{{}<>})
+{[(({{{([({({(()())[<><>]}{<[][]>(()[])}){[{<>()}[()[]]]<{<>[]}[[]{}]>}}[(<{[])[<>[]]>({{}[]}[()<>]))]){{[{<{
diff --git a/10a.c b/10a.c
new file mode 100644 (file)
index 0000000..e9ae01e
--- /dev/null
+++ b/10a.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+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 (file)
index 0000000..e8fef98
--- /dev/null
+++ b/10b.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+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 (file)
index 0000000..b1518d9
--- /dev/null
+++ b/10e.txt
@@ -0,0 +1,10 @@
+[({(<(())[]>[[{[]{<()<>>
+[(()[<>])]({[<{<<[]>>(
+{([(<{}[<>[]}>{[]{[(<()>
+(((({<>}<{<{<>}{[]{[]{}
+[[<[([]))<([[{}[[()]]]
+[{[{({}]{}}([{[{{{}}([]
+{<[[]]>}<{[{[{[]{()[[[]
+[<(<(<(<{}))><([]([]()
+<{([([[(<>()){}]>(<<{{
+<{([{{}}[<[[[<>{}]]]>[]]
index c1bccfe..75a0a9d 100644 (file)
--- 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)