advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs

commit d836c1292cf2a87c207c2d40388826bbf81c6195
parent 6a74421ce22aa41c12ba46eec2db98ae26163fcf
Author: bsandro <brian.drosan@gmail.com>
Date:   Sun, 19 Dec 2021 05:25:53 +0200

day18 trying to parse

Diffstat:
Aday18/Makefile | 25+++++++++++++++++++++++++
Aday18/input.txt | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday18/main.c | 29+++++++++++++++++++++++++++++
Aday18/puzzle.c | 131+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday18/test.txt | 10++++++++++
5 files changed, 295 insertions(+), 0 deletions(-)

diff --git a/day18/Makefile b/day18/Makefile @@ -0,0 +1,25 @@ +NAME=$(shell basename ${PWD}) +SRC=$(wildcard *.c ../common/*.c) +DEPS:=$(wildcard *.h ../common/*.h) +OBJ:=$(SRC:.c=.o) +CFLAGS=-O0 -g -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/day18/input.txt b/day18/input.txt @@ -0,0 +1,100 @@ +[[[[7,2],[0,2]],9],[[[7,8],6],[0,[2,3]]]] +[[[0,9],3],1] +[[[[0,5],6],[[0,6],[7,8]]],[[[2,2],[1,5]],[9,5]]] +[[2,1],[[3,1],[[3,2],9]]] +[[[[9,3],[7,5]],[5,9]],[[0,[0,4]],2]] +[[[9,9],4],[8,[[4,9],7]]] +[[[1,9],[[8,3],[6,1]]],[5,1]] +[[[[8,6],[1,3]],[3,[1,1]]],[[[6,4],[0,4]],[[0,0],3]]] +[[[[6,4],[3,3]],[7,5]],[[6,5],[0,4]]] +[[[[6,4],[2,3]],5],[9,[[4,4],[7,7]]]] +[[1,[[7,9],1]],[[[8,2],5],[[7,2],8]]] +[[[2,[7,2]],[5,3]],[1,5]] +[[[[2,2],[1,5]],[[1,6],[0,5]]],[6,[9,[9,9]]]] +[[7,2],[[3,8],[5,[6,7]]]] +[[[6,5],[[8,0],1]],[2,[6,0]]] +[[[8,7],6],[[[7,2],[7,0]],3]] +[[[[9,1],[0,1]],[0,4]],[8,0]] +[[[8,2],[8,[2,7]]],[[2,[2,6]],[2,[1,2]]]] +[[[1,7],[[0,0],3]],[[3,[3,6]],[6,3]]] +[[[[5,9],[1,2]],[0,0]],[[8,8],9]] +[[[1,2],[4,2]],[3,4]] +[[[9,[0,0]],[[3,5],7]],[[[8,0],[2,1]],[3,[1,5]]]] +[[[[9,2],[7,1]],[[0,4],5]],[9,2]] +[[3,[6,1]],2] +[[[0,[0,6]],[4,[5,9]]],[[5,9],1]] +[[[9,[8,3]],7],[[0,7],[[8,0],[9,2]]]] +[[[[0,7],7],[[1,8],[4,4]]],[[0,[6,4]],[[4,9],4]]] +[[[[3,3],3],7],4] +[[4,[[5,6],[8,7]]],[[[7,5],2],[2,4]]] +[[[[3,8],[3,7]],2],[[[4,4],[9,7]],4]] +[[[9,[2,9]],0],[7,[[3,2],[3,2]]]] +[[[7,[6,6]],[[6,0],1]],9] +[[3,4],[[5,[9,5]],[[2,6],2]]] +[[[[7,4],[6,8]],[1,[8,7]]],[[[2,6],[1,4]],[8,7]]] +[[3,[[0,8],3]],[[1,8],9]] +[[3,[5,[0,7]]],[[[2,9],[9,3]],[0,[8,8]]]] +[[[[1,7],5],[[4,0],[7,4]]],8] +[[[[3,4],3],4],0] +[[[[1,3],7],[4,[8,0]]],[[[5,9],[4,8]],[8,[7,8]]]] +[[[9,1],[[7,2],7]],[[3,0],9]] +[[[[6,7],2],1],[[[1,1],6],[9,[3,4]]]] +[[[3,8],[4,0]],[[6,[1,0]],[8,[8,3]]]] +[[[9,[4,1]],8],2] +[[[4,3],[[7,8],5]],[[[0,2],[0,5]],[[1,2],2]]] +[[2,7],[[5,[8,5]],[[3,5],[9,9]]]] +[6,[[[2,6],1],[[2,4],9]]] +[[[[8,0],[1,2]],[[1,6],0]],[[0,4],[4,6]]] +[[[[7,0],6],[8,1]],[6,3]] +[[[8,4],[[7,7],[2,4]]],[[7,8],[4,5]]] +[[[4,[5,6]],[[3,4],0]],4] +[[0,6],[[6,1],[8,[9,7]]]] +[[4,[0,6]],[5,[[3,3],[8,0]]]] +[7,[[[8,4],4],[6,7]]] +[[[[7,0],5],[1,[4,4]]],[[9,[0,9]],[[5,1],9]]] +[[[[4,3],[9,1]],2],[0,[[8,8],[1,3]]]] +[[[7,0],[0,[2,8]]],[[0,5],[[2,9],5]]] +[[[8,8],[2,[0,2]]],[[4,5],9]] +[[3,9],5] +[[[[8,6],3],[[5,1],[0,5]]],[3,1]] +[[[7,[1,8]],[3,4]],[4,[[7,9],5]]] +[[[[4,5],[4,3]],[[6,5],[6,9]]],[[3,[7,5]],[8,[2,9]]]] +[[5,0],[[[2,9],1],[[0,6],6]]] +[[4,[8,[3,0]]],[[[9,6],[1,9]],[[1,6],[8,0]]]] +[[[[8,7],[5,9]],[[2,1],[3,4]]],[5,9]] +[[[3,3],[2,8]],[[[1,2],9],[3,2]]] +[[[[0,1],5],[[0,6],[9,3]]],[5,[[5,8],5]]] +[[[5,6],[4,5]],[[7,[2,7]],[[3,1],[0,4]]]] +[7,[[6,2],[[7,3],6]]] +[[[5,3],[5,7]],6] +[1,[[[6,4],[8,1]],[5,[7,5]]]] +[[[3,6],[[7,5],7]],[[4,[4,6]],[6,4]]] +[[[6,3],4],5] +[[[0,9],[9,[0,1]]],[0,[4,9]]] +[[[1,0],0],[[1,4],[9,6]]] +[[[1,[8,0]],[[9,4],[2,0]]],[4,[[6,2],3]]] +[[[[6,0],[5,1]],2],[[[9,6],[5,1]],7]] +[[[9,[3,1]],[8,1]],[[6,[0,9]],[[7,1],4]]] +[[[9,2],[4,[5,3]]],[[[7,1],[2,0]],[3,[2,2]]]] +[[[3,[7,0]],1],[6,[[9,6],[4,4]]]] +[[2,9],[6,[7,3]]] +[1,[[7,[1,9]],5]] +[[[5,3],[[1,5],[7,8]]],[[[5,3],[6,8]],[1,[5,0]]]] +[[[1,[7,4]],[3,8]],[1,[4,[6,3]]]] +[[[3,[8,9]],7],[[[3,4],3],[6,[3,6]]]] +[[[3,[1,7]],[3,[1,8]]],[[[6,4],[6,4]],[[8,6],[3,4]]]] +[[4,6],[[6,[8,4]],6]] +[[[2,[7,7]],[[1,0],[6,6]]],[[[8,0],[1,6]],[[0,5],9]]] +[[[[3,0],[2,1]],[4,7]],3] +[[[[1,7],2],[[8,3],[8,9]]],[[0,4],[[6,4],2]]] +[[[[3,6],9],[0,[6,0]]],[7,8]] +[3,[[7,8],9]] +[[1,[[7,9],[1,2]]],[2,9]] +[[[3,[3,0]],4],[[1,[5,9]],[[9,6],5]]] +[[1,[3,4]],[[8,[2,3]],3]] +[[[8,0],8],[[[1,9],3],[[0,1],[8,6]]]] +[[[[4,1],[3,7]],[[6,2],[5,8]]],[[[4,0],[2,4]],[4,7]]] +[[[[9,0],[5,8]],3],2] +[7,[[[1,0],[9,7]],[[8,3],0]]] +[[[0,1],[4,[4,5]]],[9,[[3,6],[6,8]]]] +[8,[[7,[4,7]],[[0,5],3]]] diff --git a/day18/main.c b/day18/main.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <time.h> +#include <string.h> + +void puzzle(const char *filename, long long *res1, long long *res2); + +int main(int argc, char *argv[]) { + printf("Advent of Code: day 18\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/day18/puzzle.c b/day18/puzzle.c @@ -0,0 +1,131 @@ +#define _DEFAULT_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <strings.h> +#include <assert.h> +#include <stdbool.h> +#include <unistd.h> + +#include "util.h" + +#define STR_LEN 1024 + +struct pair_t { + int *left; + int *right; + struct pair_t *pleft; + struct pair_t *pright; + int **leftmost; + int **rightmost; + struct pair_t *parent; +}; + +size_t make_pair(const char *str, struct pair_t *parent, struct pair_t **pair_ptr); +void print_pair(struct pair_t *pair); + +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; + + struct array_t pairs = { .data = NULL }; + array_init(&pairs, sizeof(void *), 10); + + while (fgets(buf, STR_LEN, infile) != NULL) { + size_t len = strlen(buf); + assert(len != 0); + struct pair_t *pair = NULL; + size_t processed = make_pair(buf, NULL, &pair); + printf("len:%zu, processed:%zu\n", len, processed); + if (pairs.count >= pairs.cap) array_expand(&pairs); + ((struct pair_t **)pairs.data)[pairs.count++] = pair; + + printf("str: %s", buf); + printf("pair: "); + print_pair(pair); + printf("\n"); + + break; + ++line_num; + bzero(buf, STR_LEN); + } + + // mutiny! ignoring feof/ferror. + fclose(infile); +} + +size_t make_pair(const char *str, struct pair_t *parent, struct pair_t **pair_ptr) { + printf("pair start\n"); + + assert(str != NULL); + struct pair_t *pair = malloc(sizeof(struct pair_t)); + bzero(pair, sizeof(struct pair_t)); + printf("pair_ptr: %p, *pair_ptr: %p\n", pair_ptr, *pair_ptr); + *pair_ptr = pair; + + pair->parent = parent; + if (parent != NULL) { + pair->leftmost = parent->leftmost; + pair->rightmost = parent->rightmost; + } + + assert(str[0] == '['); + size_t processed = 1; + + if (str[processed] == '[') { + printf("making left pair...\n"); + processed += make_pair(str+processed, pair, &pair->pleft); + } else if (str[processed] >= '0' && str[processed] <= '9') { + pair->left = malloc(sizeof(int)); + *pair->left = str[processed] - '0'; + pair->leftmost = &pair->left; + printf("left num %d\n", *pair->left); + } + + processed++; + assert(str[processed] == ','); + processed++; + + if (str[processed] == '[') { + printf("making right pair...\n"); + processed += make_pair(str+processed, pair, &pair->pright); + } else if (str[processed] >= '0' && str[processed] <= '9') { + pair->right = malloc(sizeof(int)); + *pair->right = str[processed] - '0'; + pair->rightmost = &pair->right; + printf("right num %d\n", *pair->right); + } + + processed++; + assert(str[processed] == ']'); + printf("pair end\n"); + + return processed; +} + +void print_pair(struct pair_t *pair) { + printf("["); + if (pair->pleft != NULL) { + print_pair(pair->pleft); + } else { + printf("%d", *pair->left); + } + printf(","); + if (pair->pright != NULL) { + print_pair(pair->pright); + } else { + printf("%d", *pair->right); + } + printf("]"); +} diff --git a/day18/test.txt b/day18/test.txt @@ -0,0 +1,10 @@ +[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] +[[[5,[2,8]],4],[5,[[9,9],0]]] +[6,[[[6,2],[5,6]],[[7,6],[4,7]]]] +[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] +[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] +[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] +[[[[5,4],[7,7]],8],[[8,3],8]] +[[9,3],[[9,9],[6,[4,9]]]] +[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] +[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]