diff options
author | Marvin Borner | 2020-12-22 10:57:31 +0100 |
---|---|---|
committer | Marvin Borner | 2020-12-22 10:57:31 +0100 |
commit | f39eb66428a0829049da04e0b47a2653a6d553c2 (patch) | |
tree | 07ba2a84a5cd5e1be27e40de9cc2d02a48db7be9 /2020/22 | |
parent | f4adb9df46a64f274afd9decec465e2d89a5e516 (diff) |
ez
Diffstat (limited to '2020/22')
-rw-r--r-- | 2020/22/Makefile | 10 | ||||
-rw-r--r-- | 2020/22/input | 53 | ||||
-rw-r--r-- | 2020/22/solve.c | 86 |
3 files changed, 149 insertions, 0 deletions
diff --git a/2020/22/Makefile b/2020/22/Makefile new file mode 100644 index 0000000..fc2ff1a --- /dev/null +++ b/2020/22/Makefile @@ -0,0 +1,10 @@ +.PHONY: solve.c + +solve.o: solve.c + @gcc -Ofast $+ -o $@ + +clean: + @rm -f *.o + +run: solve.o + @./solve.o diff --git a/2020/22/input b/2020/22/input new file mode 100644 index 0000000..3a7ee9a --- /dev/null +++ b/2020/22/input @@ -0,0 +1,53 @@ +Player 1: +50 +19 +40 +22 +7 +4 +3 +16 +34 +45 +46 +39 +44 +32 +20 +29 +15 +35 +41 +2 +21 +28 +6 +26 +48 + +Player 2: +14 +9 +37 +47 +38 +27 +30 +24 +36 +31 +43 +42 +11 +17 +18 +10 +12 +5 +33 +25 +8 +23 +1 +13 +49 diff --git a/2020/22/solve.c b/2020/22/solve.c new file mode 100644 index 0000000..ce8c992 --- /dev/null +++ b/2020/22/solve.c @@ -0,0 +1,86 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +long part_one(FILE *fp) +{ + long res = 0; + + int player_one[512] = { 0 }, player_two[512] = { 0 }; + int one_low = 0, one_high = 0, two_low = 0, two_high = 0; + + char *line = NULL; + size_t len; + int paragraph = 0; + int index = 0; + while (getline(&line, &len, fp) != -1) { + if (line[0] == '\n') { + index = 0; + paragraph++; + continue; + } else if (line[0] == 'P') { + continue; + } else { + int num; + sscanf(line, "%d", &num); + if (paragraph == 0) + player_one[index++] = num; + else + player_two[index++] = num; + } + } + + one_high = two_high = index; + + int cnt = 0; + while (1) { + int c1 = player_one[one_low++]; + int c2 = player_two[two_low++]; + cnt++; + + if (!c1 || !c2) + break; + + if (c1 > c2) { + player_one[one_high++] = c1; + player_one[one_high++] = c2; + } else { + player_two[two_high++] = c2; + player_two[two_high++] = c1; + } + } + + int *winner = one_low == one_high + 1 ? player_two : player_one; + int low = one_low == one_high + 1 ? two_low : one_low; + int high = one_low == one_high + 1 ? two_high : one_high; + + for (int i = high - low + 1, j = 0; i > 0; i--, j++) + res += i * winner[two_low + j - 1]; + + return res; +} + +long part_two(FILE *fp) +{ + long res = 0; + + return res; +} + +int main(int argc, char *argv[]) +{ + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + clock_t tic = clock(); + printf("%lu\n", part_one(fp)); + rewind(fp); + printf("%lu\n", part_two(fp)); + clock_t toc = clock(); + printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); + + fclose(fp); + return 0; +} |