diff options
author | Marvin Borner | 2021-12-02 12:31:05 +0100 |
---|---|---|
committer | Marvin Borner | 2021-12-02 12:31:05 +0100 |
commit | b0fce9d8e9dcbb508a6a5c1e953882a97be1fb34 (patch) | |
tree | 64c1edebeb59a88a21330719d0ab8417e134b558 | |
parent | 057b901937b84e3b702215eb01b0b68f3dfb8274 (diff) |
Code
-rw-r--r-- | 2021/02/Makefile | 19 | ||||
-rw-r--r-- | 2021/02/input | 1000 | ||||
-rw-r--r-- | 2021/02/solve.c | 87 |
3 files changed, 1106 insertions, 0 deletions
diff --git a/2021/02/Makefile b/2021/02/Makefile new file mode 100644 index 0000000..4a881b7 --- /dev/null +++ b/2021/02/Makefile @@ -0,0 +1,19 @@ +DEBUG = -Wno-error -Og -g -s -fsanitize=undefined -fsanitize=address -fstack-protector-all +CFLAGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors -Ofast + +# Not the best makefile but idc + +debug: + @gcc $(CFLAGS) $(DEBUG) solve.c -o solve.o -L/usr/lib -lcrypto + +build: + @gcc $(CFLAGS) solve.c -o solve.o -L/usr/lib -lcrypto + +clean: + @rm -f *.o + +run: debug + @./solve.o + +time: build + @./solve.o diff --git a/2021/02/input b/2021/02/input new file mode 100644 index 0000000..2efbeb6 --- /dev/null +++ b/2021/02/input @@ -0,0 +1,1000 @@ +forward 9 +down 9 +down 6 +up 2 +down 5 +forward 3 +forward 6 +forward 8 +down 8 +up 4 +down 5 +down 4 +up 6 +down 7 +forward 5 +forward 5 +down 9 +down 5 +up 4 +forward 1 +forward 7 +down 8 +up 2 +down 1 +down 3 +up 7 +up 5 +down 8 +down 6 +forward 9 +up 8 +down 4 +forward 9 +forward 4 +forward 1 +up 9 +forward 5 +forward 1 +down 2 +down 7 +forward 5 +up 8 +forward 1 +down 9 +down 3 +forward 6 +up 6 +down 4 +forward 6 +down 3 +forward 5 +forward 1 +down 3 +forward 3 +down 8 +down 2 +forward 8 +forward 1 +up 4 +forward 4 +down 1 +down 5 +forward 2 +forward 9 +forward 5 +down 2 +up 9 +forward 2 +down 2 +down 2 +down 9 +up 8 +down 3 +forward 8 +down 4 +up 3 +down 1 +forward 7 +down 7 +forward 9 +down 2 +up 9 +up 3 +down 2 +up 2 +forward 8 +down 3 +forward 2 +forward 6 +down 5 +up 9 +forward 6 +forward 7 +down 3 +forward 1 +down 2 +forward 8 +up 5 +down 6 +forward 1 +down 8 +down 5 +forward 4 +down 5 +forward 3 +forward 5 +up 3 +down 4 +down 8 +up 7 +down 7 +forward 3 +down 2 +forward 3 +up 2 +down 1 +up 2 +forward 4 +forward 2 +down 1 +up 9 +forward 5 +forward 7 +down 6 +down 3 +down 5 +down 4 +up 4 +forward 8 +down 6 +forward 1 +forward 6 +down 8 +down 2 +down 3 +up 8 +forward 9 +down 5 +down 6 +forward 7 +down 2 +forward 6 +up 3 +up 6 +forward 2 +up 8 +up 2 +up 4 +forward 3 +forward 4 +down 7 +up 1 +forward 5 +forward 1 +forward 9 +forward 8 +up 6 +forward 4 +forward 2 +up 9 +forward 8 +down 2 +forward 4 +forward 4 +forward 6 +down 3 +forward 8 +forward 7 +forward 6 +forward 3 +down 5 +down 3 +forward 4 +forward 7 +forward 4 +forward 3 +forward 1 +forward 1 +up 1 +down 4 +up 5 +up 4 +up 8 +forward 7 +forward 4 +forward 3 +up 6 +up 4 +down 4 +up 4 +down 6 +forward 1 +forward 5 +up 3 +forward 5 +up 9 +down 9 +forward 1 +down 3 +forward 7 +up 2 +up 1 +down 8 +forward 9 +forward 3 +forward 2 +forward 6 +down 2 +forward 4 +up 9 +forward 6 +forward 8 +down 5 +down 6 +forward 7 +forward 5 +forward 6 +forward 9 +forward 5 +up 3 +forward 2 +forward 2 +forward 8 +down 5 +forward 9 +forward 7 +forward 5 +up 2 +up 7 +forward 7 +forward 9 +down 9 +up 5 +up 5 +up 1 +forward 8 +forward 3 +forward 2 +forward 4 +down 7 +down 7 +forward 7 +down 2 +forward 4 +down 6 +down 8 +down 5 +down 9 +forward 1 +forward 4 +forward 1 +forward 1 +up 5 +down 5 +up 7 +up 7 +down 6 +down 5 +forward 9 +up 7 +down 2 +down 9 +down 9 +forward 3 +up 9 +down 7 +down 2 +up 6 +down 9 +down 6 +up 3 +up 6 +forward 4 +forward 6 +down 2 +up 3 +down 3 +forward 8 +up 1 +forward 8 +forward 5 +forward 4 +forward 9 +forward 8 +up 1 +forward 2 +down 8 +up 9 +forward 3 +forward 9 +down 3 +down 8 +down 5 +up 5 +up 3 +down 5 +down 4 +down 2 +forward 1 +forward 8 +down 3 +down 1 +down 5 +down 1 +up 1 +down 4 +forward 6 +down 5 +down 7 +forward 4 +down 2 +down 2 +up 1 +forward 9 +down 2 +forward 7 +up 4 +down 8 +down 5 +up 2 +down 7 +forward 7 +up 4 +down 8 +forward 9 +up 3 +down 9 +forward 4 +forward 5 +up 6 +forward 5 +down 6 +down 3 +down 1 +down 6 +forward 7 +forward 3 +forward 4 +up 8 +forward 7 +down 3 +up 2 +up 3 +up 5 +forward 4 +forward 9 +forward 1 +down 2 +forward 4 +forward 1 +forward 6 +down 6 +forward 8 +down 2 +forward 7 +down 5 +forward 6 +down 4 +down 8 +down 6 +forward 9 +forward 2 +forward 4 +forward 9 +down 1 +down 9 +forward 2 +down 3 +forward 2 +down 4 +down 6 +up 5 +forward 9 +forward 1 +down 1 +forward 8 +forward 7 +forward 2 +forward 4 +forward 3 +up 8 +down 4 +forward 5 +up 5 +forward 4 +up 5 +forward 7 +up 7 +forward 5 +down 3 +up 9 +up 2 +forward 9 +forward 8 +forward 7 +down 8 +forward 8 +down 2 +forward 7 +forward 4 +down 7 +up 4 +forward 2 +forward 1 +up 5 +forward 5 +down 5 +down 1 +down 7 +up 6 +down 5 +down 6 +forward 2 +forward 2 +forward 8 +down 9 +down 4 +down 3 +forward 7 +forward 6 +forward 5 +up 9 +forward 8 +forward 9 +forward 4 +down 9 +down 6 +down 2 +down 7 +up 3 +forward 3 +forward 5 +up 6 +down 7 +down 3 +forward 3 +down 5 +down 2 +down 4 +up 4 +down 5 +forward 3 +forward 1 +forward 7 +down 7 +forward 8 +up 7 +forward 3 +forward 8 +down 9 +down 1 +forward 4 +forward 4 +forward 5 +forward 6 +down 5 +down 6 +forward 6 +down 5 +down 7 +forward 2 +down 1 +down 8 +down 3 +down 4 +forward 9 +forward 4 +down 4 +up 4 +down 3 +forward 5 +down 4 +forward 3 +forward 9 +up 7 +down 5 +down 1 +forward 8 +down 2 +forward 9 +forward 6 +forward 3 +down 7 +forward 2 +forward 5 +down 6 +forward 1 +forward 7 +up 1 +forward 3 +forward 7 +forward 3 +forward 5 +down 5 +forward 6 +forward 9 +up 4 +forward 6 +forward 3 +down 9 +up 4 +up 6 +down 5 +down 8 +down 2 +down 9 +forward 8 +up 8 +forward 3 +forward 8 +up 8 +down 5 +down 9 +down 1 +down 3 +forward 3 +forward 9 +up 9 +down 5 +down 6 +down 1 +forward 9 +up 3 +forward 3 +forward 9 +down 7 +down 4 +down 5 +up 5 +forward 2 +down 5 +up 5 +up 4 +up 5 +forward 1 +down 2 +forward 7 +down 9 +forward 8 +forward 7 +forward 5 +down 3 +forward 5 +forward 7 +forward 1 +up 5 +forward 3 +forward 1 +down 1 +up 1 +down 7 +up 4 +forward 8 +up 9 +down 9 +down 9 +forward 8 +forward 3 +forward 3 +up 4 +down 3 +forward 4 +forward 6 +forward 7 +down 2 +forward 5 +down 3 +down 9 +down 4 +down 4 +forward 3 +down 3 +down 6 +up 5 +up 4 +down 6 +up 9 +down 9 +up 2 +up 1 +down 3 +forward 6 +up 6 +forward 1 +down 9 +down 6 +down 8 +down 9 +down 6 +forward 4 +up 6 +down 2 +forward 9 +forward 2 +down 4 +up 3 +forward 6 +down 2 +down 7 +down 1 +up 6 +down 5 +forward 1 +forward 7 +up 9 +forward 2 +up 8 +down 4 +forward 8 +up 2 +up 2 +down 1 +forward 6 +down 3 +forward 7 +forward 2 +down 5 +down 6 +up 8 +forward 4 +up 2 +forward 7 +forward 4 +forward 2 +down 1 +forward 7 +down 1 +up 1 +down 9 +forward 5 +forward 3 +down 6 +up 5 +up 9 +down 1 +forward 2 +forward 8 +down 5 +down 4 +forward 6 +forward 1 +forward 4 +forward 2 +forward 4 +up 7 +forward 5 +forward 2 +down 2 +forward 1 +forward 4 +down 4 +forward 7 +down 6 +forward 9 +forward 9 +forward 3 +up 5 +up 5 +down 6 +forward 7 +forward 8 +down 5 +forward 6 +forward 5 +forward 1 +forward 7 +forward 6 +up 2 +up 4 +forward 6 +down 4 +down 9 +forward 9 +down 4 +up 2 +down 5 +forward 9 +down 7 +forward 9 +up 9 +forward 9 +up 7 +up 2 +up 6 +forward 1 +up 9 +down 4 +down 8 +forward 8 +forward 7 +up 5 +forward 4 +down 3 +down 5 +up 9 +up 5 +forward 1 +down 3 +down 9 +up 2 +forward 5 +down 8 +down 3 +forward 5 +up 8 +forward 1 +forward 8 +forward 6 +down 2 +down 8 +down 8 +down 8 +forward 8 +forward 9 +up 2 +up 9 +up 8 +forward 2 +down 4 +up 7 +down 3 +forward 1 +forward 7 +down 8 +down 2 +forward 6 +forward 2 +down 6 +down 6 +up 9 +forward 9 +down 7 +forward 7 +down 4 +up 2 +up 7 +down 8 +forward 6 +up 2 +forward 4 +forward 9 +down 7 +forward 4 +up 4 +forward 8 +down 4 +forward 6 +forward 8 +down 2 +down 6 +up 4 +forward 5 +forward 1 +down 2 +up 6 +down 6 +down 3 +up 2 +up 4 +forward 1 +up 3 +up 5 +down 4 +down 1 +down 7 +down 7 +down 4 +forward 7 +down 9 +down 4 +down 2 +down 7 +forward 7 +forward 8 +forward 1 +down 8 +down 3 +down 5 +down 8 +forward 7 +forward 9 +forward 3 +up 8 +forward 5 +up 5 +up 2 +forward 4 +forward 6 +down 8 +down 1 +up 6 +forward 5 +forward 5 +down 9 +forward 3 +up 1 +down 1 +forward 5 +forward 5 +up 5 +down 4 +up 6 +forward 8 +forward 6 +up 5 +forward 2 +up 3 +forward 9 +forward 5 +down 9 +forward 3 +forward 1 +forward 4 +up 5 +forward 3 +forward 3 +up 9 +down 2 +down 3 +down 1 +forward 8 +down 4 +forward 8 +forward 8 +down 8 +forward 9 +forward 8 +forward 7 +down 6 +forward 5 +down 6 +up 8 +forward 6 +down 8 +forward 8 +down 6 +up 2 +forward 5 +forward 1 +up 2 +forward 4 +down 5 +up 8 +up 3 +forward 8 +down 3 +forward 4 +up 5 +forward 2 +down 3 +down 2 +down 7 +forward 5 +down 5 +forward 2 +up 1 +down 3 +down 9 +up 4 +up 3 +down 8 +up 1 +down 5 +down 5 +forward 7 +up 2 +down 3 +up 3 +forward 1 +forward 4 +forward 3 +up 5 +down 5 +up 3 +down 2 +forward 6 +down 6 +up 3 +forward 9 +down 9 +up 4 +down 4 +down 6 +up 9 +forward 3 +forward 2 +up 2 +up 5 +forward 2 +up 5 +forward 7 +down 8 +down 7 +up 8 +down 1 +up 1 +up 9 +forward 7 +forward 5 +down 9 +forward 8 +down 8 +forward 4 +forward 1 +forward 2 +down 8 +forward 3 +forward 6 +down 9 +forward 2 +forward 9 +down 4 +down 1 +down 1 +down 8 +down 1 +down 4 +up 3 +down 7 +down 5 +forward 1 +forward 5 +forward 8 +forward 4 +down 7 +down 6 +forward 2 +down 3 +down 3 +down 3 +forward 5 +forward 7 +down 4 +forward 6 +forward 9 +down 1 +forward 7 +down 5 +up 4 +up 5 +down 3 +down 1 +up 5 +forward 4 +down 9 +down 7 +up 1 +up 9 +up 4 +forward 7 +down 5 +down 8 +forward 5 +forward 9 +down 2 +down 7 +forward 8 +forward 7 +down 6 +down 8 +forward 3 +forward 6 +down 5 +forward 7 +up 7 +down 8 +forward 9 +down 3 +up 3 +forward 4 +up 4 +forward 3 +forward 4 +forward 9 +forward 5 +down 3 +down 6 +forward 9 +forward 4 +forward 1 +forward 7 +forward 8 +forward 2 +forward 7 +up 9 +down 8 +forward 4 +forward 4 +down 5 +forward 2 +forward 5 +down 6 +forward 4 +forward 2 +forward 7 +down 6 +down 2 +down 5 +down 1 +forward 5 diff --git a/2021/02/solve.c b/2021/02/solve.c new file mode 100644 index 0000000..c2773d7 --- /dev/null +++ b/2021/02/solve.c @@ -0,0 +1,87 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#define FORLINE \ + char *line = NULL; \ + size_t len = 0; \ + while (getline(&line, &len, fp) != EOF) +#define FREELINE \ + if (line) \ + free(line) + +#define FORCH \ + char ch = 0; \ + while ((ch = fgetc(fp)) != EOF) + +#define WHOLE \ + fseek(fp, 0, SEEK_END); \ + long fsize = ftell(fp); \ + fseek(fp, 0, SEEK_SET); \ + char *data = malloc(fsize + 1); \ + fread(data, 1, fsize, fp); \ + data[fsize] = 0; \ + data[fsize--] = 0 + +static int part_one(FILE *fp) +{ + int depth = 0, horizontal = 0; + FORLINE + { + char command[32] = { 0 }; + int amount; + sscanf(line, "%s %d", command, &amount); + if (!strcmp(command, "forward")) + horizontal += amount; + else if (!strcmp(command, "down")) + depth += amount; + else if (!strcmp(command, "up")) + depth -= amount; + } + FREELINE; + + return depth * horizontal; +} + +static int part_two(FILE *fp) +{ + int aim = 0, depth = 0, horizontal = 0; + FORLINE + { + char command[32] = { 0 }; + int amount; + sscanf(line, "%s %d", command, &amount); + if (!strcmp(command, "forward")) { + horizontal += amount; + depth += aim * amount; + } else if (!strcmp(command, "down")) { + aim += amount; + } else if (!strcmp(command, "up")) { + aim -= amount; + } + } + FREELINE; + + return depth * horizontal; +} + +int main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + clock_t tic = clock(); + printf("%d\n", part_one(fp)); + rewind(fp); + printf("%d\n", part_two(fp)); + clock_t toc = clock(); + printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); + + fclose(fp); + return 0; +} |