From 3aa641542918765f6ceb039cfa64ff6f58597eb2 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 30 Nov 2021 21:53:46 +0100 Subject: Hype for tomorrow --- 2015/03/solve.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 2015/03/solve.c (limited to '2015/03/solve.c') diff --git a/2015/03/solve.c b/2015/03/solve.c new file mode 100644 index 0000000..8f4e9ad --- /dev/null +++ b/2015/03/solve.c @@ -0,0 +1,97 @@ +#include +#include +#include + +#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 INFINITE 1024 +static int part_one(FILE *fp) +{ + int res = 1; + char houses[INFINITE * 2][INFINITE * 2] = { 0 }; + int x = INFINITE, y = INFINITE; + + houses[x][y] = 1; + FORCH + { + if (ch == '>') + x++; + else if (ch == '<') + x--; + else if (ch == '^') + y++; + else if (ch == 'v') + y--; + + if (!houses[x][y]) { + houses[x][y] = 1; + res++; + } + } + + return res; +} + +static int part_two(FILE *fp) +{ + int res = 1; + + char houses[INFINITE * 2][INFINITE * 2] = { 0 }; + int rx = INFINITE, ry = INFINITE, sx = INFINITE, sy = INFINITE; + + houses[sx][sy] = 1; + int robo = 0; + FORCH + { + int *x = robo ? &rx : &sx; + int *y = robo ? &ry : &sy; + + if (ch == '>') + (*x)++; + else if (ch == '<') + (*x)--; + else if (ch == '^') + (*y)++; + else if (ch == 'v') + (*y)--; + + if (!houses[*x][*y]) { + houses[*x][*y] = 1; + res++; + } + + robo = !robo; + } + + return res; +} + +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; +} -- cgit v1.2.3