advent2021

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

commit 29117a3e835db21029e19364b4aa055f8b413394
parent 6aef118d598a26b339a578dd83e6dbb09366bacd
Author: bsandro <brian.drosan@gmail.com>
Date:   Mon,  6 Dec 2021 14:46:16 +0200

Day 06, puzzle 1 (bruteforce solution)

Diffstat:
Aday06/Makefile | 26++++++++++++++++++++++++++
Aday06/input.txt | 1+
Aday06/main.c | 32++++++++++++++++++++++++++++++++
Aday06/puzzle.c | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday06/test.txt | 1+
5 files changed, 192 insertions(+), 0 deletions(-)

diff --git a/day06/Makefile b/day06/Makefile @@ -0,0 +1,26 @@ +NAME=$(shell basename ${PWD}) +CC=cc +SRC=$(wildcard *.c) +DEPS:=$(wildcard *.h) +OBJ:=$(SRC:.c=.o) + +CFLAGS=-O2 -std=c99 -Werror -Wall -Wextra -I. -I../common + +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/day06/input.txt b/day06/input.txt @@ -0,0 +1 @@ +2,1,2,1,5,1,5,1,2,2,1,1,5,1,4,4,4,3,1,2,2,3,4,1,1,5,1,1,4,2,5,5,5,1,1,4,5,4,1,1,4,2,1,4,1,2,2,5,1,1,5,1,1,3,4,4,1,2,3,1,5,5,4,1,4,1,2,1,5,1,1,1,3,4,1,1,5,1,5,1,1,5,1,1,4,3,2,4,1,4,1,5,3,3,1,5,1,3,1,1,4,1,4,5,2,3,1,1,1,1,3,1,2,1,5,1,1,5,1,1,1,1,4,1,4,3,1,5,1,1,5,4,4,2,1,4,5,1,1,3,3,1,1,4,2,5,5,2,4,1,4,5,4,5,3,1,4,1,5,2,4,5,3,1,3,2,4,5,4,4,1,5,1,5,1,2,2,1,4,1,1,4,2,2,2,4,1,1,5,3,1,1,5,4,4,1,5,1,3,1,3,2,2,1,1,4,1,4,1,2,2,1,1,3,5,1,2,1,3,1,4,5,1,3,4,1,1,1,1,4,3,3,4,5,1,1,1,1,1,2,4,5,3,4,2,1,1,1,3,3,1,4,1,1,4,2,1,5,1,1,2,3,4,2,5,1,1,1,5,1,1,4,1,2,4,1,1,2,4,3,4,2,3,1,1,2,1,5,4,2,3,5,1,2,3,1,2,2,1,4 diff --git a/day06/main.c b/day06/main.c @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <time.h> + +void puzzle(const char *filename, int *res1, int *res2); + +int main(int argc, char *argv[]) { + printf("Advent of Code: day 06\n"); + double time_start = clock(); + + if (argc <= 0) { + return -1; + } + if (argc <= 1) { + printf("Usage: %s inputfile.txt\n", argv[0]); + return -1; + } + + const char *filename = argv[1]; + + int counter1 = -1; + int counter2 = -1; + + puzzle(filename, &counter1, &counter2); + + printf("Puzzle #1: %d\n", counter1); + printf("Puzzle #2: %d\n", counter2); + + double elapsed = clock() - time_start; + printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC); + + return 0; +} diff --git a/day06/puzzle.c b/day06/puzzle.c @@ -0,0 +1,132 @@ +#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> + +#define STR_LEN 16384 +#define DAYS 80 +#define KODOMO 8 +#define OYAKO 6 + +struct fish_t { + struct fish_t *next; + int value; +} fish_t; + +struct fishes_list_t { + struct fish_t *first; + struct fish_t *last; + size_t size; +}; + +void make_fishes_list(struct fishes_list_t *fishes, const char *str); +void print_fishes_list(struct fishes_list_t *fishes); +void add_fish(struct fishes_list_t *fishes, int value); +struct fish_t * new_fish(int value); + +/* ****************************************************************** */ + +void puzzle(const char *filename, int *result1, int *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 fishes_list_t fishes = { .first = NULL, .last = NULL, .size = 0}; + + while (fgets(buf, STR_LEN, infile) != NULL) { + make_fishes_list(&fishes, buf); + + ++line_num; + bzero(buf, STR_LEN); + } + + //printf("fishes:"); + //print_fishes_list(&fishes); + struct fish_t *fish = NULL; + + for (int day = 1; day <= DAYS; ++day) { + fish = fishes.first; + size_t num = 0; + size_t cnt = fishes.size; + while (fish != NULL && num < cnt) { + if (fish->value == 0) { + add_fish(&fishes, KODOMO); + fish->value = OYAKO; + } else { + fish->value--; + } + fish = fish->next; + num++; + } + //printf("day %2d: %zu\n", day, fishes.size); + //print_fishes_list(&fishes); + } + + *result1 = fishes.size; + + // mutiny! ignoring feof/ferror. + fclose(infile); +} + +struct fish_t * new_fish(int value) { + struct fish_t *fish = malloc(sizeof(struct fish_t)); + assert(fish != NULL); + fish->value = value; + fish->next = NULL; + return fish; +} + +void add_fish(struct fishes_list_t *fishes, int value) { + struct fish_t *fish = new_fish(value); + if (fishes->first == NULL) { + fishes->first = fish; + } + + if (fishes->last == NULL) { + fishes->last = fish; + } + + fishes->last->next = fish; + fishes->last = fish; + ++fishes->size; +} + +void make_fishes_list(struct fishes_list_t *fishes, const char *str) { + assert(fishes != NULL); + assert(str != NULL); + char *tmp = strndup(str, STR_LEN); + char *token = NULL; + assert(tmp != NULL); + + while ((token = strsep(&tmp, ",")) != NULL) { + int val = atoi(token); + add_fish(fishes, val); + } + + free(tmp); +} + +void print_fishes_list(struct fishes_list_t *fishes) { + assert(fishes != NULL); + assert(fishes->first != NULL); + struct fish_t *fish = fishes->first; + while (fish != NULL) { + printf("%2d", fish->value); + fish = fish->next; + } + printf("\n"); +} diff --git a/day06/test.txt b/day06/test.txt @@ -0,0 +1 @@ +3,4,3,1,2