aboutsummaryrefslogtreecommitdiff
path: root/2021/10
diff options
context:
space:
mode:
authorMarvin Borner2021-12-10 16:10:24 +0100
committerMarvin Borner2021-12-10 23:18:46 +0100
commitf27e912d003742cbeee276029b1d4ff03d2725fb (patch)
treee1c13af9289947a8d13bc1d5e4a9c1566c6dbf2b /2021/10
parent9eabd251029d1da7f2909af0520f229dd74be3ea (diff)
Lele
Diffstat (limited to '2021/10')
-rw-r--r--2021/10/Makefile19
-rw-r--r--2021/10/input90
-rw-r--r--2021/10/solve.c146
3 files changed, 255 insertions, 0 deletions
diff --git a/2021/10/Makefile b/2021/10/Makefile
new file mode 100644
index 0000000..4a881b7
--- /dev/null
+++ b/2021/10/Makefile
@@ -0,0 +1,19 @@
+DEBUG = -Wno-error -Og -g -s -fsanitize=undefined -fsanitize=address -fstack-protector-all
+CFLAGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors -Ofast
+
+# Not the best makefile but idc
+
+debug:
+ @gcc $(CFLAGS) $(DEBUG) solve.c -o solve.o -L/usr/lib -lcrypto
+
+build:
+ @gcc $(CFLAGS) solve.c -o solve.o -L/usr/lib -lcrypto
+
+clean:
+ @rm -f *.o
+
+run: debug
+ @./solve.o
+
+time: build
+ @./solve.o
diff --git a/2021/10/input b/2021/10/input
new file mode 100644
index 0000000..88ead12
--- /dev/null
+++ b/2021/10/input
@@ -0,0 +1,90 @@
+(((<(([{{((<[[<><>]<()[]>]{[{}()]<(){}>}>[<([]{})>])(<<(<><>>[<><>]>(<{}()><(){}>)>[(([])<[]()>)])){<((<{}()
+{(({({<[[[{[{({}{})[[]{}]}[{(){}}((){})]][{((){}){[]{}}}<(()())<<><>>>]}]]]{([<{<<()()>([][])>({[]()}[{}<
+[[{<(([<<{(<[[()()]]{[[]()]<{}>}><{{{}()}<<>[]>><(())[()[]]>>)([{[[][]][()()]}{{()()}}](<{{}()}>))}[([{<<
+{<[{<{[({[[[[{<>{}}<{}()>]<{<><>}{[][]}>](<<<>[]>[()()]>[{[]{}>[<>()]])]{{{[()()][<><>]}<[{}()][{}{}]
+(<[{[{[((<{[[<()()><{}{}>][(<>{}){{}{}}]]{<[<><>]>{{{}[]}(<>[])}}}(<[[[]{}](<>{})]{[[][]][[][]]}>)>)){<{
+<<<(<(<[[[<[{{[]<>}([]())}([[]])]([[[]<>][()()]]{<()()>([]{})])>]{[[<{[]{}}([]{})>{(<>[]){{}{}}}]]{({{<>[]
+[{<(({(<{<{<<{<><>}>({<><>}<<><>>)}{[{[]<>}[[][]]][<()<>>]}}<[[[()()]{<><>}]((<>)[{}()])]{<{()<>}
+<[([{{{({<({(<(){}><[]()>)((()())[[]()})})[<{<<>>((){})}<[<><>]{(){}}>>{{(<>[])({}[])}(<()<>>)}]>})}({{({
+{[{{(<[[[<(((([]())[[]<>]){(()())[[]<>]})[{([]<>)[<><>]}<({}{})>])>{[(<<<>{}>(()<>)>)]<{([<><>]<[]()>><<{}[
+(<<[<({[(<{[({<><>}<{}<>>)]}<<([<>[]])(<{}{}>[[]{}])>>>(([{[<>[]][{}[])}<(<><>)({}[])>](<<()
+{{[<(((<([{(<[<>{}]({}())>){{[{}]<[][]>}[{<>()}{<><>}]}}][<<{<[]<>>[{}<>]}[<{}{}>]>[<[[]<>]<()()>>[[[]
+<<<([([{{<({[{[][]}[{}<>]]}<[(()<>)[{}<>]]>){{{[()<>]}(<<>>({}[]))}(([[]()][[]{}])<([]<>)[<><
+({[[[([[([<{<{{}()}{<><>}>}>])[{(({([][])[{}()]}{(<><>)[[][]]})<<<{}>[{}[]]>(({}())<()<>>)>)
+<[{[[[<({<<[{<<>>{{}{}}}{[()[]]}]<[<[]<>>{<>{}}]>>>((<{{[]{}}{<><>}}>[{({}{})<{}<>>}]))}[({
+<({[<{(([[[{<<()<>>((){})><{()[]}<{}()>}}[([<>{}][()<>])]]<(([(){}])<{(){}}[[]()]>)<{[(){}](<>[
+[<<{{[<<{[<{({<>[]})({{}<>})}><<<<()[]>(()<>)>>[{{[][]}[[][]]}((<>)<<><>>)]>][(<{[[][]]<[]<>>}>(<((){}){[]<
+({[({(<{<<<<{({}())(()<>)}><({{}[]}<<><>>){[{}<>]}>>]<((([{}{}]<[][]>)({[][]}<{}()>)))<<{[[]<>][[]()]}{<<><
+<[<<([<[(<<{[[(){}]<[][]>]<({}<>)[(){}]>}<[({}[]){()()}]({{}[]}[()()])>>><(<({{}<>}{[]()})[(<>
+[[[([<<[<[<{[[(){}]((){})][(()<>)<()>]}(<{[]{}}{[]}>)>]{{[{{<><>}<()<>>}{[(){}]<<>{}>}]([[[]<
+((([[[{<{[<{[{<>{}}]}({<[]>[{}{}]}<({}{})([][])>}>(((({}{})<{}()>){<<>{}>{{}<>}})(<{{}<>}{{}}
+[<[{[({<[({{{<<>{}><()[]>}}{([<>()]{{}[]})({()[]}[<>[]])}}[{(<()[]>([]{}))<<()<>><[]()>>}{[<<>
+<{{([({<{([<<(()<>)[[]{}]><{()()}<<><>>>>]){{<{{()()}<()<>>}>[{(<><>)<[]()>}]}[((<{}()>){[()[]]{()()}})]
+<{[([{{[[[<<[<()<>><[]()>][(<>())(()<>)]>(([{}{}]([][])){<[]<>>([]{})})>{[[<{}[]>((){})]{<<>
+{[<([{({{({({[[]()]{(){}}}<([]{})[<>{}]>)<<[()[]]>([()[]])>}{[(({}<>)<()()>)[{(){}}<{}{}>]]}>{[[{
+<<({{<<{<[{[{({}{})[()]}(<{}{}><[]()})]<{[[][]]({}[])}{(<>{})<()[]>}>}<[({{}<>}{<>()}){({}[
+([[[<<{<([[(({<>()}{[][]})[[()<>][()]]){{{()[]}(<>[])}<([]<>)[[]<>]>}]])>{(({{{[<>{}]{[][]}}}}<[(({}[])<<><>
+[<(({<[[{([{<<()()>(()<>)><<[]{}>{()<>}>}{<<{}>>{{[]{}}<()<>>}}]{<{[{}<>]([][])}}<{[()()][{}]}>})(([[<{}
+<{(<{(((<{<<(<<>>{<>{}})[([]<>)(()[])]><[([]{})((){})}<<{}[]>(<><>)>>><{{{<>{}}(<>[])}{[()()][()
+<{<[<<[({<[[<{{}[]}><({}{})({}[])>]]<[<{{}<>}>]({[{}]{<>[]}})>>[<(([{}<>]){<<>[]>[<><>]})[(<{}()>{[
+(([[(([<<(<(<{()()}(<><>)><(<>{})<[][]>>)[<((){})<{}{}>>{{()()}{<>{}}}]>[<<<()()>(<>[])><(()[
+<{({{({<[(<{<(<>{})<<>{}>>(({}[]))}(((<>())<{}<>>)(<()>))>([[([]<>)<{}[]>][[<><>]<(){}>]][{<(){}>(
+<<{(<(({[<<{[(<><>)[<>{}]]{<[]()>({}{})}}<(<{}[]>)[{<>()}<[]<>>])>>]{((({(<>[]){()<>}}[{[][
+(([((<([[([<[{[]{}}({}[])][<{}{}>[()[]]]>(<<{}[]>>)])([{(<[]{}>([]<>))}<[{<>}[{}<>]]([(){}][{}[]})>]
+(([{(([{[<{<<<[][]><()<>>>(({}[]){{}<>})>}({{<[]()>({}[])}([(){}][{}<>])}(([[]<>][[][]])[{{}()}<<>
+([(<<([{<[<(<<(){}>{[]()}>((()<>){[]<>}))[{([][])}<[<><>]{{}{}}>]>(({(<>[])[(){}]}(({}[])<<>()>)))]>{{[{[[
+{(((([<{({[(<[<>[]][[]]><(()[])[[][]]>)]({{{(){}}[<>[]]}((()[])<[][]>)}{{<()<>>([][])}<{()>>})}<<(({<
+<[({<<{{(({<{{[]{}}(<><>)}<[()[]]<{}()>>)<<{{}[]}>([()[]]({}{}))>}){({{[(){}][<>()]}[[()()][()[
+{<([[{((([[{([[][]]<()<>>)<[<>{}]<{}()>>}]]((<[([]{})[<>()]][<[]>{<>}]><({{}<>}{[]()})(<()()>(<>{}))>)))){(([
+{(([{([{((({[{()[]}((){})]{{[]()}[()()]}}<<<{}[]>>({()()}{[]()})>)((({<>[]}[<>{}])(([]{})))[[{()[]}{<>{}
+(<(<[[(((<[<<{{}[]}[[]{}]>[{()[]}<(){}>]>]>)[[([[{()<>}<<>()>]<({}{})<{}{}>>]){{[{<>[]}{()()}
+(([[(([(<{([[<()()>[{}{}]][<[]{}><[]()}]]{<<{}<>>>})[[{([]())<[]()>}[<{}{}>[<>{}]]][((()())([]()))([[]
+[<([<{({({[[({()<>}[{}{}>)]{([{}()]{{}})[[[]{}]<{}{}>]}]})<{{{{(<>)([]{})}[<[]()><{}{}>]}}{{<[{}()]{{}
+<([{[([<(([[(<{}()>([]{}))({{}}(<>()))][{(()<>]([]{})}[[[]<>]{()[]}]]]((<[[]<>][<>]><{{}{}
+<<([[<{[[([[{<[]<>><[]{}>}][([<><>]([]()))<[()()]([]<>)>]][[<[()<>][<>[]]>({<>{})[<>{}])]<
+<{([[(<(({{<({()}[{}[]])(<[]<>>)>}{{[([][])({}<>)]<<{}{}>{<>[]}>}[[[[]()]({}())]((())[[][]])]>}(
+{[[{[<[<<(<{<(<>()){{}<>}>}[<<{}[]>[[][]]>((()[])[()()])]}[{<{{}{}}[{}{}]><[[][]][(){}]>}[[[<>{}]<
+{<<{[(<{{[[([<[]()>{{}()}][{<>{}}[<>()]])]({(<[]{}>){{[][]}([]())}}<[[<>]<<>{}>]<{[]()}({}{})>>)](
+[<[[[{<[{[(([{<>[]}<{}<>>][{()<>}[[]<>]]){([[]()]<()>)<[{}{}][{}{}]>})[((<{}{}><<>()>){([]<>]<[][]>})({<<
+{<(((<{<{{{<[([]{}){[]{}}]<[[]<>][<>()]>>(<[<><>][<>{}]><[(){}][()()]>)}((<{{}<>}<()[]>>{{[]{}}{
+(<<[[[[{[{{(<(<>{}){[]}>{<[]<>>([][])}){{([]())(()())}}}}){[[{(<<><>><()<>>)<(()[])<{}()>>}]]}}({[(<{[[]()
+{({(<{<<<<((<[()<>]((){})>)){<<{<>}([][])>>(<<()()>([]())><[{}{}][{}<>]>)}>>([([[[[]<>]<[]()>]{<()[]><<>()
+<{[(({{([(({<<[]<>]>{<<><>>[<><>]}}<(<()()>)[(()<>)]>){[(<{}{}>[{}<>])]})]{(<<[[[][]]([]{})]>>)[<([[()]
+{({{<(<<{[([((()<>)<{}<>>)<[<>[]]{{}<>}>](<([][])(<><>}>))[[([[]{}][<><>]){[{}()]({}{})}]<([{}[]]{<><>}
+<({[(([[[<<((([]{})<()[]>)((<>)<[]>))(<({}<>)[()()]>[([]<>){{}<>}])>[{[([])([]())]<[()()]<{}<>>>}
+{{{[[<{{{[[[(({}[]))]{([{}()][[][]])[[[]<>]([]<>)]}]]((([[<>]{[]{}}]<[<>()]<(){}>>)({[(){}][
+[{{([{{<(<<({[(){}]{()()}}[[[]{}]<<>[]>])>>[<<<{{}()}({}[])>><(<()()>(()()))({[]{}})>><{({<>{}}{<>[]})[<{}
+<<<<{{<<[[<[((()[])<{}{}>)<(()[])({}())>](({<>}{{}[]}}{[(){}]{()<>}})>({(<{}<>>(()[]))<<{}[]>{{}[]}>}<{{[][]}
+<<((({[{[{<([([]())<()()>][<{}[]>([])]){({()}<()()>)<([]())(<><>}>}><<<{{}<>}[[]<>]>[[()](<><>)]>
+(<{[[[[([([[<{<>{}}<[]()>>{<<>[]>}]{((<>{}){[]<>})<(()())([]{})>}](<[([]<>)(<><>)]{<[]<>>(<
+[{<(<<<[(<{<[[[][]](<>())][<{}{}><{}()>]><{<[][]><<>[]>}{({}<>)}>}<<[({}())([]())]<<{}{}><()<>>>>[(
+[[([({[{([[(<[(){}]<[]{}>>((())(()()))]]((({<><>}(()())){{{}()}}){{[<>()]{{}()}}<[[]<>]<{}()>>})]<(({<[][]>
+<<{[(<({{({(<{{}<>}(()<>)><{[]{}}[[]{}]>){[<<>()>{{}}]((()[])[[]{}])}}[((({}<>)[()()])<[{}[]]<<>[]>>
+[<{<({{({{{({{()[]}[()[]]})<{(<><>)[()<>]}{{<>{}}}>}<<{({}{})({}<>)}[[()<>][[]{}]]>[([{}[]](()))(<<>
+{{({{{[[[(<[<{<>()><{}[]>>{<[]<>>[<>]}]>[<<[<>()]([]{})>{(<>)(<><>)}>(<(<>{})([]<>)>(<(){}>[{}[]]))
+<{[{[{[(<<({[({})([][])][[(){}]([][])]}<{<{}<>>{[][]}}{((){}){{}{}}}>)[<{[[]{}][{}{}]}({()()}([]{}))>[[[<
+({<<{[<(({[[([[][]])[(<>[])[{}()]]]<{<{}<>><[]<>>}<({}{})<{}()>>>}(<(<()[]>[{}[]])<([])<<>[]>>>{<[<>()]
+<<{<{{([(({([{[]<>}[{}{}]]([()()][[]{}]))}[<{{[][]>{{}[]}}{[<>[]]{{}()}}>{((()<>)[(){}])}]))][({[<(<[]
+({<[[{((<[[[({<>[]}{[]{}})({{}[]][<>[]])](({()<>}<{}[]>)(({}<>)<[]<>>))][<<<(){}>>{(<>{}){(
+({(((<{[([{[(([][])([]<>))[{[][]}<[][]>]]}((<((){})(<><>)>(([][]){<>}))([({}[])<[]()>]{<<>[]>{(){}}}))]({<<
+([[[[[([<<{<[<[]<>>([]())]{{<>{}}<<><>>}>[<[()[]][[]<>]>({<>{}}<()})]}[[[(()[])({}())]]((((
+([(<<{<(<{<{[[<>]]<[<>[]]({}())>}><<{(()<>)(<><>)}{(<>[])<{}[]>}><<[<>()]<<>>><{{}{}}>>>}>)>}>{{
+({<<{([({{([{{<>}({}{})}([[]()]{[]()})][{<<>()>([])}<[[]{}]}])(([[[]{}]{()[]}]([{}()]<()<>>))
+(<{<[{[(<{([(((){}))[((){})<()()>]]{{[[][]]<(){}>}((<><>)<[][]>)})}{((<([]{})<<>[]>>{<[]{}>{{}<>}}))[({{<>(
+<<<{{([[[{[{(((){})[<>[]])([{}()){()<>})}{{({}())}{{<><>}}}][<<[[]<>](<>)>{[()()]{{}()}}>[[<{}{
+<(<[{[([[(({[<{}<>>](([]{})([]()))}({<{}[]><()<>>}<[[][]]<[][]>>))<{[<()()><(){}>]([<><>]({}[]))}>)[[
+<({<<[(({{{([{{}<>}<()()>](({}{})<()<>>))((<(){}>([]{}))([<><>]))}{{<{{}{}}{<>[]}>[{(){}}<(){}>]}(
+{{{([[([[<[(<<[]{}><[]()>>(<{}>(<>{})))[([[]{}](()<>))((<><>)[{}()])]]>]<([<((<>[])(<>))[{()()}({})]>]
+{[<{((([<[({{{()[]}{()()}}((<>{})[[]<>])>){(<[[][]]>[(()())(<>[])])[<{<>[]}({}<>)>]}][(([[{}()]({}
+{<{[(({{[({[[[()()][(){}]]{<[]()>(()[])}]<(<<>()>({}{}))>})<(<<([]<>)(()())>{((){}){<>()}}>)>]{{([[[{}]]]((<<
+<<{({[{[[<[({(<>{})({}[])}<([]())[(){}]>)<<{(){}}(()<>)>{({}{})<{}<>>}>]{<[[()()]{{}<>}]({{}}<()()>)>[<
+({([{<<{[{(({(()())([][])}({{}[]}((){}))))}([{<{()[]}({}())>[({})[[]{}}]}<([<><>]([]()))>](<{(<>())(
+([<{{({<{[[{<{[]<>}>}([{[][]}[[][]]])]({[{[]<>}[[]<>]](({}[])[()()])}{({()()))<{{}[]}{[]{}}>})][[([<()
+((({<<(({(<{[<<>[]>]{[(){}]<()()>}}{<{[]}{<><>>>([<>[]]<<><>>)}>{<({(){}})[({}{})([]{})]>({{<>[]}<{}[]>}<<[
+[{<({[[([<<[(({}[])((){}))]({[[][]][[][]]})>(([{[][]}[<>()]]{{<>()}(<>)}){<{<><>}){[()[]]{{}[]}}})>])]({(
+{(<(([{{<{[[[(()[])(()<>)][{<>{}}[[][]]]]][{{<{}<>>{{}[]}}({()<>}[[]{}])}{[<{}{}>[[]<>]](<<>{}><(){}>
+[(<[<<<({{[<(({}())<()()>){[[]{}]}>({({}())([]{})}([[]()]))]{[[<{}{}><[][]>]<<{}[]>({}())>]{{{()[]}{[][]}
+([{[{<<<[{{({[{}()]}<<<>[]>{<>[]}>)((([]<>))([<>{}]))}{{[([]{})<(){}>}((()[])<[]()>)}({[(){}
+[({[<[[[(((<<[[]]<[]<>>>>{{([]<>){()[]}}})(({(<><>)({}())})<({{}[]}{[]()})[{()<>}([][])]>)
+<[[{[[{{[[[<<{()[]}<[][]>>{<<><>>[{}]}>{[<{}<>><[]<>>](((){}))}]{{<((){}){<>{}}>{(<>())[<>[]]}}<<{{}()}((
+(<[{(<<([[([[<{}{}>[<>[]]]][[{{}()}]<[<>()][()<>]>])<{[(<>{})(()[])]{[{}()][<>()]}}<((<>[]){<>()})(<()
diff --git a/2021/10/solve.c b/2021/10/solve.c
new file mode 100644
index 0000000..e25c27f
--- /dev/null
+++ b/2021/10/solve.c
@@ -0,0 +1,146 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define FORLINE \
+ char *line = NULL; \
+ size_t len = 0; \
+ while (getline(&line, &len, fp) != EOF)
+#define FREELINE \
+ if (line) \
+ free(line)
+
+enum bracket {
+ NONE,
+ ROUND,
+ SQUARE,
+ CURLY,
+ ANGLE,
+ INVALID,
+};
+
+enum side {
+ HUH,
+ LEFT,
+ RIGHT,
+};
+
+static const int syntax_points[] = { 0, 3, 57, 1197, 25137, 0 };
+static const int complete_points[] = { 0, 1, 2, 3, 4, 0 };
+
+#define LBRACKET(ch) \
+ ((ch) == '(' ? ROUND : \
+ (ch) == '[' ? SQUARE : \
+ (ch) == '{' ? CURLY : \
+ (ch) == '<' ? ANGLE : \
+ INVALID)
+#define RBRACKET(ch) \
+ ((ch) == ')' ? ROUND : \
+ (ch) == ']' ? SQUARE : \
+ (ch) == '}' ? CURLY : \
+ (ch) == '>' ? ANGLE : \
+ INVALID)
+
+#define RORL(ch) \
+ (((ch) == '(' || (ch) == '[' || (ch) == '{' || (ch) == '<') ? LEFT : \
+ ((ch) == ')' || (ch) == ']' || (ch) == '}' || (ch) == '>') ? RIGHT : \
+ HUH)
+
+/**
+ * TODO: syntax() and complete() can probably be merged together
+ */
+
+static char *syntax(char *p, char **failure)
+{
+ if (*p == '\n' || !*p || *failure)
+ return p;
+
+ enum side rorl = RORL(*p);
+ if (rorl == LEFT) {
+ char *close = syntax(p + 1, failure);
+ if (*failure)
+ return p;
+ if (RBRACKET(*close) == LBRACKET(*p))
+ return syntax(close + 1, failure);
+ *failure = close;
+ return p;
+ } else if (rorl == RIGHT) {
+ return p;
+ }
+
+ fprintf(stderr, "Unknown '%c'\n", *p);
+ return 0;
+}
+
+static char *complete(char *p, long *score)
+{
+ if (*p == '\n' || !*p)
+ return p;
+
+ enum side rorl = RORL(*p);
+ if (rorl == LEFT) {
+ char *close = complete(p + 1, score);
+ if (RBRACKET(*close) == LBRACKET(*p))
+ return complete(close + 1, score);
+ *score *= 5;
+ *score += complete_points[LBRACKET(*p)];
+ return p;
+ } else if (rorl == RIGHT) {
+ return p;
+ }
+
+ fprintf(stderr, "Unknown '%c'\n", *p);
+ return 0;
+}
+
+// For qsort
+static int compare(const void *a, const void *b)
+{
+ if (*(const long *)a < *(const long *)b)
+ return -1;
+ return *(const long *)a > *(const long *)b;
+}
+
+static void solve(FILE *fp)
+{
+ int first = 0;
+
+ long scores[100] = { 0 };
+ int score_cnt = 0;
+
+ FORLINE
+ {
+ char *failure = 0;
+ syntax(line, &failure);
+ if (failure && *failure && *failure != '\n') {
+ first += syntax_points[RBRACKET(*failure)];
+ } else {
+ long points = 0;
+ complete(line, &points);
+ scores[score_cnt++] = points;
+ }
+ }
+ FREELINE;
+
+ qsort(scores, score_cnt, sizeof(scores[0]), compare);
+
+ printf("%d\n%lu\n", first, scores[score_cnt / 2]);
+}
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ FILE *fp = fopen("input", "r");
+ if (!fp)
+ exit(EXIT_FAILURE);
+
+ clock_t tic = clock();
+ solve(fp);
+ clock_t toc = clock();
+ printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
+
+ fclose(fp);
+ return 0;
+}