advent2021

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

commit ec79421a5a9c955ee159e40b0dea1733b8bd1bf5
parent 26f405dd024d8fee2afc30ab5a2a71c76eeca746
Author: bsandro <brian.drosan@gmail.com>
Date:   Thu,  2 Dec 2021 08:23:27 +0200

Day02, puzzle 1

Diffstat:
Aday02/Makefile | 23+++++++++++++++++++++++
Aday02/input.txt | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday02/main.c | 24++++++++++++++++++++++++
Aday02/puzzle1.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Aday02/puzzle1.h | 1+
5 files changed, 1096 insertions(+), 0 deletions(-)

diff --git a/day02/Makefile b/day02/Makefile @@ -0,0 +1,23 @@ +NAME=$(shell basename ${PWD}) +CC=cc +SRC=$(wildcard *.c) +DEPS:=$(wildcard *.h) +OBJ:=$(SRC:.c=.o) + +CFLAGS=-O0 -std=c99 -g -Werror -Wall -Wextra -I. + +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 diff --git a/day02/input.txt b/day02/input.txt @@ -0,0 +1,1000 @@ +forward 5 +down 8 +down 6 +down 7 +down 8 +forward 7 +down 3 +up 6 +forward 6 +down 2 +forward 5 +down 6 +up 3 +down 4 +forward 4 +down 6 +down 1 +up 5 +forward 5 +down 1 +down 7 +up 2 +down 7 +forward 1 +forward 6 +down 1 +up 1 +up 4 +forward 3 +forward 6 +forward 1 +forward 4 +up 3 +forward 1 +forward 4 +down 9 +forward 4 +forward 8 +up 8 +forward 5 +up 4 +up 3 +down 8 +forward 5 +down 4 +forward 1 +forward 7 +down 1 +forward 8 +down 4 +forward 2 +forward 7 +forward 9 +up 4 +down 3 +forward 7 +forward 6 +down 8 +forward 2 +forward 5 +forward 4 +down 6 +forward 6 +up 5 +down 3 +down 6 +down 5 +down 7 +down 8 +up 5 +down 5 +forward 5 +forward 4 +up 3 +down 7 +down 3 +forward 4 +down 2 +forward 4 +forward 3 +forward 4 +forward 9 +forward 6 +forward 8 +up 8 +down 8 +up 5 +down 4 +down 8 +up 7 +up 8 +down 6 +down 3 +forward 2 +forward 7 +up 1 +up 2 +forward 2 +down 7 +down 1 +up 9 +forward 6 +forward 4 +down 2 +up 6 +down 2 +down 1 +down 3 +up 6 +down 1 +down 8 +forward 7 +up 8 +forward 5 +forward 8 +down 8 +forward 6 +forward 8 +down 3 +down 4 +down 6 +up 2 +forward 6 +up 9 +forward 4 +forward 8 +up 4 +down 8 +forward 8 +down 8 +down 4 +down 5 +forward 7 +down 6 +down 6 +up 2 +up 1 +forward 7 +forward 8 +forward 4 +forward 9 +down 7 +forward 4 +up 5 +down 3 +up 4 +down 9 +down 2 +down 8 +forward 3 +forward 5 +forward 7 +forward 9 +forward 5 +forward 8 +forward 6 +forward 4 +forward 6 +forward 7 +forward 2 +down 1 +down 8 +down 4 +down 5 +down 6 +up 3 +up 2 +forward 4 +down 4 +forward 7 +up 6 +up 9 +down 1 +down 3 +down 1 +up 3 +up 1 +down 2 +up 5 +forward 1 +down 7 +forward 9 +down 4 +up 4 +down 6 +down 3 +forward 4 +up 6 +up 4 +forward 1 +up 7 +down 1 +down 7 +down 7 +forward 9 +down 3 +down 3 +forward 6 +down 2 +forward 7 +up 4 +up 8 +down 8 +forward 7 +forward 6 +down 7 +forward 5 +up 6 +up 6 +down 9 +up 6 +up 2 +forward 9 +forward 1 +up 5 +up 3 +down 9 +up 8 +down 7 +up 7 +forward 5 +down 7 +down 4 +forward 2 +forward 3 +forward 5 +down 1 +up 6 +down 6 +up 6 +down 8 +down 3 +down 4 +forward 9 +down 3 +forward 3 +up 1 +down 2 +forward 8 +down 7 +up 9 +forward 1 +down 3 +forward 1 +forward 8 +down 3 +forward 8 +forward 6 +down 1 +down 9 +forward 2 +down 1 +down 6 +up 1 +up 7 +down 9 +forward 6 +forward 5 +forward 2 +up 6 +down 6 +forward 6 +up 3 +down 7 +down 8 +forward 5 +down 7 +forward 8 +down 8 +forward 4 +down 6 +forward 4 +down 7 +up 5 +down 5 +down 5 +down 4 +down 3 +forward 8 +forward 1 +down 8 +down 2 +forward 3 +forward 7 +forward 3 +down 5 +down 6 +down 8 +down 6 +forward 9 +forward 4 +forward 8 +down 5 +down 7 +forward 4 +up 5 +down 8 +up 6 +up 7 +down 6 +down 8 +forward 3 +up 6 +forward 7 +down 4 +up 1 +up 8 +forward 3 +down 6 +down 1 +forward 7 +down 1 +down 9 +forward 6 +down 4 +forward 3 +forward 1 +down 5 +down 9 +down 9 +down 5 +down 8 +down 7 +forward 1 +forward 5 +down 2 +forward 2 +forward 1 +down 8 +forward 6 +down 3 +forward 4 +up 2 +up 8 +forward 7 +forward 4 +down 8 +up 6 +forward 3 +up 1 +up 2 +forward 5 +forward 9 +down 5 +forward 2 +forward 5 +up 6 +down 1 +down 1 +down 6 +forward 6 +down 7 +forward 5 +forward 8 +down 7 +down 5 +forward 9 +forward 1 +up 6 +down 7 +forward 1 +forward 4 +down 5 +down 6 +up 3 +up 8 +up 5 +down 8 +down 8 +down 6 +down 2 +down 3 +down 9 +forward 8 +forward 7 +forward 7 +up 5 +down 5 +forward 9 +up 8 +up 5 +forward 1 +down 9 +down 9 +forward 9 +forward 4 +forward 6 +up 9 +up 5 +up 3 +down 9 +up 7 +up 1 +down 3 +down 9 +down 7 +forward 6 +down 7 +forward 7 +forward 8 +down 2 +forward 5 +up 1 +down 6 +up 9 +forward 5 +up 9 +down 2 +down 3 +forward 5 +down 9 +forward 9 +forward 2 +forward 8 +down 1 +forward 8 +up 1 +forward 3 +up 1 +down 1 +forward 9 +down 2 +forward 2 +up 1 +up 8 +down 2 +down 7 +down 5 +up 2 +up 6 +down 9 +down 7 +down 7 +up 6 +up 8 +down 7 +forward 5 +down 4 +down 5 +up 8 +up 6 +down 6 +forward 6 +up 6 +down 1 +down 1 +down 1 +forward 1 +down 8 +down 4 +down 5 +down 2 +down 5 +up 8 +up 8 +down 3 +down 6 +down 1 +forward 6 +forward 5 +forward 1 +down 3 +down 4 +up 9 +down 3 +up 8 +forward 5 +down 5 +forward 2 +down 8 +down 2 +up 1 +forward 7 +up 8 +forward 7 +down 3 +down 1 +down 3 +forward 4 +down 5 +down 8 +forward 8 +forward 3 +forward 7 +down 7 +forward 4 +down 1 +forward 3 +up 2 +down 7 +down 1 +forward 4 +forward 7 +down 3 +down 1 +forward 4 +down 3 +forward 2 +up 9 +down 5 +down 9 +forward 5 +up 5 +down 3 +up 6 +up 8 +down 7 +down 3 +down 9 +forward 6 +forward 8 +forward 3 +down 6 +up 8 +forward 8 +forward 9 +down 4 +down 1 +forward 2 +down 2 +up 2 +down 5 +down 1 +down 3 +forward 4 +down 3 +up 8 +up 6 +up 5 +down 4 +forward 3 +up 6 +forward 6 +forward 2 +down 8 +down 5 +forward 3 +up 1 +forward 5 +forward 9 +forward 5 +down 5 +forward 3 +forward 6 +forward 5 +forward 3 +down 1 +down 1 +down 1 +down 9 +forward 8 +forward 2 +forward 4 +forward 8 +down 1 +up 8 +down 1 +down 6 +down 5 +up 8 +down 4 +forward 8 +forward 6 +down 6 +forward 2 +forward 7 +forward 2 +up 7 +forward 4 +up 1 +up 8 +down 3 +down 2 +down 3 +up 7 +down 9 +up 5 +down 1 +down 3 +up 5 +down 6 +up 9 +down 4 +down 7 +down 6 +down 4 +forward 5 +forward 6 +down 8 +forward 3 +forward 8 +up 5 +up 6 +up 8 +forward 8 +forward 1 +down 6 +forward 3 +forward 3 +forward 6 +down 3 +down 2 +forward 5 +down 5 +forward 6 +down 3 +down 9 +down 8 +down 6 +down 6 +forward 1 +up 5 +down 9 +forward 3 +forward 3 +down 2 +forward 8 +forward 3 +forward 2 +forward 5 +down 4 +down 1 +up 2 +down 1 +down 1 +forward 5 +down 7 +up 7 +down 9 +down 8 +down 6 +forward 3 +forward 5 +down 3 +down 6 +up 3 +up 2 +up 8 +down 3 +up 3 +down 6 +forward 7 +forward 4 +up 5 +forward 1 +up 3 +forward 8 +down 2 +down 5 +down 2 +forward 4 +forward 4 +down 4 +up 8 +down 1 +up 2 +forward 2 +forward 9 +forward 4 +down 3 +down 7 +forward 1 +down 2 +forward 8 +down 8 +forward 3 +down 7 +forward 9 +forward 6 +up 1 +forward 3 +up 2 +up 3 +forward 6 +down 8 +up 9 +down 2 +down 9 +down 6 +down 4 +forward 5 +forward 3 +up 7 +forward 7 +up 7 +up 6 +down 7 +down 2 +up 7 +down 5 +up 9 +forward 3 +up 6 +up 6 +up 6 +up 1 +forward 5 +forward 5 +down 8 +forward 6 +forward 7 +down 3 +down 4 +down 2 +down 4 +down 1 +forward 7 +down 7 +down 5 +forward 8 +up 6 +up 8 +forward 8 +forward 2 +forward 4 +down 6 +down 4 +down 2 +down 3 +forward 8 +forward 6 +down 3 +forward 7 +forward 4 +up 8 +down 9 +forward 5 +up 5 +up 5 +up 7 +forward 3 +up 1 +down 2 +forward 5 +forward 5 +up 1 +forward 4 +down 6 +up 5 +up 3 +forward 9 +down 9 +down 6 +down 1 +down 2 +down 4 +down 7 +forward 3 +up 5 +forward 2 +down 3 +forward 7 +up 8 +up 3 +forward 6 +up 7 +up 1 +up 2 +down 5 +forward 5 +down 3 +down 5 +down 6 +up 1 +down 2 +up 1 +forward 3 +down 3 +down 4 +down 6 +down 1 +down 3 +forward 9 +forward 1 +down 1 +up 3 +forward 4 +forward 7 +forward 4 +down 2 +forward 6 +forward 2 +forward 7 +down 9 +forward 8 +forward 3 +up 8 +down 9 +up 8 +forward 5 +forward 9 +down 4 +forward 1 +up 9 +forward 2 +down 6 +up 3 +forward 1 +forward 3 +forward 8 +down 7 +down 3 +down 5 +down 2 +down 2 +forward 4 +forward 1 +down 2 +up 8 +down 2 +forward 3 +down 2 +down 6 +down 1 +up 1 +down 7 +down 3 +forward 3 +forward 1 +forward 9 +down 9 +down 2 +up 1 +forward 9 +up 2 +down 2 +forward 3 +down 4 +forward 9 +forward 5 +up 5 +forward 2 +up 3 +forward 8 +down 3 +forward 5 +forward 5 +down 8 +up 9 +forward 7 +up 2 +up 2 +up 1 +up 7 +down 8 +forward 9 +forward 9 +up 6 +down 5 +forward 7 +down 9 +down 8 +down 5 +down 3 +down 2 +forward 6 +down 7 +forward 3 +up 5 +forward 1 +up 7 +forward 3 +down 5 +down 9 +down 8 +forward 2 +up 4 +forward 7 +forward 5 +forward 8 +forward 7 +up 7 +forward 4 +up 7 +down 9 +forward 1 +forward 3 +down 3 +forward 4 +down 3 +forward 3 +down 5 +down 1 +forward 6 +down 4 +down 3 +down 2 +up 1 +down 1 +down 6 +down 6 +forward 9 +down 5 +forward 1 +up 4 +forward 7 +down 8 +forward 1 +forward 9 +forward 7 +down 1 +down 3 +up 2 +down 5 +up 6 +forward 2 +up 2 +down 7 +down 9 +forward 3 +up 5 +up 7 +down 4 +forward 6 +down 8 +forward 7 +up 1 +up 4 +forward 4 +down 9 +forward 9 +forward 9 +down 3 +forward 5 +forward 1 +down 3 +down 8 +forward 7 +down 4 +forward 3 +down 3 +forward 8 +forward 2 +forward 6 +up 9 +forward 2 +down 9 +forward 2 +down 1 +forward 9 +up 1 +up 4 +up 1 +down 1 +forward 4 +up 9 +up 8 +down 1 +down 3 +down 2 +forward 9 +down 7 +down 4 +forward 2 +up 9 +down 7 +down 1 +down 9 +forward 2 +down 2 +forward 9 +down 5 +up 1 +down 3 +up 6 +down 4 +forward 8 +down 2 +down 2 +down 9 +forward 9 +forward 2 +down 1 +forward 6 +down 2 +up 4 +down 8 +up 4 +down 6 +down 2 +forward 7 +down 3 +up 3 +forward 1 +up 4 +forward 5 +down 7 +down 8 +forward 7 +forward 3 +down 5 +up 6 +down 7 +down 1 +up 7 +down 1 +forward 6 +forward 3 +forward 3 +forward 7 diff --git a/day02/main.c b/day02/main.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include "puzzle1.h" +//#include "puzzle2.h" + +int main(int argc, char *argv[]) { + printf("Advent of Code: day 02\n"); + + 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 = puzzle1(filename); + printf("Puzzle #1: %d\n", counter1); + //int counter2 = puzzle2(filename); + //printf("Puzzle #2: %d\n", counter2); + + return 0; +} diff --git a/day02/puzzle1.c b/day02/puzzle1.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <strings.h> + +#define MAX_LEN 32 +#define MAX_CMD 16 + +int puzzle1(const char *filename) { + FILE *infile = fopen(filename, "r"); + if (infile == NULL) { + fprintf(stderr, "fopen() error: %s\n", strerror(errno)); + return -1; + } + + static const char *cmd_up = "up"; + static const char *cmd_down = "down"; + static const char *cmd_forward = "forward"; + char buf[MAX_LEN] = {0}; + char cmd[MAX_CMD] = {0}; + int depth = 0; + int distance = 0; + int arg = 0; + + while (fgets(buf, MAX_LEN, infile) != NULL) { + if (sscanf(buf, "%15s %d", cmd, &arg) == 2) { + if (strncmp(cmd, cmd_up, MAX_CMD) == 0) { + depth -= arg; + } else if (strncmp(cmd, cmd_down, MAX_CMD) == 0) { + depth += arg; + } else if (strncmp(cmd, cmd_forward, MAX_CMD) == 0) { + distance += arg; + } else { + fprintf(stderr, "Invalid command string"); + break; + } + + bzero(buf, MAX_LEN); + bzero(cmd, MAX_CMD); + } + } + + // mutiny! ignoring feof/ferror. + + fclose(infile); + return depth * distance; +} diff --git a/day02/puzzle1.h b/day02/puzzle1.h @@ -0,0 +1 @@ +int puzzle1(const char *filename);