diff options
author | Marvin Borner | 2020-12-15 18:15:15 +0100 |
---|---|---|
committer | Marvin Borner | 2020-12-15 18:15:15 +0100 |
commit | fe5fd818f26fd5bc5e5738917dfc9907c51d2963 (patch) | |
tree | 215382ce34cb4b95da08f2b76070b40064d93eeb /2020/15 | |
parent | 53505c93aed2f42eef45a3523320e5097b3dcb9c (diff) |
Very inefficient solution (hashmap?)
Diffstat (limited to '2020/15')
-rw-r--r-- | 2020/15/Makefile | 10 | ||||
-rw-r--r-- | 2020/15/input | 1 | ||||
-rw-r--r-- | 2020/15/solve.c | 45 |
3 files changed, 56 insertions, 0 deletions
diff --git a/2020/15/Makefile b/2020/15/Makefile new file mode 100644 index 0000000..9c7ab36 --- /dev/null +++ b/2020/15/Makefile @@ -0,0 +1,10 @@ +.PHONY: solve.c + +solve.o: solve.c + @gcc -g -Ofast $+ -o $@ + +clean: + @rm -f *.o + +run: solve.o + @./solve.o diff --git a/2020/15/input b/2020/15/input new file mode 100644 index 0000000..909319b --- /dev/null +++ b/2020/15/input @@ -0,0 +1 @@ +19,20,14,0,9,1 diff --git a/2020/15/solve.c b/2020/15/solve.c new file mode 100644 index 0000000..0707208 --- /dev/null +++ b/2020/15/solve.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#define MAX_NUMS 30000000 + +static int nums[MAX_NUMS] = { 0 }; + +int part_one(FILE *fp, int count) +{ + long size = 0; + while (size < count) + if (fscanf(fp, "%d,", &nums[size++]) != 1) + break; + + for (int i = size - 2; i < count - 1; i++) { + for (int j = i - 1; j >= 0; j--) { + if (nums[j] == nums[i]) { + nums[i + 1] = i - j; + break; + } + } + } + + return nums[count - 1]; +} + +int main(int argc, char *argv[]) +{ + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + clock_t tic = clock(); + printf("%d\n", part_one(fp, 2020)); + memset(nums, 0, 2020); + rewind(fp); + printf("%d\n", part_one(fp, 30000000)); + clock_t toc = clock(); + printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); + + fclose(fp); + return 0; +} |