Compare commits

...

10 commits

30 changed files with 4757 additions and 277 deletions

54
Makefile.common.mk Normal file
View file

@ -0,0 +1,54 @@
CC := clang
YACC := bison
LEX := flex
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
BISONFLAGS :=
BUILD := ./build
INCLUDE := -I./
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/bin
SRC := $(wildcard ./*.c) lexer.c parser.c
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
# targets
all: build $(APP_DIR)/$(TARGET)
run: all
$(APP_DIR)/$(TARGET)
flex:
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
bison:
$(YACC) $(BISONFLAGS) --output=parser.c parser.y
generate: flex bison
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
.PHONY: all build clean debug release
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CFLAGS += -DDEBUG -g
debug: BISONFLAGS += -Wcounterexamples
debug: all
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
sanitize: all
release: CFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}

View file

@ -1,54 +1,3 @@
CC := clang
YACC := bison
LEX := flex
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
BUILD := ./build
INCLUDE := -I./
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/bin
TARGET := day_1 TARGET := day_1
SRC := $(wildcard ./*.cpp) lexer.c parser.c include ../Makefile.common.mk
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
# targets
all: build $(APP_DIR)/$(TARGET)
run: all
$(APP_DIR)/$(TARGET)
flex:
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
bison:
$(YACC) --output=parser.c parser.y
generate: flex bison
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
.PHONY: all build clean debug release
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CFLAGS += -DDEBUG -g
debug: all
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
sanitize: all
release: CFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}

3
day_10/Makefile Normal file
View file

@ -0,0 +1,3 @@
TARGET := day_10
include ../Makefile.common.mk

141
day_10/input.txt Normal file
View file

@ -0,0 +1,141 @@
addx 1
noop
addx 5
addx -1
addx 5
addx 1
noop
noop
addx 2
addx 5
addx 2
addx 1
noop
addx -21
addx 26
addx -6
addx 8
noop
noop
addx 7
noop
noop
noop
addx -37
addx 13
addx -6
addx -2
addx 5
addx 25
addx 2
addx -24
addx 2
addx 5
addx 5
noop
noop
addx -2
addx 2
addx 5
addx 2
addx 7
addx -2
noop
addx -8
addx 9
addx -36
noop
noop
addx 5
addx 6
noop
addx 25
addx -24
addx 3
addx -2
noop
addx 3
addx 6
noop
addx 9
addx -8
addx 5
addx 2
addx -7
noop
addx 12
addx -10
addx 11
addx -38
addx 22
addx -15
addx -3
noop
addx 32
addx -25
addx -7
addx 11
addx 5
addx 10
addx -9
addx 17
addx -12
addx 2
noop
addx 2
addx -15
addx 22
noop
noop
noop
addx -35
addx 7
addx 21
addx -25
noop
addx 3
addx 2
noop
addx 7
noop
addx 3
noop
addx 2
addx 9
addx -4
addx -2
addx 5
addx 2
addx -2
noop
addx 7
addx 2
addx -39
addx 2
noop
addx 1
noop
addx 5
addx 24
addx -20
addx 1
addx 5
noop
noop
addx 4
noop
addx 1
noop
addx 4
addx 3
noop
addx 2
noop
noop
addx 1
addx 2
noop
addx 3
noop
noop

51
day_10/lexer.l Normal file
View file

@ -0,0 +1,51 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
%}
%option warn nodefault
/* makes the scanner terminate after reaching <<EOF>> 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 " "
NOP "noop"
ADD "addx"
/*general definitions*/
NUM [0-9\-]
%%
{NL} { return NEWLINE; }
{SPACE} { /* return SPACE; */ }
{NOP} { return NOP; }
{ADD} { return ADDX; }
{NUM}+ {
int num = atoi(yytext);
yylval->num = num;
return NUM;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%

132
day_10/parser.y Normal file
View file

@ -0,0 +1,132 @@
/* 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 <stdbool.h>
typedef void* yyscan_t;
struct parser_state {
/// Program Counter
size_t cycle;
/// Contents of register X
long reg;
/// Task 1
long sum;
size_t next_target_cycle;
};
}
// Code for the c file
%{
#include <stdio.h>
#include <stdlib.h>
#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);
}
void increment_cycle(struct parser_state *state) {
state->cycle++;
if (state->cycle == state->next_target_cycle) {
long tmp = state->cycle * state->reg;
printf("[info] Register contents at cycle %zu: %ld\n", state->cycle, state->reg);
state->sum += tmp;
if (state->next_target_cycle < 220) {
state->next_target_cycle += 40;
}
}
}
%}
// 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 {
int num;
}
%start input
%token NEWLINE
%token NOP ADDX
%token <num> NUM
%term END_OF_FILE
%%
input
: line input
| END_OF_FILE { return 0; }
;
line
: instruction NEWLINE
| NEWLINE
;
instruction
: NOP { increment_cycle(state); }
| ADDX NUM {
increment_cycle(state);
increment_cycle(state);
state->reg += $2;
}
;
%%
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->reg = 1;
state->next_target_cycle = 20;
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("Sum of signal strengths: %ld\n", state->sum);
free(state);
return 0;
}

View file

@ -1,54 +1,3 @@
CC := clang TARGET := day_2_1
YACC := bison
LEX := flex
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
BUILD := ./build
INCLUDE := -I./
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/bin
TARGET := day_2
SRC := $(wildcard ./*.cpp) lexer.c parser.c include ../Makefile.common.mk
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
# targets
all: build $(APP_DIR)/$(TARGET)
run: all
$(APP_DIR)/$(TARGET)
flex:
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
bison:
$(YACC) --output=parser.c parser.y
generate: flex bison
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
.PHONY: all build clean debug release
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CFLAGS += -DDEBUG -g
debug: all
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
sanitize: all
release: CFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}

View file

@ -1,54 +1,3 @@
CC := clang TARGET := day_2_2
YACC := bison
LEX := flex
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
BUILD := ./build
INCLUDE := -I./
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/bin
TARGET := day_2
SRC := $(wildcard ./*.cpp) lexer.c parser.c include ../Makefile.common.mk
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
# targets
all: build $(APP_DIR)/$(TARGET)
run: all
$(APP_DIR)/$(TARGET)
flex:
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
bison:
$(YACC) --output=parser.c parser.y
generate: flex bison
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
.PHONY: all build clean debug release
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CFLAGS += -DDEBUG -g
debug: all
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
sanitize: all
release: CFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}

View file

@ -1,54 +1,3 @@
CC := clang
YACC := bison
LEX := flex
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
BUILD := ./build
INCLUDE := -I./
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/bin
TARGET := day_4 TARGET := day_4
SRC := $(wildcard ./*.cpp) lexer.c parser.c include ../Makefile.common.mk
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
# targets
all: build $(APP_DIR)/$(TARGET)
run: all
$(APP_DIR)/$(TARGET)
flex:
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
bison:
$(YACC) --output=parser.c parser.y
generate: flex bison
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
.PHONY: all build clean debug release
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CFLAGS += -DDEBUG -g
debug: all
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
sanitize: all
release: CFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}

View file

@ -1,56 +1,3 @@
CC := clang
YACC := bison
LEX := flex
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
BISONFLAGS :=
BUILD := ./build
INCLUDE := -I./
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/bin
TARGET := day_5 TARGET := day_5
SRC := $(wildcard ./*.cpp) lexer.c parser.c include ../Makefile.common.mk
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
# targets
all: build $(APP_DIR)/$(TARGET)
run: all
$(APP_DIR)/$(TARGET)
flex:
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
bison:
$(YACC) $(BISONFLAGS) --output=parser.c parser.y
generate: flex bison
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
.PHONY: all build clean debug release
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CFLAGS += -DDEBUG -g
debug: BISONFLAGS += -Wcounterexamples
debug: all
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
sanitize: all
release: CFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}

View file

@ -6,6 +6,8 @@
// Code for the header file generated by bison // Code for the header file generated by bison
%code requires { %code requires {
#include <stdbool.h>
typedef void* yyscan_t; typedef void* yyscan_t;
#define NUM_PILES 9 #define NUM_PILES 9
@ -20,6 +22,8 @@
// points to the topmost item in the stack // points to the topmost item in the stack
struct stack_item** stacks; struct stack_item** stacks;
unsigned stack_idx; unsigned stack_idx;
// whether the crane is generation two (task 2)
bool is_gen_two;
}; };
} }
@ -27,6 +31,7 @@
%{ %{
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include "parser.h" #include "parser.h"
#include "lexer.h" #include "lexer.h"
@ -115,32 +120,58 @@ expr
: MOVE_OP NUMBER ORIGIN NUMBER DEST NUMBER { : MOVE_OP NUMBER ORIGIN NUMBER DEST NUMBER {
// Source: $4, Dst: $6, repeat $2 times // Source: $4, Dst: $6, repeat $2 times
// printf("move %lu from %lu to %lu", $2, $4, $6); // printf("move %lu from %lu to %lu", $2, $4, $6);
for (unsigned long i = 0; i < $2; i++) {
// get the current element
struct stack_item* it = state->stacks[$4 -1];
if (!it) { if (state->is_gen_two) {
// TODO(feliix42): use yyerror? struct stack_item* it = state->stacks[$4 - 1];
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m"); for (unsigned long i = 1; i < $2; i++) {
break; if (!it) {
// TODO(feliix42): use yyerror?
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m");
break;
}
it = it->next;
} }
// delete old references struct stack_item* tmp = state->stacks[$4 - 1];
state->stacks[$4 -1] = it->next; state->stacks[$4 - 1] = it->next;
// insert on new stack it->next = state->stacks[$6 - 1];
it->next = state->stacks[$6 -1]; state->stacks[$6 - 1] = tmp;
state->stacks[$6 -1] = it; } else {
for (unsigned long i = 0; i < $2; i++) {
// get the current element
struct stack_item* it = state->stacks[$4 -1];
if (!it) {
// TODO(feliix42): use yyerror?
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m");
break;
}
// delete old references
state->stacks[$4 -1] = it->next;
// insert on new stack
it->next = state->stacks[$6 -1];
state->stacks[$6 -1] = it;
}
} }
} }
; ;
%% %%
int main(void) { int main(int argc, char** argv) {
struct parser_state parser_state; struct parser_state parser_state;
parser_state.stack_idx = 0; parser_state.stack_idx = 0;
if (argc > 1 && argv[1][0] == '2') {
parser_state.is_gen_two = true;
} else {
parser_state.is_gen_two = false;
}
parser_state.stacks = calloc(NUM_PILES, sizeof(struct stack_item*)); parser_state.stacks = calloc(NUM_PILES, sizeof(struct stack_item*));
if (!parser_state.stacks) { if (!parser_state.stacks) {
fprintf(stderr, "\033[91m[error] Could not initialize parser state\033[0m\n"); fprintf(stderr, "\033[91m[error] Could not initialize parser state\033[0m\n");

3
day_6/Makefile Normal file
View file

@ -0,0 +1,3 @@
TARGET := day_6
include ../Makefile.common.mk

2
day_6/input.txt Normal file
View file

@ -0,0 +1,2 @@
hrbbjllllspssblslvvrdrbbpbbmcccfppvbbwvbbmrmjrjrfrgfgbffgfqfqlltlwttscsncscchssrppffvwwvvpnnwwwpvwvhhnvhhbttvzzdlzdlzzwmmjhhznnjdnnnqddbtdbdbsdsmdsdrrdpdwpdppgcgqgcctftsfszslljbljbjwbwbnwnqqrnnztntmtrmmzwzdwzwgwwwjhjsjgjtjjhpjhhppqzqdqffrvrtvvsmmgwmgwgbbclltctptzpzhpzptzppcfpcfftflfzftztddzgzmmfsmsrmmsstttvbvmbvvsmsqmqlldjdtthwtwbwggrzzjrzrcctffsshqshhpthhlnhlhqhdqqrwrmmcttpfttzfzgzdgdzzwrrtsrrsnrnccrbbsssbpbjjvzzwlwtwjwsjwjggzqgzzrsszzjnzjzwjzzcrzczncnqqztzfzhfhvvtjvjdvjjmrjrppvzppczpczcggshghvhnhhsrsnsdszzdpzpzlpzzhwwmnwmwmcwwfnwfwjjcbcncllcsllqdqzzhqhmqmbbjvjjwwcjjpnpllzfzddtmtccqrcrtrwrpphpmpplslmltthnnvhhvrvbblhhrrdqqmbqqgtqggdgcdcvvsbsswvvpggbbtftlflglzlmlbbfhhrshswhshffhhdnnrfrvrmrnrprrmfmpmnpnfnggvvcncdcrczrzccpmmssrbbdjdtdrdwrrwhrrvrtvvszvvwvzzmhhjhhwlhlqlvlttzftztdtstftrfrdddmtmzzsqzqvvpdpdcpcncnrrtntznzrzgznztnznhhsqqnrqrhhlzhzthhfddrzdrrqmqggcmmllnjjvwwjccfjfqfcfzccwvcwvcvjcjtjtnnqsqmqrmrzrszrszzfwfggnmmcdmdjmmhwwgfwfnwnlwwcffsrffvnvbvnvwnwgnwnmmbzbmbpplmplpspzpmzpzdzgzrzrtrjrbbppwvwgwmggqwgghqqshhcwcqwcqcfcbbnsnrrtztzrtrvtvhthzhmmrqqrwqqsjqjcctgtwthhqmmnffmgmdgdlgljjhwjwggrqqfrqqjvqvhqqgsqsgsttmrrbprbppmjppslpsllvlfvlvrvhvtqmrjcdzwsbzfmgmwmwqwhztqrsdzhqjqvbjbntnbndflthljcczdmmhszfgsplrtlqnfzbrlqngwdqtfwcmrdjrsmdpmjmqwrbwfjzwnvqhfmlqtvvnlfzbfccwslqpbzzjccbvrzhghqwtvqgwrmsfzqnmnqqjsjtpcmngpqgllfsnpqtjjbqcdppnsmtwrslnrbqtwvnbctzvwfmgctscmzjbqqgqdwbpzmrdwgfcjzftzgmfcjhchbnmnqnrgtqngwrmncjvptqqdtjtgtpzzdrfsdgmwlwrjnqldbwrqjrhwcczlzvlhpgrnwzhbwjnpthggczfgtrjnzvnlfdfbwcnzfbwlwlmgnnjnpvhbhqgnzhqsnmvbcftsmrcgpvnnnmgnrvpbzlpwnbwpzmwpgqvbfgjwfrjqnvvgmqwwcfddqmdznmfhpjcfgptqdqwmplrglbwlmsqzjshrlhflcjvptgrcfhjfgqmlfzrtphpbvcqzwpcnwljjdlmqzhcctqshdngrgtlfsrfccdtlvmqcdgnpcvphdsrpzfzwclvsqcpzqlfvvqzggdhpfzdvhshglvfzfmcllrdfjfsjtngjgddcpqnlmrnplwtlvwdvzftltnsnspcdztgqhlhvvbnwvnmhscfnqbngpvprzfrjcmfpfzfftrlnwgllhnjndpjdrwcgqpcgcqngnbfzlvzvhnqdjthflmwvppmbdssddmgsbgrqnpjzrjpzdddqgsdlmwnhhpjbthclvqhgrsnrbqgtnsjhncnzbhrdgftvbptrqssvsqfpqnddhmgwcrfqndqjsqgffmhdvqhjrdlmrlcqctqccprwlbqgqrwmtfhwmfjfqzdqbsdsjbtsvfvgbsrvqwnqqqqthpsqgcfslsqtnjwtsrcdcctggdghrjwpbfccrtwgszwbrsjswmjmjbcqrsgbcfsdjzsbjnnssnddnnvwgftlrqvphnqcgjszscrlhhjnljlqcjqtqfwbmdmrgdlcqqwmbsmsdhpplvlfglqwspbfptlbzqjwhqmfvzvsvpjclcdzsbvntmhdqdvhghcmmflpjbglsghbswdshtsbdrgpsrsclrmfwwqbrgdjsqztgttqpwhnfhszlgbfpzhczsnwqflmshlgbrpmdzgpqwtsbssgfjbtrwbmztlwwfmsdgpgfgdjfdccwlfgztbcbqjvjtvslmddjplrswwcszspgplsrhrnwnmrrfbcgdmntcrlvnfqtwwcczsglrhtrfqnmhvgzjpmlplqvqhmnfgvzqcmzhqszgslvndqtqhvrbvbmclbcbjdswvcjrzgfdmdwnnlzlzqcffsrqdfmmpzfnmdsnqlpcrhzsdnsflblcjsfsgcnsspftjrlmdjsmfpqtmlgfvnlfnjscsgwzwvpjrvvclhsbqldlnmtglhbjfwlzmvrbvgtprfjbjhhnlqnbrswwlqtcgrjrltdrnfrjhrntllptlsbhqrwvdsfrlghtfcndznzjwcgmtdvffltgrdmljlqhdtmdvnfsfsrvdpmhlrrsttvqlwfptddwbpfrbclwwzmfpttmrmmqzjnbbnnfvzwmmcfshvrlbdbjzprftbqvdsghnnzwbjccpthdsvsdlgvphsgjdqjwsgmzqnqpqvgqjvwgjtzpmqqwnlwrwhqqjjclcbhjgpwhqdclwmqfmwbwmwwvcbhfznfhcfbprfcdqlbcttnvgnjwswcmpbrghtzgdbppbprffzjgvddzpwmdctrhnrfzdfhtmnfrsfdqvzcnrtncflhvldcndwqtvbggmwlzhchlcwtcbqcvlfhdwljgddwpvcfczvfqmphgtdsnsqwdpvvmwnwqjbrjwbdhhgtffphsdrvspsbgmfrmwmhnrgqdfppzgfpgmqjcsnglczgwhjthfhztzrlpgzjhcfrjpjvtjptptbvflftjtcfhmbwlhlbhvnjnbfmwjrgbvvhmdlncdgncgfjcnnpdljfcjsmsfscqpwsgcmlhhqmldsnjfrrqpghwncmgwgnjsdtvbhrbbnmpqjrrctqqnqzztmbqmdsgdvmmlwmbvprllzgntnmttrlzrttmjjlrwpwmtfznmwnsjmjhjdnsppfhcrjpzhjqzdtdbsjshfzzvrwvjbjbgtsfpgggbdztczwlhpmthfjdgsbrvlwmlrvgdrpjzccwmgpcnqqzmqdjqmwsrzwsmtmdjdhmjrwfwnzlmfnqtcgtslwtlnwhvmqntmglhntnsjlnmzfvfdztcfwmpchsrsdmqvqcwljzrmmssjvbmvvnmqlbsdwnrbmqctdtmfzlgfzpmjcnftgftvjpfbwwmzfdrrwjwcfwfcfmzbbnppgjrmbcvmvnjpdrzmvndvddtvshlnjjwgtsvnwtwnhcbfpnthpjlrhgrqccdgppjvdqjwqrfrrgnvhfwvjhnwhntnpmghphrtgqhwtbrqhqljfdjbgnlgmqqgfcqpqfhcpgspdbvlbfjvlrgmtjztwdzlrhqwwtcpdvsqgssjbjjgqlwbcctzzqvvmdzpfrmspmqhtzwgcfsslpnhpjfwqrrfbwbndrvhnnsjnlvlvqdsgwzjsrprhgtvsfbhbcpljdczbtdwzcnhzntrwcrjctmhtjfdlthznzmqblppzcqgpjhlzjrmcvpptfjjzltdhmvwphwlccscwrwfcqpqwwrzcmnltzdcfvtjrcvsqwtchrmdfzjmzjfhppjzbhglwqggzqqnspfmzrfwrqdqdrsdbsdhcgdqrrnjlwrqhfhpzjhrvjndqphndnnnbwhrjvqrrbvlhhbljjcwmfpvnhcszfshlsnczgtcfhjslbhzczdqdmdnvqdzhbmbpcnbntwgllfscrcwhfrgtfvftmwhbgfhjzjrbvvwc

41
day_6/lexer.l Normal file
View file

@ -0,0 +1,41 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
%}
%option warn nodefault
/* makes the scanner terminate after reaching <<EOF>> 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]
CHAR [a-z]
%%
{NL} {
return NEWLINE;
}
{CHAR} {
yylval->cval = yytext[0];
return CHAR;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%

187
day_6/parser.y Normal file
View file

@ -0,0 +1,187 @@
/* 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 <stdbool.h>
#define BUFSIZE 14
typedef void* yyscan_t;
struct parser_state {
// for storing the most recently read 14 chars
char buffer[BUFSIZE];
// location tracker
unsigned long loc;
bool found_start_of_packet;
unsigned long packet_start;
bool found_start_of_msg;
unsigned long msg_start;
bool check_matrix[26];
};
}
// Code for the c file
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#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 {
char cval;
}
%start input
%token NEWLINE
%token <cval> CHAR
%term END_OF_FILE
%%
input
: msg blanks END_OF_FILE { return 0; }
;
blanks
: NEWLINE blanks
| NEWLINE
;
msg
: bit msg
| bit
;
bit
: CHAR {
if (!state->found_start_of_packet || !state->found_start_of_msg) {
state->loc++;
// left shift existing data
for (int i = 0; i < (BUFSIZE-1); i++) {
state->buffer[i] = state->buffer[i+1];
}
state->buffer[BUFSIZE-1] = $1;
// check whether condition is met
// offset array indices by the base -- 'a'
char offset = 'a';
// Search for start of packet
bool search_sop = !state->found_start_of_packet; // sop - start of packet
bool search_som = !state->found_start_of_msg; // som - start of message
if (search_sop) {
state->found_start_of_packet = true;
}
if (search_som) {
state->found_start_of_msg = true;
}
// the start of packet only concerns itself with the last 4 characters read; the start of message with the last 14
// hence, we check the last 4 first, then the rest
for (int i = BUFSIZE - 4; i < BUFSIZE; i++) {
if (!state->check_matrix[state->buffer[i] - offset]) {
state->check_matrix[state->buffer[i] - offset] = true;
} else {
if (search_sop) {
state->found_start_of_packet = false;
}
if (search_som) {
state->found_start_of_msg = false;
}
break;
}
}
// now check the remaining elements
for (int i = 0; i < BUFSIZE - 4; i++) {
if (!state->check_matrix[state->buffer[i] - offset]) {
state->check_matrix[state->buffer[i] - offset] = true;
} else {
if (search_som) {
state->found_start_of_msg = false;
}
break;
}
}
if (search_sop && state->found_start_of_packet) {
state->packet_start = state->loc;
}
if (search_som && state->found_start_of_msg) {
state->msg_start = state->loc;
}
// housekeeping; reset the fields to false
for (int i = 0; i < BUFSIZE; i++) {
state->check_matrix[state->buffer[i] - offset] = false;
}
}
}
;
%%
int main(void) {
struct parser_state parser_state;
for (int i = 0; i < BUFSIZE; i++) {
parser_state.buffer[i] = 'a';
}
parser_state.loc = 0;
parser_state.packet_start = 0;
parser_state.msg_start = 0;
parser_state.found_start_of_packet = false;
parser_state.found_start_of_msg = false;
yyscan_t scanner;
if (yylex_init(&scanner)) {
fprintf(stderr, "\033[91m[error] Could not initialize lexer\033[0m\n");
return EXIT_FAILURE;
}
if (yyparse(&parser_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);
printf("First start-of-packet marker detected at: %lu\n", parser_state.packet_start);
printf("First start-of-message marker detected at: %lu\n", parser_state.msg_start);
return 0;
}

3
day_7/Makefile Normal file
View file

@ -0,0 +1,3 @@
TARGET := day_7
include ../Makefile.common.mk

30
day_7/filetree.c Normal file
View file

@ -0,0 +1,30 @@
#include "filetree.h"
#include <stdlib.h>
void add_directory(struct dirnode *cur, struct dirnode *item) {
// NOTE(feliix42): I'm aware that this is super inefficient, but who cares
struct dirnode **items = realloc(cur->directories, (cur->num_dirs + 1) * sizeof(struct dirnode *));
if (!items) {
fprintf(stderr, "\033[93m[error] Failed to allocate memory\033[0m\n");
return;
}
cur->directories = items;
cur->directories[cur->num_dirs] = item;
cur->num_dirs++;
}
void add_file(struct dirnode *cur, struct filenode *item) {
// NOTE(feliix42): I'm aware that this is super inefficient, but who cares
struct filenode **items = realloc(cur->files, (cur->num_files + 1) * sizeof(struct filenode *));
if (!items) {
fprintf(stderr, "\033[93m[error] Failed to allocate memory\033[0m\n");
return;
}
cur->files = items;
cur->files[cur->num_files] = item;
cur->num_files++;
}

30
day_7/filetree.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef FILETREE_H
#define FILETREE_H
#include <stdlib.h>
#include <stdio.h>
struct filenode {
char *name;
unsigned long size;
};
struct dirnode {
char *name;
struct filenode **files;
size_t num_files;
struct dirnode **directories;
size_t num_dirs;
unsigned long size;
};
void add_directory(struct dirnode *cur, struct dirnode *item);
void add_file(struct dirnode *cur, struct filenode *item);
#endif // FILETREE_H

984
day_7/input.txt Normal file
View file

@ -0,0 +1,984 @@
$ cd /
$ ls
dir gqcclj
dir lmtpm
dir nhqwt
dir qcq
dir vwqwlqrt
$ cd gqcclj
$ ls
62425 dqp.gjm
174181 hrtw.qsd
273712 pflp.mdw
169404 zlthnlhf.mtn
180878 zprprf
$ cd ..
$ cd lmtpm
$ ls
dir clffsvcw
163587 cvcl.jqh
dir dcqnblb
dir dtpwln
dir fvt
dir hrcrw
dir jdqzmqn
236754 nrdmlj
205959 pflp.mdw
dir qcq
dir rsn
129926 vdgcqdn.sqd
dir zprprf
$ cd clffsvcw
$ ls
6997 dcqnblb.wbh
145711 dqp
159225 pflp.mdw
$ cd ..
$ cd dcqnblb
$ ls
dir dcqnblb
dir gfn
dir lpswsp
dir lvt
dir zprprf
$ cd dcqnblb
$ ls
2020 grpdmd.ggz
dir zpswzfvg
$ cd zpswzfvg
$ ls
206998 zprprf.gnw
$ cd ..
$ cd ..
$ cd gfn
$ ls
277530 rhbvtblc.mvw
$ cd ..
$ cd lpswsp
$ ls
173180 dcqnblb
$ cd ..
$ cd lvt
$ ls
dir hjllwsvl
dir ptbt
$ cd hjllwsvl
$ ls
dir wqnc
$ cd wqnc
$ ls
64695 grpdmd.ggz
$ cd ..
$ cd ..
$ cd ptbt
$ ls
150880 vvbt.gtp
$ cd ..
$ cd ..
$ cd zprprf
$ ls
dir ldzslndn
dir qftt
$ cd ldzslndn
$ ls
dir bwqqsbhg
129454 vbn
$ cd bwqqsbhg
$ ls
108701 zprprf.gss
$ cd ..
$ cd ..
$ cd qftt
$ ls
64268 cvcl.jqh
$ cd ..
$ cd ..
$ cd ..
$ cd dtpwln
$ ls
196215 cvcl.jqh
dir dpwg
dir ldzslndn
dir znnsqqh
$ cd dpwg
$ ls
192388 gmh
47754 grgzh.qdl
99449 hqsh
dir pbmf
50061 pflp.mdw
192902 qcq.pgg
dir rmpvj
dir scgc
$ cd pbmf
$ ls
210083 wpfnwbl.mgf
$ cd ..
$ cd rmpvj
$ ls
125738 nmlnbvrd
226214 zprprf.jnp
114257 zprprf.srs
$ cd ..
$ cd scgc
$ ls
182115 rrc.rcc
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
201992 qcrm.cpd
$ cd ..
$ cd znnsqqh
$ ls
85635 cvcl.jqh
$ cd ..
$ cd ..
$ cd fvt
$ ls
dir dcqnblb
dir gnc
75864 vfn
$ cd dcqnblb
$ ls
dir dcqnblb
dir lbnflwsh
$ cd dcqnblb
$ ls
269901 cvcl.jqh
$ cd ..
$ cd lbnflwsh
$ ls
33336 grpdmd.ggz
42861 phg.wmc
$ cd ..
$ cd ..
$ cd gnc
$ ls
dir jhjbjsp
dir jjppr
$ cd jhjbjsp
$ ls
96177 ldzslndn
$ cd ..
$ cd jjppr
$ ls
181016 dqp
$ cd ..
$ cd ..
$ cd ..
$ cd hrcrw
$ ls
261376 dtjfpppr.dww
54658 vsrgvw.pfn
$ cd ..
$ cd jdqzmqn
$ ls
52342 dcpndc.vlg
171946 gggpchh.tbb
dir ldzslndn
11156 nbfrfvv.gzw
$ cd ldzslndn
$ ls
107873 cvcl.jqh
216034 gfdjrbz
68844 pqllfrrh.jcf
$ cd ..
$ cd ..
$ cd qcq
$ ls
152886 ldzslndn.ltn
105125 vwplh.vbf
$ cd ..
$ cd rsn
$ ls
15385 hqcmjdgv.jjv
105735 qcq.bzg
58805 snczcsp
26668 vbn
$ cd ..
$ cd zprprf
$ ls
dir chbmq
dir dcqnblb
dir dqp
dir nfspb
89506 zprprf.hnt
$ cd chbmq
$ ls
dir cnjvw
dir dqp
151434 frsvrdnt
dir msztjvcb
240689 qcq.jlh
dir sjzrcg
97312 vnr.zfr
dir zprprf
$ cd cnjvw
$ ls
dir bpbs
252403 cqhtshc
dir djmjhn
10935 fhqmswr
6582 pdwml.ldd
dir qcq
219282 rfmd
$ cd bpbs
$ ls
147582 bnhwsnsj.gdm
61362 cvcl.jqh
152857 vdgcqdn.sqd
$ cd ..
$ cd djmjhn
$ ls
dir bjdbcjbb
dir dcqnblb
dir dqp
dir lgdwtt
$ cd bjdbcjbb
$ ls
110710 cvcl.jqh
252792 hmshctr.lgz
dir mjhtmbj
189745 shsswcgr
dir tfnhp
194940 vbn
dir zprprf
$ cd mjhtmbj
$ ls
dir dqp
dir hbthpcmb
$ cd dqp
$ ls
200832 sbcrz.qgw
$ cd ..
$ cd hbthpcmb
$ ls
55191 ffcntg
$ cd ..
$ cd ..
$ cd tfnhp
$ ls
276825 dqp
161538 gqmr.wgb
$ cd ..
$ cd zprprf
$ ls
287638 dcqnblb.ssp
41274 hgmrvj.mwf
249118 sbb.gsf
105141 wwrg.gqz
$ cd ..
$ cd ..
$ cd dcqnblb
$ ls
1957 btmmc
32386 dtzbzg.dhm
dir mmrbj
98283 ntmhfgtl.pmf
dir zprprf
$ cd mmrbj
$ ls
273194 wnsq
251527 zprprf
$ cd ..
$ cd zprprf
$ ls
27678 ldzslndn.rrl
62866 ljf.fdj
148502 qcq.dlg
dir rvgqvm
179231 tllnmhn.pjp
64033 vbn
dir zcdrj
$ cd rvgqvm
$ ls
dir ntbv
262324 prhgj.szz
dir qbvdh
$ cd ntbv
$ ls
116608 cgv.fvj
175200 swpswq.twt
$ cd ..
$ cd qbvdh
$ ls
160353 sdhfrb.wjn
$ cd ..
$ cd ..
$ cd zcdrj
$ ls
283262 ctl
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir jfzm
111438 rdrgb.mjf
64194 wgtmqrq
dir zprprf
$ cd jfzm
$ ls
158774 pflp.mdw
$ cd ..
$ cd zprprf
$ ls
215264 sgsstcp
$ cd ..
$ cd ..
$ cd lgdwtt
$ ls
dir qcq
$ cd qcq
$ ls
165461 ldzslndn.vvb
$ cd ..
$ cd ..
$ cd ..
$ cd qcq
$ ls
dir dpd
165044 grpdmd.ggz
82343 ldzslndn
dir mwg
176689 psjcwp.wct
44404 qcq.zwd
$ cd dpd
$ ls
84087 dqp
227386 zprprf.gfs
$ cd ..
$ cd mwg
$ ls
214086 pflp.mdw
dir sjjsdn
225859 wcdt
158892 zprprf.frs
$ cd sjjsdn
$ ls
260121 gplgp.dfn
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir hcrwclpg
dir zphd
$ cd hcrwclpg
$ ls
dir cmqntjj
16393 ldzslndn.qbm
91152 qqdtc.zdq
$ cd cmqntjj
$ ls
272266 ldzslndn.pll
$ cd ..
$ cd ..
$ cd zphd
$ ls
165711 chftwcsw.fqw
256871 cvcl.jqh
251168 zprprf.gfv
$ cd ..
$ cd ..
$ cd msztjvcb
$ ls
206231 brzn.lmn
dir dcqnblb
21571 dqp
dir fmn
45779 mlfctz.cjr
288827 pflp.mdw
220578 qcq.fqf
$ cd dcqnblb
$ ls
198121 ghbwgs
93681 nmqhl.vpq
$ cd ..
$ cd fmn
$ ls
29407 mdfws.qvs
$ cd ..
$ cd ..
$ cd sjzrcg
$ ls
155120 ddclvsjr.rpq
136029 ldzslndn.dcm
dir vhzh
$ cd vhzh
$ ls
212446 vbn
$ cd ..
$ cd ..
$ cd zprprf
$ ls
240335 crt.gqh
185363 gnmm.qgh
dir ldzslndn
dir nwl
dir qll
277043 vbn
217796 vtvgpdl.vtm
$ cd ldzslndn
$ ls
273570 cvcl.jqh
68510 fgdmz.hrc
dir npq
dir swjrzzrm
$ cd npq
$ ls
97923 dzcjsqwt
$ cd ..
$ cd swjrzzrm
$ ls
180599 tmpgn.bjf
$ cd ..
$ cd ..
$ cd nwl
$ ls
171833 dlwrfhh.qgn
$ cd ..
$ cd qll
$ ls
219926 dcqnblb.bvn
$ cd ..
$ cd ..
$ cd ..
$ cd dcqnblb
$ ls
dir lvpb
276198 tbgcm.qct
$ cd lvpb
$ ls
142590 bvhjlld
268259 gnjfg.sgb
dir qcq
206220 qcq.zsg
258137 rrsw.dnb
dir tmr
215549 vbn
$ cd qcq
$ ls
dir mmpgd
dir tdsz
dir tmfvsjwc
$ cd mmpgd
$ ls
70793 jwbnpwnn
$ cd ..
$ cd tdsz
$ ls
246310 tdvrhhg.bzq
$ cd ..
$ cd tmfvsjwc
$ ls
103899 grpdmd.ggz
287850 ldzslndn
125930 llhr
$ cd ..
$ cd ..
$ cd tmr
$ ls
83344 fbtfcg.hqp
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir lbgmcbv
dir nbg
$ cd lbgmcbv
$ ls
81776 wzdzzdp
$ cd ..
$ cd nbg
$ ls
dir mfsgjp
155574 pflp.mdw
$ cd mfsgjp
$ ls
199400 vdgcqdn.sqd
$ cd ..
$ cd ..
$ cd ..
$ cd nfspb
$ ls
262412 csrdtbs
73867 vbn
136389 zqps.hjt
$ cd ..
$ cd ..
$ cd ..
$ cd nhqwt
$ ls
123766 cvcl.jqh
dir dhrtvctp
222086 grpdmd.ggz
dir gzg
26005 lhpmz.tgz
dir mcnjwwfr
117122 msn.gst
$ cd dhrtvctp
$ ls
224079 vdgcqdn.sqd
$ cd ..
$ cd gzg
$ ls
124395 dqp
dir wqdbtqm
$ cd wqdbtqm
$ ls
237354 pflp.mdw
212019 vdgcqdn.sqd
$ cd ..
$ cd ..
$ cd mcnjwwfr
$ ls
92504 cshdztf
dir dctl
dir dqp
dir flcrmhlj
161879 grpdmd.ggz
dir gtt
dir hlbnhchz
220093 mdtdsgvm.zgg
dir twntr
287192 vbn
$ cd dctl
$ ls
dir bbhch
155396 hrrj.jzm
164971 pblqmwj.vdb
dir wnlgfpvf
$ cd bbhch
$ ls
dir dpqtp
dir jvdrcw
$ cd dpqtp
$ ls
174135 gwb.qrb
$ cd ..
$ cd jvdrcw
$ ls
215993 dcqnblb.cqp
200800 stjttf.ngc
$ cd ..
$ cd ..
$ cd wnlgfpvf
$ ls
135978 cvcl.jqh
dir dqp
54018 lbrfmt
$ cd dqp
$ ls
270516 dcqnblb.jqw
dir dqp
144626 grpdmd.ggz
157731 hvcv.rhp
133773 lnnt
76250 vdgcqdn.sqd
$ cd dqp
$ ls
41504 zprprf.cmc
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir dqp
dir ldzslndn
236737 mqzcvm.fjh
239746 nhcdz.ncj
dir rpchqq
248824 vdgcqdn.sqd
250937 zrchht.mwg
$ cd dqp
$ ls
203381 qcq.djm
$ cd ..
$ cd ldzslndn
$ ls
dir dqp
dir fptnzlv
dir gmbnpm
dir vhvblt
$ cd dqp
$ ls
19579 qcq.lhg
$ cd ..
$ cd fptnzlv
$ ls
209930 dcqnblb
$ cd ..
$ cd gmbnpm
$ ls
dir ldzslndn
dir qcq
$ cd ldzslndn
$ ls
11075 pflp.mdw
$ cd ..
$ cd qcq
$ ls
dir tdp
$ cd tdp
$ ls
40741 vdgcqdn.sqd
$ cd ..
$ cd ..
$ cd ..
$ cd vhvblt
$ ls
dir lzr
$ cd lzr
$ ls
62245 gbnj.llg
$ cd ..
$ cd ..
$ cd ..
$ cd rpchqq
$ ls
dir bcs
dir dcqnblb
dir fvjzn
dir lrphzrv
$ cd bcs
$ ls
179794 bbn.dzb
242069 cmjdmzjf.zgf
1703 cvcl.jqh
dir gnmhwj
dir ldzslndn
152520 qltpsz.jsj
dir sqqjfps
$ cd gnmhwj
$ ls
dir gvs
201600 hptn.ftf
dir hzrnb
dir qcq
dir sqhl
$ cd gvs
$ ls
152358 zprprf.mlh
$ cd ..
$ cd hzrnb
$ ls
94290 gplsfd
$ cd ..
$ cd qcq
$ ls
91909 vmqd.bmg
$ cd ..
$ cd sqhl
$ ls
238673 vdgcqdn.sqd
262885 zmdvr.nfg
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
240461 mdz
84303 qtj
$ cd ..
$ cd sqqjfps
$ ls
88753 fwn.tff
$ cd ..
$ cd ..
$ cd dcqnblb
$ ls
dir dqp
189996 dqp.pvp
$ cd dqp
$ ls
dir qvfjz
196506 vbn
$ cd qvfjz
$ ls
209316 pflp.mdw
107459 rwpbh.vpt
$ cd ..
$ cd ..
$ cd ..
$ cd fvjzn
$ ls
241464 cvcl.jqh
dir dqp
dir ldzslndn
dir msp
125 pflp.mdw
131895 vbn
$ cd dqp
$ ls
34019 pflp.mdw
202957 vbn
$ cd ..
$ cd ldzslndn
$ ls
147492 cvcl.jqh
248719 spc.rfv
$ cd ..
$ cd msp
$ ls
184407 cvcl.jqh
$ cd ..
$ cd ..
$ cd lrphzrv
$ ls
dir bbwqmbg
81858 cvcl.jqh
dir dqp
248670 gqqsww.tsn
199141 grpdmd.ggz
dir ldzslndn
34514 ldzslndn.ctw
dir tln
214615 zprprf.fwm
$ cd bbwqmbg
$ ls
129750 flf
dir pvlw
dir qcq
126 sqcqphz.tbm
$ cd pvlw
$ ls
198005 jfvj.hdv
$ cd ..
$ cd qcq
$ ls
dir wgdzws
$ cd wgdzws
$ ls
253522 ldzslndn.qwt
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
281993 cvcl.jqh
dir hwqjlwcb
50532 msccz.qgm
102187 trv.tnq
111 wplnmj.bfl
$ cd hwqjlwcb
$ ls
267580 dhjqb.dsb
153195 ldzslndn.jqv
41526 mvwcwc.zsc
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
58666 cvcl.jqh
79950 dqp.tmc
242217 hns.lrb
dir njswzh
240692 vdgcqdn.sqd
dir zvmjvcdm
52909 zzh
$ cd njswzh
$ ls
149732 cvcl.jqh
dir rnmfd
$ cd rnmfd
$ ls
75368 dqp.hmv
14350 vbn
$ cd ..
$ cd ..
$ cd zvmjvcdm
$ ls
dir jgczt
$ cd jgczt
$ ls
dir qcq
95941 qzvvwshv.jwc
$ cd qcq
$ ls
273942 pflp.mdw
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd tln
$ ls
dir bmcng
1518 lrg
dir vnjfrhp
$ cd bmcng
$ ls
38917 fqcrt
$ cd ..
$ cd vnjfrhp
$ ls
dir dcqnblb
dir dqp
247186 grpdmd.ggz
dir ldzslndn
169216 pflp.mdw
206487 vdgcqdn.sqd
16976 vlsrzjmb.mmc
257938 wjl
$ cd dcqnblb
$ ls
dir dqp
$ cd dqp
$ ls
184133 qcq
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir dcqnblb
31612 dqp.pnt
212283 ldzslndn
61600 vdbfc.ddj
197189 wpv.wff
$ cd dcqnblb
$ ls
62412 tfzllmrj
dir zprprf
$ cd zprprf
$ ls
dir bqnpsl
dir dszrvpzc
$ cd bqnpsl
$ ls
261548 spbsbbsw.cmn
$ cd ..
$ cd dszrvpzc
$ ls
188232 sggpqslr.smn
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
dir bgnhd
dir pgvcdzwz
dir qgzhm
$ cd bgnhd
$ ls
56989 cvcl.jqh
$ cd ..
$ cd pgvcdzwz
$ ls
110034 qhgnndv
$ cd ..
$ cd qgzhm
$ ls
247232 grpdmd.ggz
269292 ldzslndn
153843 tpz
dir vnschqwr
162392 wnq.btb
$ cd vnschqwr
$ ls
43005 fvtvzfqm.jvc
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd flcrmhlj
$ ls
245668 dcqnblb.sdj
dir lffj
229909 pflp.mdw
280176 vbn
$ cd lffj
$ ls
116451 jmzz.jdd
dir pjlwb
162815 pmhlqq.snr
226183 zffth
$ cd pjlwb
$ ls
67518 qcq.hjq
$ cd ..
$ cd ..
$ cd ..
$ cd gtt
$ ls
52105 grpdmd.ggz
126869 zprprf.fgj
$ cd ..
$ cd hlbnhchz
$ ls
3064 dqp.lrw
278756 grpdmd.ggz
177208 ldzslndn.wlv
141685 vbn
$ cd ..
$ cd twntr
$ ls
63747 cvcl.jqh
$ cd ..
$ cd ..
$ cd ..
$ cd qcq
$ ls
226858 cwblp.zgp
dir jjqsmfhr
dir rjbqtrq
dir vwmpnbts
141715 wdbhdch
286381 zprprf
$ cd jjqsmfhr
$ ls
dir btmm
dir fqndtlgq
$ cd btmm
$ ls
4031 dqp.lrr
dir fzdd
$ cd fzdd
$ ls
dir vnwpn
$ cd vnwpn
$ ls
dir bzlgsl
dir ztvzrrbv
$ cd bzlgsl
$ ls
9294 ldzslndn.sqr
$ cd ..
$ cd ztvzrrbv
$ ls
256017 cvcl.jqh
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd fqndtlgq
$ ls
271528 ccbmgp.bwd
$ cd ..
$ cd ..
$ cd rjbqtrq
$ ls
122150 ldzslndn
46467 tpdvp.pjf
$ cd ..
$ cd vwmpnbts
$ ls
47518 fcrwfzvm
263343 gmc.lrt
212764 qcq
$ cd ..
$ cd ..
$ cd vwqwlqrt
$ ls
dir psrs
$ cd psrs
$ ls
281998 zprprf.hml

79
day_7/lexer.l Normal file
View file

@ -0,0 +1,79 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#ifdef DOESNT_HAVE_STRDUP
#warning DOESNT_HAVE_STRDUP
char *strdup(const char *s);
#endif
%}
%option warn nodefault
/* makes the scanner terminate after reaching <<EOF>> 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 [ ]
/*general definitions*/
CHAR [a-zA-Z]
NUM [0-9]
/*operands*/
SHELL [$]
ROOT [/]
PARENT ".."
CHDIR "cd"
LIST "ls"
DIR "dir"
%%
{NL} { return NEWLINE; }
{SPACE} { return SPACE; }
{SHELL}{SPACE} { return PROMPT; }
{CHDIR} { return CHDIR; }
{LIST} { return LIST; }
{ROOT} { return ROOT; }
{PARENT} { return PARENT; }
{DIR} { return DIRECTORY; }
{NUM}+ {
unsigned long num = strtoul(yytext, NULL, 10);
yylval->num = num;
return SIZE;
}
{CHAR}+ |
{CHAR}+"."{CHAR}+ {
yylval->path = strdup(yytext);
// yylval->path = calloc(strlen(yytext) + 1, sizeof(char));
// strcpy(yylval->path, yytext);
return PATHSPEC;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%

286
day_7/parser.y Normal file
View file

@ -0,0 +1,286 @@
/* 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 "filetree.h"
typedef void* yyscan_t;
struct dirstack {
struct dirstack *next;
struct dirnode *item;
};
struct parser_state {
struct dirnode *root;
struct dirnode *cur;
/// models the current directory stack we're in for simplicity
struct dirstack *stackptr;
};
struct dirstack* dirstack_push(struct dirstack *stack, struct dirnode *item);
struct dirstack* dirstack_pop(struct dirstack *stack);
}
// Code for the c file
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#include "lexer.h"
const unsigned long upper_bound = 100000;
const unsigned long fs_size = 70000000;
const unsigned long needed_space = 30000000;
struct dirstack* dirstack_push(struct dirstack *stack, struct dirnode *item) {
struct dirstack *head = calloc(1, sizeof(struct dirstack));
if (!head) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
return NULL;
}
head->item = item;
head->next = stack;
return head;
}
struct dirstack* dirstack_pop(struct dirstack *stack) {
if (!stack) {
fprintf(stderr, "\033[91m[error] Called `dirstack_pop on an empty stack\033[0m\n");
return NULL;
}
struct dirstack *next_item = stack->next;
free(stack);
return next_item;
}
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 {
char *path;
unsigned long num;
}
%start input
%token NEWLINE SPACE
%token PROMPT CHDIR LIST
%token ROOT PARENT DIRECTORY
%token <path> PATHSPEC
%token <num> SIZE
%term END_OF_FILE
%%
input
: command input
| END_OF_FILE { return 0; }
;
command
: PROMPT CHDIR SPACE ROOT NEWLINE {
while (state->stackptr->next) {
state->stackptr = dirstack_pop(state->stackptr);
}
state->cur = state->stackptr->item;
}
| PROMPT CHDIR SPACE PARENT NEWLINE {
state->stackptr = dirstack_pop(state->stackptr);
state->cur = state->stackptr->item;
}
| PROMPT CHDIR SPACE PATHSPEC NEWLINE {
// printf("searching in %s now for path %s\n", state->cur->name, $4);
// move into the new directory (creating it if it doesn't exist is not necessary, LIST does that)
struct dirnode *cur_node = state->cur;
// search for the directory name
size_t found = 0;
int c = -1;
size_t i;
for (i = 0; i < cur_node->num_dirs; i++) {
if (strcmp($4, cur_node->directories[i]->name) == 0) {
found = i;
c = 0;
// printf("equal: [%s] and [%s]\n", $4, cur_node->directories[i]->name);
} else {
// printf("not equal: [%s] and [%s]\n", $4, cur_node->directories[i]->name);
}
}
if (c == -1) {
fprintf(stderr, "\033[91m[error] got no such directory lol\033[0m\n");
// TODO: error, directory not found
} // else {
// printf("found it\n");
// }
struct dirnode *next = cur_node->directories[found];
// actually move into the new directory
state->stackptr = dirstack_push(state->stackptr, next);
state->cur = next;
}
| PROMPT LIST NEWLINE output
| NEWLINE
;
output
: listing NEWLINE output
|
;
listing
: DIRECTORY SPACE PATHSPEC {
struct dirnode *item = calloc(1, sizeof(struct dirnode));
if (!item) {
// TODO: error handling
}
item->name = $3;
add_directory(state->cur, item);
}
| SIZE SPACE PATHSPEC {
struct filenode *item = calloc(1, sizeof(struct filenode));
if (!item) {
// TODO: error handling
}
item->name = $3;
item->size = $1;
add_file(state->cur, item);
}
;
%%
unsigned long sum_and_count(struct dirnode *node) {
unsigned long total_sum = 0;
for (size_t i = 0; i < node->num_files; i++) {
node->size += node->files[i]->size;
}
for (size_t j = 0; j < node->num_dirs; j++) {
// recursively call function on subdirectory to compute its size
total_sum += sum_and_count(node->directories[j]);
node->size += node->directories[j]->size;
}
if (node->size <= upper_bound) {
total_sum += node->size;
}
return total_sum;
}
struct dirnode* find_deletion_candidate(struct dirnode *cur, unsigned long needed_space) {
if (cur->size < needed_space) {
// abort recursion if directory sizes get too small
return NULL;
}
struct dirnode *best_candidate = cur;
unsigned long best_diff = best_candidate->size - needed_space;
for (size_t i = 0; i < cur->num_dirs; i++) {
struct dirnode *candidate = find_deletion_candidate(cur->directories[i], needed_space);
// only proceed if there's an actual candidate
if (candidate) {
unsigned long delta = candidate->size - needed_space;
if (delta < best_diff) {
best_candidate = candidate;
best_diff = delta;
}
}
}
return best_candidate;
}
int main(void) {
struct dirnode *root = calloc(1, sizeof(struct dirnode));
if (!root) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
return EXIT_FAILURE;
}
root->name = "/\0";
struct dirstack *stack_ptr = dirstack_push(NULL, root);
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->root = root;
state->cur = root;
state->stackptr = stack_ptr;
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
unsigned long sum_of_sizes = sum_and_count(root);
printf("The sum of all directory sizes < %zu is: %zu\n", upper_bound, sum_of_sizes);
// task 2
unsigned long free_space = fs_size - root->size;
unsigned long to_clear = needed_space - free_space;
printf("------------------------------------------------\n");
printf("Total Disk Space Available: %zu\n", fs_size);
printf("Total Disk Space Used: %zu\n", root->size);
printf("Free Disk Space: %zu\n", free_space);
printf("Required Disk Space: %zu\n", needed_space);
printf("------------------------------------------------\n\n");
printf("Looking to free %zu units\n", to_clear);
struct dirnode *closest_match = find_deletion_candidate(root, to_clear);
printf("Recommended deletion candidate: `%s` (size %zu)\n", closest_match->name, closest_match->size);
return 0;
}

3
day_8/Makefile Normal file
View file

@ -0,0 +1,3 @@
TARGET := day_8
include ../Makefile.common.mk

100
day_8/input.txt Normal file
View file

@ -0,0 +1,100 @@
102212110110302230012132130441442043243242145112422525112333344240121120342411110001231112222021211
222101001302011022124443303121322323142224235123233315215333545242421321241403143213333101310210211
200201223122002022301310313414224443253123231515151513541254251131310343344222040320023211301320200
200010213032223103230132143412204353411323525332513442523314324332413034422122304413120211230122212
010211000332000340422300132032513533513115523254315325423242433353351244232123121111220123121333221
200122303232310323111312343413321244422555551325423211253434511531321144403333313021201212232131220
000212003210330240303442205443113542544534111314155152232435243131155352422402104202123433111201232
103312101101443020314140255354241134315242455146454323625133153413235312512313332430221300331131100
010220000301314241214335313133422432322123354664326463436444322422342554355225140334421002113133121
323101132001123022243353315252233331435562622453564455524652525652244413142424222440021003003130221
311311300011420441113315524451532135232526456634245324454325625255644144511435413342301130211231301
122330114303143110342335214315243456332354435254662545365545352556636544332142113343020330142302023
022120000403423231542555223312544433353262526636242665353354342624542223253541435144031113311431220
210013131013443325232155555522255445233525524424425466462465252544334662211253135515414212121223103
122130234323200111421534352526522446635243456643673475346653325362454362443421552412233011444032132
330230413401024335441342264233664326224445367753634776764473243636265344556415445523524224043114211
020342003212232435413445263654334245243765543345436537434453757662242253666363442422244144403424000
200102231042423341434316352256242653765564636564563755363577363456266625662553514113422123403421132
313204040243524231344465644555626363733665445433637447335677545646763646633226445435325125112213414
314103222004445425153443435336445733737355475744355366644536666633436334423645665121453234041020422
241224100115215322523535566553547766677645474354634357364574375657645546622424324323251244433440201
022222042544545521533326245235374365344376774543443767376646565377535776236535264522124353322203234
422222135434133533655352463473735556656745548755787857555356377355447565365456463664354434453044312
304324242134435426624362552443745545463386458858447758455754543677463336472433462334512421444332223
341414135242433442655442346556355346658767857767584647675766884536373667347366462552211223152411402
322422011132511352664233364365653466744454566746588468655774844685736565337465223255264314431430230
230422221453335454423325334433457458687888455486757548466746647544834643335672463455634151341522302
234004552353522225535427357745555646855484747845567786675574858868655476475453443665223324353452113
020135314133142653352266355334757758775757788848887684458858675874884637537467525625364253131211244
414342423551254236456446367655347867476487576757978699668748774646468564677435372565356631415154301
402224514555445234247373353744455587885585976595966767878796785855864485334466564262266452143444311
322114434454542333646465444736668764744886688767569596959858556668754886535574755434322662442212133
121324243324423343546763375477766585857886776859668579795978997478645686674366773535335522313423220
133542124333622654643366436564658876577978579997657798669999767568447887467776367522435455533321443
023413415133262422437473376558558855786797675666795779995686567865485584745545375742263662533555543
141153353554563662677655657576756468676875598877958956899955978897446555748753563362626454221131125
325353321363522257537756588466744857658697678559699997698595865765578845586445367745536633643312255
423213544666625365635544655675458885666985859697786876896858858797786874566466456657466442525434421
111454444524556247634754788885848856779676996689987686878899758967589455644666556534542332454425232
113454234223324274556335586767549686998856766879779799897977886877798476548873366444323624534235512
413111246643442463347377577668885588996599677886898888866767856868795965878475573774545255562121514
033322552545543547534354548647668766698879698968876698988768678695558875847754474335753623445451141
033335136263443377434448478688978869869678786967679698969669778569788867674444636373445324354533414
234114125435462653577777876855756557856867678669979986768879699797765796588455677467665646523542244
315515242442526536477764768685998958898698776897898777978978889975778988546787635537642445246521555
321543542424553344445665648775695579876967767799989797989877898976866779658748865436436245526532153
214442166263264436546368554888589767787766679977779878789696679785859879648657647656345336454331514
534225433653422444656547767677967556776977788979899998877878867678798578885766845756635543235613324
451252264522467473463764566489668785797977788978898788799896886865858766846568667573775654463254212
142243545346537474556565585866986596889698977988978988797896887869755867746847846457535246243622521
314533543366656735447764644785876759686776988887899987977868668886569975674466755746757543333613415
423155324556563534544657846546998786988679877979797897977866789679597759764485834747537363422511525
111123232243256547763345885669578779768699999987898779978667968776659685647547563666543566442343212
153351242236666565346454875568986796797687677988888979789886768978999686477777536735573656352555412
323332424652563666766546867459698977996778977989988888888788678879875896444874453373545246666243111
135523512363264534676375878685595968577686668988899788976766699895667985668547534554755246336443443
532252346635325565376768676458557777667966786898889778979967688766768868568846557747445423253133325
021423433266355547435457658778656599566767767766877768698699878975689884477647674353546424463554451
143143223365543567635335747757587977786897796676679778966786689768589657474877567637754423223422312
042415253643232575556737767785757788676996887999678977967666877589856675557885677334454634555223453
234134135655336276745375664665486567958579869999988689769887997756966947584657365476724435655111251
042425212462644477445435755454677877597667699897769689877689755868578567458576456463552232355231414
354513445446645356573335466767579577676958688689866686776999686878859666766855753737445265524242214
233154124332425336374475485444574569589885768789686679877995988778897787557646674666625354631424244
242554311144536267576756377488488867896687566758869688757566767878568654674856637746343532221224535
031352533424343666353537635778657888979797968869596858555897765778484557476745657576565652351431515
244111523123546656444743447588484547797769687556999759895965969964878878867565456634442462655533330
101154321124235234634644573645444776899979778958586696559589578574766767767776545323635556543324520
230453243413346443656346633655876856466765588966595989676877687676564857856756335622566465554243444
400115531525436425435363466676884457554568997656888965766988578885546646566546575455244664152151421
121244523334642663334357574375756475655847758985795997565974854847688754735474744655256243422233523
442124553352143426554554663364376844777584585568578895884475557584464677453543662566322652421252333
121445542333545553333233367636337576575784548858744665868786757667467367567665363332432223545423200
020321333225342646522423376435557446777478648478548458845546545678744437577337244622534425211350022
041332232423124342256245433633577566478865647545565685666764666787764767576462433566522451433113043
014424132244453224234546334667567447876654868784855884886878754645547664773756545644453424241524312
414311054242342142224533466634464356437556445785767567866777856766576577644556443654324531342222310
310322213415213433425552662577476354776577788877577647884464577437343437336643223244511135242014011
300241033122121121456356264344335565375647366774864774858553747547334674624355256434534221515041124
244113020144235342335234634564444633644736357543455335653765466354354346565443633362331511132134132
143133204011342551536234336443376364333444577754334556634356574777657644433624432451425543542044012
124214441344114214133244352626436376363654365447576564735347353654435326246226356243423532004304101
101243244443555155315534544462433463654377635665747456733666473757525254363464365544115552421213312
220422144420114253554354265345456453344337374375545645445347674334436455364526444454513431401002123
313223311020451432455413454445642422634537364635666546754656735345535652356322545145325423121103202
331004402420123535413315226646464442326356744477434645574333466622343266264444114525225333122240311
112302121222114254552233452266654536466263462644653436655626223464363536324452444455541022040211131
212012013423120023144244325334534664552523526224436442254665635246443333635121151334524401320433130
001121341442114243125433441523554465523535244533322625264462365223623553532133143211423424102210003
223220023212441130233151252342414632544243253526323465544536453355466344221452532544441122243331202
312121212403143043222224331132453532666642654664652655433535463465343224212113542013034330243203021
202012103340130001242425435444222111424542356345243653462563232352212415345354221301440242321032000
212202110121021140441443523243344241514332562652664256563344623454345255222133212212024343023301122
021010131200334014423400143154151315325512332544243346551323121315214235513143134101443433302011002
122302321231131322434420345125245413215132313112255444513255422341524133353314114412224230231302110
110111100102010040122344032453213533213254233521331414232113423512123251530432223244441332003202300
200023022231020232023042313022141535414442413511535352414333413345343120402340033343211011203223002
001120213231313130220231100314421411535333234424124253354254335222245404210032440401122303031121122
011011030023132123022011022032214014533223521512511221453113132215230244324434210342210100311021222

45
day_8/lexer.l Normal file
View file

@ -0,0 +1,45 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
%}
%option warn nodefault
/* makes the scanner terminate after reaching <<EOF>> 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]
/*general definitions*/
NUM [0-9]
%%
{NL} { return NEWLINE; }
{NUM} {
unsigned long num = strtoul(yytext, NULL, 10);
yylval->num = num;
return NUM;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%

290
day_8/parser.y Normal file
View file

@ -0,0 +1,290 @@
/* 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 {
typedef void* yyscan_t;
struct parser_state {
/// Layout of the data: forest[row][col]
unsigned short **forest;
size_t rows;
size_t cols;
unsigned short *current_line;
/// for tracking the current position in a line during parsing
size_t pos;
size_t allocated;
};
}
// Code for the c file
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#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 <num> NUM
%term END_OF_FILE
%%
input
: treerow input
| END_OF_FILE { return 0; }
;
treerow
: NUM treerow {
if (state->pos >= state->allocated) {
state->allocated += 10;
state->current_line = realloc(state->current_line, state->allocated * sizeof(unsigned short));
if (!state->current_line) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
YYABORT;
}
}
state->current_line[state->pos] = $1;
state->pos++;
}
| NEWLINE {
if (state->pos != 0) {
state->forest = realloc(state->forest, (state->rows + 1) * sizeof(unsigned short *));
if (!state->forest) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
YYABORT;
}
// resize the current line if necessary
if (state->allocated > state->pos) {
state->current_line = realloc(state->current_line, state->pos * sizeof(unsigned short));
// cannot really fail but oh well
if (!state->current_line) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
YYABORT;
}
}
if (state->pos != state->cols) {
if (state->cols != 0) {
fprintf(stderr, "\033[91m[error] Column size has changed mid-parse\033[0m\n");
YYERROR;
}
state->cols = state->pos;
}
// write the current line to the forest
state->forest[state->rows] = state->current_line;
state->rows++;
// initialize a new row
state->allocated = state->cols;
state->pos = 0;
state->current_line = calloc(state->allocated, sizeof(unsigned short));
if (!state->current_line) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
YYABORT;
}
}
}
;
%%
unsigned long compute_scenic_score(struct parser_state *state, size_t row, size_t col) {
unsigned short height = state->forest[row][col];
unsigned long up = 0;
for (int r = row - 1; r >= 0; r--) {
up++;
if (state->forest[r][col] >= height) {
break;
}
}
unsigned long down = 0;
for (size_t r = row + 1; r < state->rows; r++) {
down++;
if (state->forest[r][col] >= height) {
break;
}
}
unsigned long left = 0;
for (int c = col - 1; c >= 0; c--) {
left++;
if (state->forest[row][c] >= height) {
break;
}
}
unsigned long right = 0;
for (size_t c = col + 1; c < state->cols; c++) {
right++;
if (state->forest[row][c] >= height) {
break;
}
}
return up * down * left * right;
}
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->allocated = 100;
state->current_line = calloc(state->allocated, sizeof(unsigned short));
if (!state->current_line) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
return EXIT_FAILURE;
}
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
// create a bitmap marking counted visible trees
bool **bitmap = calloc(state->rows, sizeof(bool *));
if (!bitmap) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
return EXIT_FAILURE;
}
for (size_t i = 0; i < state->rows; i++) {
bitmap[i] = calloc(state->cols, sizeof(bool));
if (!bitmap[i]) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
return EXIT_FAILURE;
}
}
// count all outer rows and cols as visible
unsigned long visible = 0;
for (size_t c = 0; c < state->cols; c++) {
unsigned short tallest = state->forest[0][c];
bitmap[0][c] = true;
visible++;
// top->down
for (size_t r = 0; r < (state->rows - 1); r++) {
if (state->forest[r][c] > tallest) {
tallest = state->forest[r][c];
if (!bitmap[r][c]) {
visible++;
bitmap[r][c] = true;
}
}
}
// bottom->up
tallest = state->forest[state->rows - 1][c];
visible++;
bitmap[state->rows - 1][c] = true;
for (size_t r = state->rows - 1; r > 0; r--) {
if (state->forest[r][c] > tallest) {
tallest = state->forest[r][c];
if (!bitmap[r][c]) {
visible++;
bitmap[r][c] = true;
}
}
}
}
// move through individual rows
for (size_t r = 0; r < state->rows; r++) {
unsigned short tallest = state->forest[r][0];
if (!bitmap[r][0]) {
bitmap[r][0] = true;
visible++;
}
// left->right
for (size_t c = 0; c < (state->cols - 1); c++) {
if (state->forest[r][c] > tallest) {
tallest = state->forest[r][c];
if (!bitmap[r][c]) {
visible++;
bitmap[r][c] = true;
}
}
}
// right->left
tallest = state->forest[r][state->cols - 1];
if (!bitmap[r][state->cols - 1]) {
bitmap[r][state->cols - 1] = true;
visible++;
}
for (size_t c = state->cols - 1; c > 0; c--) {
if (state->forest[r][c] > tallest) {
tallest = state->forest[r][c];
if (!bitmap[r][c]) {
visible++;
bitmap[r][c] = true;
}
}
}
}
printf("Visible trees: %zu\n", visible);
unsigned long best = 0;
for (size_t r = 0; r < state->rows; r++) {
for (size_t c = 0; c < state->cols; c++) {
unsigned long tmp = compute_scenic_score(state, r, c);
if (tmp > best) {
best = tmp;
}
}
}
printf("Best scenic score: %zu\n", best);
return 0;
}

3
day_9/Makefile Normal file
View file

@ -0,0 +1,3 @@
TARGET := day_9
include ../Makefile.common.mk

2000
day_9/input.txt Normal file

File diff suppressed because it is too large Load diff

56
day_9/lexer.l Normal file
View file

@ -0,0 +1,56 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
%}
%option warn nodefault
/* makes the scanner terminate after reaching <<EOF>> 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;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%

172
day_9/parser.y Normal file
View file

@ -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 <stdbool.h>
#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 <stdio.h>
#include <stdlib.h>
#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> 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;
}

11
shell.nix Normal file
View file

@ -0,0 +1,11 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
propagatedNativeBuildInputs = with pkgs; [
flex
bison
clang
];
}