diff options
author | Marvin Borner | 2021-12-24 17:37:34 +0100 |
---|---|---|
committer | Marvin Borner | 2021-12-24 17:37:34 +0100 |
commit | 2ca27006c504ea4acfddaa1774924e009db64971 (patch) | |
tree | 1ff2d9bf71c32ab8d89eda3fde5c261c115684bb /2021/24/solve.c | |
parent | 58f86002e905a8123bd94df8078e0ce697f4fe26 (diff) |
yay
Diffstat (limited to '2021/24/solve.c')
-rw-r--r-- | 2021/24/solve.c | 110 |
1 files changed, 77 insertions, 33 deletions
diff --git a/2021/24/solve.c b/2021/24/solve.c index 72bf9de..8eff5f3 100644 --- a/2021/24/solve.c +++ b/2021/24/solve.c @@ -1,45 +1,89 @@ -#include <math.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <time.h> -// Non-trial-and-error solution in solve.js +#define COUNT(a) ((int)(sizeof(a) / sizeof 0 [a])) -static const int xs[] = { 14, 14, 14, 12, 15, -12, -12, 12, -7, 13, -8, -5, -10, -7 }; -static const int ys[] = { 14, 2, 1, 13, 5, 5, 5, 9, 3, 13, 2, 1, 11, 8 }; -static const int zs[] = { 1, 1, 1, 1, 1, 26, 26, 1, 26, 1, 26, 26, 26, 26 }; +#define FORLINE \ + char *line = NULL; \ + size_t len = 0; \ + while (getline(&line, &len, fp) != EOF) +#define FREELINE \ + if (line) \ + free(line) -int main(int argc, char *argv[]) +typedef char block[17][16]; + +struct data { + int idx; + int value; +}; + +typedef char out[15]; + +static void solve(FILE *fp) { - srand(time(0)); - long w = 19599999999999; - - char ws[15]; - while (1) { - sprintf(ws, "%lu", w); - - long x = 0, y = 0, z = 0; - for (int i = 0; i < 14; i++) { - x = z; - x %= 26; - z /= zs[i]; - x += xs[i]; - x = (x == (ws[i] - '0')) ? 0 : 1; - y = 25; - y *= x; - y += 1; - z *= y; - y = (ws[i] - '0') + ys[i]; - y *= x; - z += y; + block blocks[14] = { 0 }; + int block_idx = -1; + int line_idx = 0; + FORLINE + { + if (line[0] == 'i') { + block_idx++; + line_idx = 0; + continue; + } + + strcpy(blocks[block_idx][line_idx++], line); + }; + FREELINE; + + out min = { 0 }, max = { 0 }; + + struct data arr[100] = { 0 }; + int idx = 0; + + for (int i = 0; i < COUNT(blocks); i++) { + if (blocks[i][3][6] == '1') { + int num; + sscanf(blocks[i][14], "add y %d", &num); + arr[idx++] = (struct data){ i, num }; + continue; } - if (z == 0) { - printf("Found %ld %ld %ld -> %ld\n", w, x, y, z); - break; + + int num; + sscanf(blocks[i][4], "add x %d", &num); + struct data data = arr[--idx]; + int d = data.value + num, j = i; + if (d < 0) { + j = data.idx; + data.idx = i; + d *= -1; } - if (w % 100000 == 0) - printf("%ld %ld\n", w, z); - w--; + max[j] = '9'; + max[data.idx] = 9 - d + '0'; + min[j] = 1 + d + '0'; + min[data.idx] = '1'; } + + printf("%s\n%s\n", max, min); +} + +int main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + clock_t tic = clock(); + solve(fp); + clock_t toc = clock(); + printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); + + fclose(fp); return 0; } |