diff options
author | Marvin Borner | 2020-12-12 15:30:32 +0100 |
---|---|---|
committer | Marvin Borner | 2020-12-12 15:30:32 +0100 |
commit | 99805b864c6c1e94946ea197b4917d9c42ed33bc (patch) | |
tree | 0954f2f54daed18fc0fc0efd373107a1a5909485 | |
parent | 51a1115dc9f44286c6c19d48ce5261147792e8c4 (diff) |
Opened 12th door
-rw-r--r-- | 2020/12/Makefile | 10 | ||||
-rw-r--r-- | 2020/12/input | 783 | ||||
-rw-r--r-- | 2020/12/solve.c | 145 |
3 files changed, 938 insertions, 0 deletions
diff --git a/2020/12/Makefile b/2020/12/Makefile new file mode 100644 index 0000000..fc2ff1a --- /dev/null +++ b/2020/12/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/12/input b/2020/12/input new file mode 100644 index 0000000..375baf2 --- /dev/null +++ b/2020/12/input @@ -0,0 +1,783 @@ +N4 +F85 +L90 +N4 +E3 +L90 +E1 +L90 +W4 +R270 +S5 +F54 +N5 +W4 +L90 +N1 +F55 +L90 +S2 +N2 +L180 +F97 +W1 +F55 +N1 +L180 +F45 +F49 +L90 +F76 +R90 +S3 +F35 +N3 +W1 +S4 +R90 +S4 +F83 +L90 +S1 +F41 +S2 +R90 +F98 +N5 +F51 +L90 +N4 +W1 +F73 +W5 +F34 +S3 +R180 +F85 +N1 +F74 +W4 +F97 +L270 +F65 +W5 +R90 +S1 +W5 +L90 +W2 +N4 +N1 +F81 +S3 +L270 +F96 +W2 +F39 +F27 +W4 +L180 +F75 +N4 +W2 +F38 +R90 +W4 +F100 +R180 +E5 +R180 +E5 +R90 +S5 +L90 +F17 +W3 +N5 +F9 +N1 +W4 +F80 +S1 +W4 +F50 +R180 +W4 +L90 +E4 +F78 +N3 +F86 +W2 +R90 +F46 +S3 +W5 +L180 +S1 +L90 +L90 +F54 +R90 +N5 +E5 +F83 +S4 +W3 +R180 +E4 +N5 +E5 +F90 +W1 +S3 +W5 +F91 +L90 +N1 +F22 +E4 +S2 +F65 +R90 +F16 +R90 +E4 +R180 +W3 +R90 +F94 +L180 +F2 +W1 +R180 +N1 +F59 +R270 +E2 +R90 +S3 +S3 +R90 +F52 +N2 +L90 +R90 +E5 +L90 +R180 +S3 +F26 +E4 +S1 +W5 +R90 +E5 +F69 +E2 +L90 +S2 +E3 +S5 +W2 +S3 +W3 +F78 +N5 +E5 +F87 +L90 +W5 +S1 +L90 +F21 +L90 +F80 +W2 +N4 +W2 +L270 +F52 +L90 +R90 +N3 +F29 +L90 +F20 +R90 +W1 +R90 +E1 +F100 +W1 +S4 +E3 +S3 +F73 +S4 +L90 +S5 +W4 +R180 +E4 +F32 +S1 +R270 +E4 +S5 +L90 +F64 +F12 +E5 +F59 +E4 +F85 +N2 +F27 +R90 +W4 +N2 +F7 +N2 +E3 +F70 +W1 +R180 +F50 +R270 +F43 +L90 +E3 +F23 +W4 +N2 +F15 +L180 +N4 +F31 +R90 +F35 +N1 +E2 +S4 +E4 +N1 +F98 +S2 +W5 +N5 +R90 +F59 +W2 +F22 +S1 +S4 +W5 +F10 +L90 +E3 +R90 +W4 +F11 +N3 +L90 +F12 +S1 +R90 +F25 +E3 +E1 +R90 +F66 +S5 +E1 +F33 +W1 +F57 +L90 +N4 +F44 +W4 +L90 +N5 +L270 +F25 +S3 +F20 +L180 +E4 +R90 +F49 +E4 +S1 +F96 +W2 +R90 +F26 +L180 +F78 +L180 +F88 +W2 +N4 +E1 +F12 +E4 +F81 +E1 +F97 +S3 +E2 +F21 +R180 +S4 +E1 +S4 +E5 +R90 +E4 +S2 +S1 +L90 +L90 +W1 +S5 +L180 +S2 +W2 +S2 +E5 +N5 +W5 +F57 +R270 +N1 +W3 +N5 +F47 +R180 +F28 +S1 +E1 +L180 +F34 +N1 +F96 +S5 +W2 +L90 +S5 +F48 +L90 +E4 +S5 +F24 +L90 +F95 +W1 +R180 +E2 +F76 +R90 +N1 +F51 +S3 +E4 +F24 +S2 +E5 +R90 +E1 +R90 +S4 +E3 +F70 +S1 +E4 +F43 +R180 +F84 +E2 +R90 +F70 +E1 +R90 +F85 +E2 +L90 +F29 +S4 +F73 +F25 +S5 +R180 +S2 +F66 +S3 +F5 +F52 +N3 +L180 +E5 +F14 +W5 +S5 +F81 +N4 +F67 +E3 +R180 +S1 +W4 +F96 +F100 +L270 +F65 +R90 +W2 +S5 +E3 +F32 +F61 +L180 +F57 +N2 +L90 +F10 +F57 +N2 +R90 +F34 +E5 +F13 +E3 +N2 +L90 +N2 +F68 +N3 +W1 +F49 +L90 +F91 +E3 +F84 +F73 +L90 +S1 +E3 +S4 +F66 +L90 +N3 +E2 +S1 +L180 +S5 +L90 +S4 +E4 +S1 +L180 +R90 +S2 +W4 +F59 +L90 +F6 +R90 +W2 +S5 +R180 +S1 +S2 +F96 +W1 +R180 +F87 +R90 +F67 +W5 +L270 +E4 +F51 +R180 +S4 +L90 +F59 +E5 +F66 +E2 +S1 +E4 +F79 +S1 +L180 +F41 +R270 +F66 +L90 +F90 +E4 +S2 +R180 +W2 +F4 +L90 +S5 +L90 +F18 +L90 +F90 +R90 +N4 +E5 +R90 +E4 +S2 +W3 +F97 +W4 +F53 +L90 +S5 +F61 +S2 +F72 +E4 +R90 +S2 +R90 +S2 +F56 +W2 +R180 +W3 +L90 +F31 +F65 +S2 +F11 +N5 +W4 +L270 +E2 +N4 +L90 +F90 +L90 +N3 +L90 +E3 +L90 +F8 +S2 +F63 +L270 +E4 +F51 +F40 +L90 +F34 +W1 +L180 +F79 +N5 +R90 +L90 +W3 +L180 +W1 +L180 +E1 +R90 +N2 +E5 +S1 +R90 +E3 +S2 +E2 +F56 +S2 +W3 +F95 +R90 +N4 +R270 +W4 +S5 +F33 +L180 +N3 +F95 +N1 +E2 +S3 +F4 +L90 +F66 +S1 +R90 +E2 +S3 +F11 +R90 +S5 +N1 +E2 +F64 +N2 +W5 +S2 +F2 +N1 +R90 +N4 +L180 +N1 +F95 +W5 +F99 +F6 +N4 +F69 +S2 +E4 +F49 +R90 +F91 +F76 +R90 +S1 +E2 +S3 +F79 +W4 +L90 +F18 +S5 +R90 +E4 +S1 +F91 +N3 +F40 +E2 +L90 +W2 +S4 +R90 +S1 +R90 +F59 +W3 +N2 +W1 +F86 +R90 +F32 +S1 +F22 +S2 +F4 +S3 +E5 +S4 +S4 +E5 +S4 +F36 +E4 +L90 +F35 +L180 +N1 +W1 +L90 +N4 +R180 +R90 +F22 +R180 +E2 +S2 +W5 +F99 +E2 +S3 +F22 +E1 +L180 +S2 +L180 +W4 +R270 +F26 +N5 +E2 +F89 +E5 +L90 +N4 +R90 +W4 +L270 +W5 +N5 +W4 +S2 +E3 +N4 +W1 +F95 +W1 +F10 +N3 +R90 +W1 +F73 +W1 +N3 +F33 +L180 +E3 +R90 +R90 +N2 +E4 +E5 +W4 +N1 +F91 +N1 +W1 +F49 +S2 +E5 +S3 +F43 +W5 +F34 +E3 +E1 +N1 +E3 +L180 +W2 +F27 +L180 +E5 +F28 +R90 +W1 +L90 +F99 +S2 +F48 +W1 +W1 +R180 +W1 +L180 +F35 diff --git a/2020/12/solve.c b/2020/12/solve.c new file mode 100644 index 0000000..111f689 --- /dev/null +++ b/2020/12/solve.c @@ -0,0 +1,145 @@ +#include <stdio.h> +#include <stdlib.h> + +enum dir { N, E, S, W }; + +// I <3 recursion +enum dir rotate(enum dir dir, int cnt, int r) +{ + if (!cnt) + return dir; + + if (r) { // right + if (dir == W) + dir = N; + else + dir++; + } else { // left + if (dir == N) + dir = W; + else + dir--; + } + return rotate(dir, cnt - 1, r); +} + +// Quite inefficient but I like this approach. +int *rotate_wp(int dirs[], int cnt, int r) +{ + if (!cnt) + return dirs; + + if (r) { + int tmp = dirs[W]; + dirs[W] = dirs[S]; + dirs[S] = dirs[E]; + dirs[E] = dirs[N]; + dirs[N] = tmp; + } else { + int tmp = dirs[N]; + dirs[N] = dirs[E]; + dirs[E] = dirs[S]; + dirs[S] = dirs[W]; + dirs[W] = tmp; + } + + return rotate_wp(dirs, cnt - 1, r); +} + +int part_one(FILE *fp) +{ + char *line = NULL; + size_t len = 0; + int coords[4] = { 0 }; + enum dir cur = E; + while (getline(&line, &len, fp) != -1) { + char dir; + int cnt; + sscanf(line, "%c%d\n", &dir, &cnt); + switch (dir) { + case 'N': + coords[N] += cnt; + break; + case 'E': + coords[E] += cnt; + break; + case 'S': + coords[S] += cnt; + break; + case 'W': + coords[W] += cnt; + break; + case 'L': + cur = rotate(cur, cnt / 90, 0); + break; + case 'R': + cur = rotate(cur, cnt / 90, 1); + break; + case 'F': + coords[cur] += cnt; + break; + default: + break; + } + } + + return abs((coords[E] - coords[W]) + (coords[S] - coords[N])); +} + +int part_two(FILE *fp) +{ + char *line = NULL; + size_t len = 0; + int wp[4] = { 1, 10, 0, 0 }; + int coords[4] = { 0 }; + enum dir cur = E; + while (getline(&line, &len, fp) != -1) { + char dir; + int cnt; + sscanf(line, "%c%d\n", &dir, &cnt); + switch (dir) { + case 'N': + wp[N] += cnt; + break; + case 'E': + wp[E] += cnt; + break; + case 'S': + wp[S] += cnt; + break; + case 'W': + wp[W] += cnt; + break; + case 'L': + rotate_wp(wp, cnt / 90, 0); + break; + case 'R': + rotate_wp(wp, cnt / 90, 1); + break; + case 'F': + coords[N] += cnt * wp[N]; + coords[E] += cnt * wp[E]; + coords[S] += cnt * wp[S]; + coords[W] += cnt * wp[W]; + break; + default: + break; + } + } + + return abs((coords[E] - coords[W])) + abs((coords[S] - coords[N])); +} + +int main(int argc, char *argv[]) +{ + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + printf("%d\n", part_one(fp)); + rewind(fp); + printf("%d\n", part_two(fp)); + + fclose(fp); + return 0; +} |