commit 6b31037dba861e5afcf1e7246a6ba4a32aa65675
parent 3cb2fa46a54b8bdfdf7d2c59ad7e3c8c3cf510c7
Author: bsandro <[email protected]>
Date: Fri, 10 Dec 2021 20:21:57 +0200
Day 10, puzzle 1
Diffstat:
5 files changed, 274 insertions(+), 0 deletions(-)
diff --git a/day10/Makefile b/day10/Makefile
@@ -0,0 +1,26 @@
+NAME=$(shell basename ${PWD})
+CC=cc
+SRC=$(wildcard *.c ../common/*.c)
+DEPS:=$(wildcard *.h ../common/*.h)
+OBJ:=$(SRC:.c=.o)
+CFLAGS=-O2 -std=c99 -Werror -Wall -Wextra -I. -I../common
+LDFLAGS=-lc
+
+all: $(NAME)
+
+.PHONY: clean run
+
+clean:
+ rm -f $(OBJ) $(NAME)
+
+%.o : %.c $(DEPS)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+$(NAME): $(OBJ)
+ $(CC) $(OBJ) -o $@ $(LDFLAGS)
+
+run: $(NAME)
+ ./$(NAME) input.txt
+
+test: $(NAME)
+ ./$(NAME) test.txt
diff --git a/day10/input.txt b/day10/input.txt
@@ -0,0 +1,90 @@
+([<([{{<<(<<{[<><>)}<([][])<<>{}>>>{[{(){}}(()[])]<{()()}<()()>>}>([<<{}<>>(<><>)><[{}[]](<>())>][
+({{<<(<<<<{{[({}{})<[]()>]<([]{}){[]<>}>}{([{}[]]}}}>({(((()()))<{<>()}[<>()]>)[{(<>{})(()[])}[{[]
+[{{[([<({[([<{[]}({}<>)>]{(([]())[()[]]){<[]<>>[<><>]}}>{<[(()<>){[]<>}][[<>{}]<()()>]>[[[<>[]][()
+{(<<{([([<(<{(<>[])<()<>>}>){[[<<>()>[<>[]]]{[{}{}]{[]<>}}]{<{{}<>}{{}()}>{[()()]<[]<>>})}>[{({[{}
+<{<<<<<{{{({<<[][]>[()[]]><<(){}>([]{})>}{{<[]{}><{}[]>}{<{}{}>({}<>)}}){([(()[]){[]()}][<[]()><[]{}>])
+<{({{<{{{<(({(<>)({}[])}{<()>{(){}}})[[(<><>)[[]]]<[{}[]]{<>()}>])>{<<([{}()]{[][]})({{}{}}{()})><
+<([[<<[{[{({{({}<>)[(){}]}{[{}<>]{[]{}}}}({[()()]<<><>>}(<{}[]><[]{}>)))}<({<<[]<>>({}[])>
+[[{<{{[{<{<[{[()[]]<()()>}][<(<>[]){<>()}>[{<><>}(<>())]]><[{{{}<>}<{}{}>}{[{}[]][[][]]}]<[([]<>){[]{}}]<(
+[({[<([[{<[{{(()[])<[]{}>}<[{}<>>{[][]}>}(<<[]{}>([]<>)>[([]){()[]}])]<[[[[]()][<>{}]]<(<>{})((
+<{[[{{{[([([{[<>()]{[]{}}}<{[][]}[<>()]>](<<<><>>{()()}>))<<{{<><>}[{}()]}{(<><>)[<>{}]}>>])]{[<<[<([]
+[[(<<<({[(((([<>()]([]{}))((<>){[]()})})[[<[[]<>]<{}<>>>[<()()>[()<>]]][[[<>{}]{[][]}]]])<<(({{}[
+{{{[[<<{{[{<[[{}()]]>[{[<>[]]([][])}<{{}()}{{}[]}>]}([[{()}{(){}}]([<>{}]<{}<>>)][{{[]<>}}[{{}{}}[{
+{[{[(<{{([{<<<[][]><()<>>>[{{}()}{[]<>}]>({<{}()>[()[]]}<<()[]>([][])>)}{[([[]{}][<><>]){{{}[]}<
+{[({<{[<{{[(({()<>}({}[]))([()]{<>[]}))({{[]{}}<<>[]>>)]{((<()[]>{(){}})[{[]<>}])}}}<[[<<<(){}><()
+[[[[[{{[{<<[<<{}<>>>{<()()><{}[]>}]>(<([()<>}<<>[]>)(((){})<()[]>)><({{}{}}<[]()>)<{{}<>}(
+{{<<[<{(<[([(<{}<>>[[][]])[(()())]]{[([][])({}<>)]<(<>{}){[][]}>})([{(<>{})<()<>>}])]>(([[[(<>{})](<[]
+<<([{<(<{({<{({}<>)<()()>}{<<>[]>[{}{}]}>(<((){})>(<[]()>[<><>]))}<<(([])[()])<([][])(()<>)>>>)([{([<>{}]([]<
+{[[{{[[({{[([{<>[]}[{}[]]]){(<[]{}>[()()]){[<>{}][<>]>}]}{<{<<{}()>({}())>}{[[<>[]]{<>{}}]<[{}[]]{{}{}}>}
+{<[[{({<{<[<(<(){}><<>{}>)>{[[(){}]]{({}())({}[])}}]{[{{(){}}}]}>}(<{{{([]{})[{}<>]}(<<>{}>)}(
+<[[<(<([[({(<{<>()}([][])><(<>[])[<>{}]>)((<<>()>[[]()])<{()<>}<()()>>)}>[[({{{}<>}[[]<>]}
+<{([{({[[({([[{}()]<()()>][<()[]>[[]<>]]}([<(){}>{<><>}][<{}<>><<>{}>])}[(([<>[]][<>[]])){
+<{[<(<(<[[[{[{<>{}}]((())({}()))}][(([{}<>]{{}{}}))[{[[]()]((){})}<[<>[]]([][])>]]]][[((({
+({[<<([[[<[[<(<>{}){{}[]}>[((){})[<><>]]][{{<>{}}{(){}}}(<[]{}}({}<>))]]{((<{}[]>({}<>))<<<>{}>[[]{}]>)[((
+{[<<[([[([{<<[{}{}]({}[])><<{}<>)(<>[])>>[([()()]{<><>}){[[]{}]{(){}}}]}{<([<>]([]())){<(){}>{()<>}}><<{
+[<[[[[({{([{{(<>[])<<><>>}<{[]{}}<<>>>}(<{[]{}}<<>()>>)][{(<<><>><()[]>){[{}()]>}<[{{}{}}{<>[]}]>]
+{<{[<(([{{({[<<>[]><{}[]>]<{<>{}}{()()}>}([<{}{}>{[]<>}]{<{}<>><[]>}))<([([]<>){[]{}}]<{[]<>}
+[<<(({<<[{[([[<>]<()[]>]{[[]<>]{()<>}}){{<<><>>{[]<>]}[({}<>){()<>}]}](<{{()[]}}({{}[]}<{}[
+({(<{<{<([([[((){})<{}<>>]<[{}[]](<>{})>])])<<[{<<<>[]>>}<[(<><>){<>{}}][(()[])(<>{})]>]<(<{<>{}}
+([<{<<[([({<<<()()>(())><[[]<>]{<>[]}>>([(()())]{[[]()]<{}<>>})})[<[[(()<>){<>{}}]][<(<><>){<>[]}>]><<([{}
+([[<[[[({[<<[[{}()](()<>)]<{()[]}>>{<[{}<>]<()[]>><<[][]>>}>]<(<<[{}<>){<>}>{<[]()>[[][]]}>)>}({
+(<[(<[([<{<{<[(){}][<>()]><(<>())(()<>)>}{{[{}>([]<>)}[<<>{}>(()())]}>[{([<><>]<()()>)}{([()]{()<
+({{{{<[{{<{<<(()[])>{[(){}]{[]{}}}>}{{([<><>][<><>])<(()<>)[[][]]>}<<(()<>)><{<>()}>>}>}}][(<{(<[{<><>}[{}[
+<{[{((([({{({<()()>(())}){([()<>]((){}))<(()<>)[(){}]>}}{[{([]())((){})}[{[]<>>{<>{}}]]<<<()[]><()<>>>((()()
+{([({[(<[<([{[<>[]][()<>]}{[(){}]{{}<>}}][<<{}()>>[<<>()>[()}]])<((({}{})[[]<>]))<{[[]{}]([]<>)}>>><{
+([[[{<[<([{(<<<>()>({}<>)><[<>{}]>)[[<<><>><()[]>](<()<>>(<>[]))]}([({()()}([]())){<[]{}>[[]<>]}][<
+<<((<({{(<((<({}[])(<>())>{({}[])({}<>)}))<<<([]<>)[()[]]>{<()<>>[[]<>]}>>)({(<[<>{}]((){})><{()()}[{}(
+{({([(({{<{{{<[]()>{{}[]})[[(){}](()<>)]}(([[]{}]{()<>}))}>{(<[{()()}]{[<>[]][()()]}>{{<()()
+{[(<{<[<<[{<<<<>{}>>[[[]{}]<<>()>]>((<[]{}>{{}()}))}{({[{}()](()<>)}<<[][]>[{}<>]>)({[{}[]]}<<{}[]
+{{{<{<{[[{([{{[]()}}({()[]}(<>))]<((<><>)({}{}))>)}]]{[({<(<{}[]>{<>{}})((()<>)([]()))>})[<[[{<>{}}<<><>>]<{<
+[[<{<<[(<<<([<{}()>[{}()]](<{}[]>[{}()]))[([[]<>][{}{}])[<[]()><[]()>]]><[[[[]<>]({}{})][{[]}{<>[]}]][<[<>
+(<<<<(<<[<[(({[]()}[[]<>]))({(())(()[])}[<<>[]>{()[]}])]{<({{}{}}(<>{}))><[[<>[]]<[]()>]>}>{({{(<>()){
+<<(<[(<<<{({[<()>{()()}][([]{})([][])]}{({{}}{<>{}})<(())[{}()]>})}[[{<[[][]]{()}){[{}<>]<{}(
+<<{<{{[<[(({({[][]}<<><>>)}<((<><>)[{}[]])<<[]<>>>>)[{{[()[]][[]<>]}{({}())<(){}>}}])({[([[]<>](<><>))<
+<<{<[(<(<([[{{()()}{<>()}}(<<>()>({}[]))]<<{{}{}}{()<>}>(({})(()()))>])[[([(<>[])(<>)](<(){
+({[((({[([<{[{<><>}[()[]]][{[][]}{(){}}]>>(<{{[]{}}<()[]>}<{()<>}<[]()>>>[{({}<>)<{}()>}])])]<(<{{{<[]()
+<[{<<{<<{<(({[<><>]{{}<>}})){<[{<>[]}({}{})]([{}()]{<>()})>[<{<><>}<<>{}>}([<>[]]<[]()>)]}>(
+(<<(({<<(<(({[<>[]]<[][]>}({<>()}[[]()])){((<>[])){([]{}){[][]}}}){{[[[]{}][{}[]]][<{}[]>([]<>)]}[(({
+<(<(<<<[<{({([<>()])[[[][]]{<>[]}]}[<[()]<{}<>>><[(){}]<()()>>])((<(()[])<{}{})>)((([][]))))}>]<[<([[<<>()
+{{<[<<<{((([[([]{})][[<>[]]]])(<[[(){}]<<>[]>]>{[({}())([]<>)][<<>{}><[]()>]}))){{<[[[[][]
+{([<{[<<[{({({<>{}}[[][]])[{(){}}{{}()}]}[[[{}()]<()()>]])({<[{}{}]>({{}{}}[[]{}])}{{({}())<()()>}{{<><>}<[]
+[<{[{<{{<{<[{{{}{}}[[]()]}([{}[]]<{}()>)](<{()()}{<>[]}><(<>[])>)>}{{{((()<>)[<><>])[[()()]
+([{[<[({([<<<([][])<{}{}>>[[()<>](<>{})]>{{({}<>)}([<><>]<()<>>)}>](<(<<[]<>>({}[])>[<<>{}>[()()]])(<<()[]>((
+(<[{<({<([<({<<>()><{}()>}(<[]{}>({}<>)))[([[]()][<><>>){[<>[]]{{}<>}}]>({[{[]{}}({}[])]<{[
+<<[<(<({<(<({<()><[]()>})>{[{[[]()]<{}>}<(()<>)(<>())>)(((()())[<><>])([(){}]))})([(<{<>()}([]())>{[{}{
+[{<{<{<[<((({[()[]]{{}}}[[<><>]<<>{}>])<({[][]}{()[]})<([]())([]{})>>))((<([{}<>]{(){}})>[
+[[[[([<{[[(<{{{}[]}[<>()]}<{[]}{<><>}>><{({}<>){()<>}}[{[]{}}{<>[]}]>)[([({}<>}([]<>)]{{{}{}
+<{{[[<{[(({({<{}>[(){}]}(<[]()>))}<<<[{}{}]>(<[]{}>(<>[]))>([{{}<>}(()())]<{<><>}{<>()}>)>)<[<<
+{[{{{(<<([([((()[])[[]<>])({{}{}}({}<>))]<({{}[]}{[][]})<{[][]}>>){<<({})[[]<>]>(<[][]>(<>{}))>}]<[
+<[<(<{<[([[[{[()[]][<>{}]}[{(){}}{<><>}]]{<([]{}){()<>}>[({}<>)<<>[]>]}][<[({}[]){()[]}][<<>{}><()<>>]>]]<(
+([<{(([[<<{([([]{})([]())]<{(){}}{[]<>}>)([<<>{}>[()()]]{[[]{}]<[]<>>})}[({{()()}<<>{}>>){[<()
+([({{{[[[([{<{<><>}{()()}>(<[]())[<>])}{([<><>]<(){}>){(<>())[{}()]}}]<{[[<><>]{{}[]}]([{}<>]{[]()
+[[<<<{{({[{[({{}{}}<(){}>)<[[]{}]({}<>)>]}][(<{<<>{}>}{{[]{}}{()[]}}>[{[<>{}]<[][]>}<{<><>
+[([{(([{(([{[{[]()}[{}]][[[]<>]{<>{}}]}{[<{}{}>({}[])][<<><>>(()<>)]}]{<({[]{}}{<>{}}}>}){
+(<<<<<[{<{({({[]{}}({}())){([][])[{}{}]}}([{<><>}{{}{}}][[{}[]]({}{})]))[[[{{}()}[<>{}]][{{}<>}{()}]
+(([{<(<[(<{{{[[]<>](<>[])}{[()<>]<()()>}}}>)<{{({<{}<>>[{}{}]}({<>[]}[()()]))}([([<>[]][[]{}])(<<><>
+((<{[(([{{([<[<>()]<<>[]>>{((){})([])}][<{[]()}<<>()>>(<{}()>(<>))])}[{{{{[]()}}{(<>[])[()<
+<[[{{[<<({[<<{(){}}{(){}}>>(<{[]<>}<<>()>>({()<>}{{}<>}))]{[([[][]][{}<>])][((<>{})(()[]))[<()[]>{<>()}]]}
+{<{<<[{[([{{{{{}()}<()<>>}<[()[]]>}<[<[]<>><()<>>](({}())<{}>)>}<<({[]()}(()()))({{}<>}{{}<>
+{{[<{[<{[[{{({[]<>}<{}[]>)((<>{})({}{}))}([[[]{}]<{}[]>]<({}()){()<>}>)}[<([{}<>}<<><>>)><{[()[]][<>[]]}{
+([{{{[{(<<<[<{<>[]}{()[]}>(([]())<<>{}>)]>[[(<{}()><[]<>>)][{[[][]]<(){}>}<<()()>({}[])>)]>>)<<[[(<(<>())((
+<({[{{(({(<<<{[]()}(<>{})>{(()<>)[[]<>]}><[[<>()]]{([]{})[[]()]}>>)})({[{<(<[]<>>{{}[]])[<<>{}>[(){}]]><(({}(
+((<(([{([{<[{<[]()>(()[])}<({})[<>[]]>]>}]{[<<{[[]()]<<>()>)<<{}[]>{<><>}>>>[[([(){}](<>[]))]{{<
+[([<<([(<[<<([[]{}]<(){}>)([()[]][{}{}])>([([]<>)<(){}>]([[][]]{[]<>}))>{{[{[]<>}[{}[]]]{([]
+[<{<[[<{<({<[<()<>><[]<>>][<{}<>><<>()>]><{((){})(()())}>}{[([{}<>]({}{})){(<>{})}]([[<><>](<>)])})><{<{
+[(<[[<({{<<([<(){}><<><>}]<<{}()>({}<>)>){[(<>())[(){}]]<<<><>>[{}<>]>}><{[{(){}}][<{}{}>[()(
+<[<{[{{<([{<[{()<>}{()<>}]]{<[{}<>](<>{})>([{}<>]{{}()})}}<[([(){}](()[]))<{{}{}}>]{{{<>}<[
+(<{[[{[[{[[(<[[][]][<>{}]>(<()<>>(<>{})))[{([]{}}<{}[]>}[[[]{}]{()()}]]]{<[<<>{}>(()<>)]<{{}<>}<[
+(<<<[[(<{<({{(<>())<(){}>}<(()())<()[]>>}<[({}{})[()[]]]>)>}{<{[<[()<>][<>[]]>[([]{})(()())]][({{}{}}
+({({[[[(<<[{<<<>[]][{}()]>(({}())([]()))}]{<{[()[]]({}())}>({<()[]><[]{}>}[{<>()}<[]{}>])}>>)]]]}[({[[{({(((
+((([[<{[(<({<{<><>}[[]]>[{(){}}>}((<()<>>{()()})<<<>()>({}())>))((([<>()]{[][]})[[(){}]<{}
+<[{([(<[({[[([{}()]({}{}))([<><>]({}()))](({(){}}{()[]}><[[]()]<<><>>>)](<([(){}][{}{}])[{
+{<((({<((<[[<{()[]}<{}{}>>](<<[]>{<>}><[[]{}]({})>)]<<{[{}{}]}[{<>{}}}>>><(([<<>>(()())]<(
+{[[[<<(<{(<(<[{}<>]<[]()>>[{{}{}}{()<>}])[<{{}()}{{}{}}><{[]<>}({}())>]>)<<(<<()<>><<><>>>)>>}[(
+[<{<[([({([[[{{}()}{{}()}]([[]()])]])}<<<[<<<>{}>[<><>)><{[]<>}(()<>)>](({{}<>}[()<>]){{<>[]}((){})})>({(
+([[{{[[[<(<({(()())<{}[]>}<({}<>){[][]}>)[<{()()}[[]<>]>]>)([[({<>[]}[()()])[{[]<>}[{}{}]]][[<
+({[(<<<{((<{<[()()][[]<>]>{<{}<>><()()>}}<{[()[]]([]<>)}<<{}()><{}{}>>>>[<<{(){}}[()[]]>[([]<
+([({{{<[{[{{[([]{})<[]{}>][<<>[]>[{}[]]]}}[{[<<><>>{[][]}]((<>()){<>{}})}]]]][[(<({[[]()][[]{}]
+(((<{{<<<([[{(<><>)<{}>}<{{}{}}[{}<>]>]{[(()())[{}<>}]}]{<(<[]()>)>([<()[]>{{}()}]<({}()){(){}}>)})<[({(<>()
+[{(([[<(<<(<{<[]()>(<>{})}[{()[]}[()()]]><[<[]()>[[]<>]]>)<{<[[]<>]<()<>>>{[()[]]<{}[]>}}>>([((<[][
+{<{<{[([(<<[{<()<>>(<>[])}[(<><>)[[]<>]]]<<<[]<>><(){}>>{{{}<>}<<><>>}>>(<{{<>()}[()<>]}><[
diff --git a/day10/main.c b/day10/main.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+
+void puzzle(const char *filename, long long *res1, long long *res2);
+void puzzle_test(const char *filename, long long *res1, long long *res2);
+
+int main(int argc, char *argv[]) {
+ printf("Advent of Code: day 10\n");
+ double time_start = clock();
+
+ if (argc <= 1) {
+ printf("Usage: %s inputfile.txt\n", argv[0]);
+ return -1;
+ }
+
+ const char *filename = argv[1];
+ long long counter1 = -1;
+ long long counter2 = -1;
+
+ puzzle(filename, &counter1, &counter2);
+
+ printf("Puzzle #1: %lld\n", counter1);
+ printf("Puzzle #2: %lld\n", counter2);
+
+ double elapsed = clock() - time_start;
+ printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC);
+
+ return 0;
+}
diff --git a/day10/puzzle.c b/day10/puzzle.c
@@ -0,0 +1,118 @@
+#define _DEFAULT_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <strings.h>
+#include <stdbool.h>
+#include <assert.h>
+#include <time.h>
+#include <inttypes.h>
+#include <ctype.h>
+
+#include "util.h"
+
+#define STR_LEN 1024
+
+// simple stack operations on array_t
+void array_push(struct array_t *array, char bracket);
+char array_pop(struct array_t *array);
+
+char validate_string(const char *str);
+
+void puzzle(const char *filename, long long *result1, long long *result2) {
+ FILE *infile = fopen(filename, "r");
+ if (infile == NULL) {
+ fprintf(stderr, "fopen() error: %s\n", strerror(errno));
+ return;
+ }
+
+ char buf[STR_LEN] = {0};
+ unsigned int line_num = 0;
+
+ *result1 = 0;
+ *result2 = 0;
+
+ while (fgets(buf, STR_LEN, infile) != NULL) {
+ char illegal = validate_string(buf);
+ if (illegal != 0) {
+ switch (illegal) {
+ case ')':
+ *result1 += 3;
+ break;
+ case ']':
+ *result1 += 57;
+ break;
+ case '}':
+ *result1 += 1197;
+ break;
+ case '>':
+ *result1 += 25137;
+ break;
+ }
+ }
+
+ ++line_num;
+ bzero(buf, STR_LEN);
+ }
+
+ // mutiny! ignoring feof/ferror.
+ fclose(infile);
+}
+
+char validate_string(const char *str) {
+ size_t len = strlen(str);
+ assert(len > 2); // at least 2 characters + EOL
+ struct array_t brackets = { .data = NULL };
+ array_init(&brackets, sizeof(char), 10);
+
+ for (size_t i = 0; i < len; ++i) {
+ char c = str[i];
+ switch (c) {
+ case '{':
+ array_push(&brackets, '}');
+ break;
+ case '(':
+ array_push(&brackets, ')');
+ break;
+ case '<':
+ array_push(&brackets, '>');
+ break;
+ case '[':
+ array_push(&brackets, ']');
+ break;
+ case '}':
+ case ')':
+ case '>':
+ case ']': {
+ char expected = array_pop(&brackets);
+ if (c != expected) {
+ return c;
+ }
+ break;
+ }
+ case '\n':
+ // do nothing
+ break;
+ default:
+ printf("invalid symbol: %c (#%d)\n", c, c);
+ }
+ }
+
+ free(brackets.data);
+ return 0;
+}
+
+void array_push(struct array_t *array, char bracket) {
+ if (array->count >= array->cap) {
+ array_expand(array);
+ }
+ char *data = (char *)array->data;
+ data[array->count++] = bracket;
+}
+
+char array_pop(struct array_t *array) {
+ char *data = (char *)array->data;
+ return data[--array->count];
+}
diff --git a/day10/test.txt b/day10/test.txt
@@ -0,0 +1,10 @@
+[({(<(())[]>[[{[]{<()<>>
+[(()[<>])]({[<{<<[]>>(
+{([(<{}[<>[]}>{[]{[(<()>
+(((({<>}<{<{<>}{[]{[]{}
+[[<[([]))<([[{}[[()]]]
+[{[{({}]{}}([{[{{{}}([]
+{<[[]]>}<{[{[{[]{()[[[]
+[<(<(<(<{}))><([]([]()
+<{([([[(<>()){}]>(<<{{
+<{([{{}}[<[[[<>{}]]]>[]]