diff --git a/day_9/Makefile b/day_9/Makefile new file mode 100644 index 0000000..d8b1e31 --- /dev/null +++ b/day_9/Makefile @@ -0,0 +1,3 @@ +TARGET := day_9 + +include ../Makefile.common.mk diff --git a/day_9/input.txt b/day_9/input.txt new file mode 100644 index 0000000..db7d58e --- /dev/null +++ b/day_9/input.txt @@ -0,0 +1,2000 @@ +D 2 +U 2 +L 2 +R 1 +L 1 +U 1 +L 2 +D 1 +L 2 +D 1 +L 2 +D 2 +L 1 +R 2 +U 1 +R 1 +U 2 +R 1 +U 2 +D 1 +L 2 +U 1 +D 1 +L 1 +D 1 +U 1 +R 2 +D 2 +L 1 +R 1 +L 2 +D 1 +R 2 +D 1 +U 2 +D 2 +R 2 +L 2 +U 2 +R 2 +L 1 +D 1 +R 1 +U 1 +R 2 +L 1 +D 1 +R 1 +L 2 +D 2 +U 1 +L 1 +R 1 +U 1 +L 2 +D 2 +L 2 +U 1 +L 2 +R 1 +L 1 +R 1 +D 1 +R 2 +D 1 +R 1 +D 1 +U 1 +L 1 +R 1 +L 1 +R 1 +D 2 +L 1 +D 2 +R 1 +D 1 +U 2 +R 2 +U 2 +L 1 +U 1 +R 1 +L 2 +D 2 +R 1 +U 1 +L 1 +U 2 +L 1 +D 2 +R 1 +U 1 +R 1 +U 2 +R 1 +U 2 +L 1 +R 1 +U 2 +R 2 +U 1 +L 2 +R 1 +L 2 +U 2 +L 2 +U 2 +R 2 +D 1 +U 2 +L 2 +D 3 +U 1 +L 1 +R 1 +D 3 +U 1 +L 3 +R 1 +U 1 +R 3 +D 2 +U 1 +R 2 +L 2 +U 1 +D 1 +U 1 +D 1 +L 2 +U 2 +R 3 +D 2 +L 2 +D 2 +U 3 +D 3 +U 2 +L 1 +U 2 +R 3 +D 2 +R 2 +D 1 +U 3 +L 3 +D 1 +U 3 +R 3 +D 1 +L 1 +D 3 +U 3 +D 1 +U 2 +R 1 +D 2 +U 3 +D 1 +R 2 +D 3 +L 3 +R 1 +D 3 +L 2 +D 3 +U 3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L 7 +U 15 +L 3 +D 15 +R 3 +U 6 +L 17 +D 17 +L 12 +D 16 +R 14 +L 1 +R 7 +D 15 +U 2 +L 5 +U 16 +D 7 +L 11 +U 1 +R 2 +D 4 +L 13 +U 17 +R 5 +D 17 +R 14 +L 1 +D 11 +U 11 +R 8 +U 12 +D 12 +U 17 +R 15 +L 4 +D 7 +U 8 +R 4 +L 14 +U 11 +L 3 +R 1 +L 14 +U 3 +L 3 +D 15 +L 14 +D 7 +R 2 +L 6 +U 6 +R 12 +U 17 +D 1 +R 15 +D 1 +U 14 +D 6 +U 4 +R 4 +L 3 +U 14 +L 1 +R 4 +U 13 +D 18 +L 11 +U 4 +D 9 +R 10 +U 17 +R 8 +D 12 +R 9 +L 5 +D 6 +L 9 +R 9 +L 17 +D 9 +R 2 +L 4 +R 8 +D 8 +R 8 +D 13 +R 12 +L 13 +U 8 +D 8 +L 16 +D 15 +U 11 +L 8 +R 7 +D 2 +R 6 +U 19 +D 4 +U 7 +L 17 +R 9 +U 18 +D 4 +R 19 +L 14 +U 8 +D 19 +L 12 +R 14 +U 13 +D 3 +L 8 +D 4 +R 9 +U 18 +D 1 +U 2 +L 16 +U 2 +D 12 +U 5 +L 4 +D 6 +U 14 +R 1 +L 7 +D 7 +L 19 +D 5 +U 16 +D 15 +R 14 +U 19 +D 19 +L 6 +R 3 +L 14 +R 16 +L 1 +U 4 +R 19 +U 2 +L 3 +D 19 +L 17 +U 13 +D 5 +L 10 +U 18 +R 13 +D 6 +L 17 +D 9 +R 10 +D 8 +R 19 +L 6 +D 7 +R 18 +U 8 +L 6 +U 18 +L 18 +D 16 +R 8 +L 10 +R 6 +D 1 +L 6 +U 8 +L 19 +D 18 +U 3 +D 17 +U 9 +R 13 +D 18 +R 2 +L 10 +D 17 +L 17 +U 8 +L 13 +D 5 +L 1 +U 10 +L 3 +R 3 +D 12 +U 11 +L 15 +D 15 diff --git a/day_9/lexer.l b/day_9/lexer.l new file mode 100644 index 0000000..646f699 --- /dev/null +++ b/day_9/lexer.l @@ -0,0 +1,56 @@ +%{ +#include +#include +#include +#include "parser.h" + +%} + +%option warn nodefault + +/* makes the scanner terminate after reaching <> instead of assuming a new input was provided */ +%option noyywrap +/* disable some unused functionality, add scanner tracking */ +%option nounput noinput batch debug + +/* gimme a reentrant parser (overkill but more pure) */ +%option reentrant + +%option bison-bridge + + +NL [\n] +SPACE " " + +DOWN "D" +UP "U" +LEFT "L" +RIGHT "R" + +/*general definitions*/ +NUM [0-9] + +%% + +{NL} { return NEWLINE; } +{SPACE} { /* return SPACE; */ } +{DOWN} { return DOWN; } +{UP} { return UP; } +{LEFT} { return LEFT; } +{RIGHT} { return RIGHT; } + +{NUM}+ { + unsigned long num = strtoul(yytext, NULL, 10); + yylval->num = num; + return NUM; + } + +<> { + return END_OF_FILE; + } +. { + printf("[error] Encountered unexpected token %s\n", yytext); + return 0; + } + +%% diff --git a/day_9/parser.y b/day_9/parser.y new file mode 100644 index 0000000..b12a5d2 --- /dev/null +++ b/day_9/parser.y @@ -0,0 +1,172 @@ +/* Require bison minimal version */ +%require "3.2" + +/* write out a header file containing the token defines */ +%header + +// Code for the header file generated by bison +%code requires { + #include + + #define MAX_X 500 + #define MAX_Y 500 + + typedef void* yyscan_t; + + struct parser_state { + bool field[MAX_X][MAX_Y]; + + size_t head_x; + size_t head_y; + size_t tail_x; + size_t tail_y; + + unsigned long visited; + }; +} + +// Code for the c file +%{ + #include + #include + #include "parser.h" + #include "lexer.h" + + void yyerror(struct parser_state* state, yyscan_t scanner, const char* msg) { + (void)state; + (void)scanner; + fprintf(stderr, "\033[93mSyntax Error: %s\033[0m\n", msg); + } +%} + +// define a reentrant parser +%define api.pure + +// enable parser tracing and detailed error messages (plus Lookahead Correction) +%define parse.trace +%define parse.error detailed +%define parse.lac full + +%lex-param { yyscan_t scanner } +%parse-param { struct parser_state* state } +%parse-param { yyscan_t scanner } + +%union { + unsigned long num; +} + +%start input +%token NEWLINE +%token DOWN UP LEFT RIGHT +%token NUM +%term END_OF_FILE + +%% + +input + : line input + | END_OF_FILE { return 0; } + ; + +line + : movement NEWLINE + | NEWLINE + ; + +movement + : UP NUM { + for (size_t i = 0; i < $2; i++) { + state->head_y++; + if ((state->head_y - state->tail_y) > 1) { + state->tail_y = state->head_y - 1; + state->tail_x = state->head_x; + } + + if (!state->field[state->tail_x][state->tail_y]) { + state->field[state->tail_x][state->tail_y] = true; + state->visited++; + } + } + } + | DOWN NUM { + for (size_t i = 0; i < $2; i++) { + state->head_y--; + if ((state->tail_y - state->head_y) > 1) { + state->tail_y = state->head_y + 1; + state->tail_x = state->head_x; + } + + if (!state->field[state->tail_x][state->tail_y]) { + state->field[state->tail_x][state->tail_y] = true; + state->visited++; + } + } + } + | RIGHT NUM { + for (size_t i = 0; i < $2; i++) { + state->head_x++; + if ((state->head_x - state->tail_x) > 1) { + state->tail_x = state->head_x - 1; + state->tail_y = state->head_y; + } + + if (!state->field[state->tail_x][state->tail_y]) { + state->field[state->tail_x][state->tail_y] = true; + state->visited++; + } + } + } + | LEFT NUM { + for (size_t i = 0; i < $2; i++) { + state->head_x--; + if ((state->tail_x - state->head_x) > 1) { + state->tail_x = state->head_x + 1; + state->tail_y = state->head_y; + } + + if (!state->field[state->tail_x][state->tail_y]) { + state->field[state->tail_x][state->tail_y] = true; + state->visited++; + } + } + } + ; + + +%% + + +int main(void) { + struct parser_state *state = calloc(1, sizeof(struct parser_state)); + if (!state) { + fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n"); + return EXIT_FAILURE; + } + + state->head_x = MAX_X / 2; + state->head_y = MAX_Y / 2; + state->tail_x = MAX_X / 2; + state->tail_y = MAX_Y / 2; + + state->visited = 1; // the starting position + state->field[state->tail_x][state->tail_y] = true; + + yyscan_t scanner; + if (yylex_init(&scanner)) { + fprintf(stderr, "\033[91m[error] Could not initialize lexer\033[0m\n"); + return EXIT_FAILURE; + } + + if (yyparse(state, scanner)) { + // TODO: Error handling https://www.gnu.org/software/bison/manual/html_node/Parser-Function.html + // error during parse occured + return EXIT_FAILURE; + } + + yylex_destroy(scanner); + + // task 1 + printf("Visited nodes: %zu\n", state->visited); + + return 0; +}