diff options
author | Marvin Borner | 2021-12-07 10:45:06 +0100 |
---|---|---|
committer | Marvin Borner | 2021-12-07 10:45:06 +0100 |
commit | 6d2c90d4f74bad1a99b80145be55e84461a8a307 (patch) | |
tree | 442d652461cc105720399ea0b309c52a7de1104b /2021/07/solve.c | |
parent | 1a81f0925c8ae7fccf8826d8d0378378e0f38129 (diff) |
yee
Diffstat (limited to '2021/07/solve.c')
-rw-r--r-- | 2021/07/solve.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/2021/07/solve.c b/2021/07/solve.c new file mode 100644 index 0000000..6e399c9 --- /dev/null +++ b/2021/07/solve.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +#define ABS(a) ((a) < 0 ? -(a) : (a)) + +static void solve(FILE *fp) +{ + int crabs[1024] = { 0 }; + long cnt = 0; + char ch; + int pos, max = 0; + while (fscanf(fp, "%d%c", &pos, &ch) != EOF && (ch == ',' || ch == '\n')) { + crabs[cnt++] = pos; + if (pos > max) + max = pos; + } + + int fuel1 = 0xfffffff, fuel2 = fuel1; + for (int i = 0; i < max; i++) { + int a = 0, b = 0; + for (int j = 0; j < cnt; j++) { + a += ABS(crabs[j] - i); + int d = ABS(crabs[j] - i); + b += (d * (d + 1)) / 2; + } + + if (a < fuel1) + fuel1 = a; + + if (b < fuel2) + fuel2 = b; + } + + printf("%d\n%d\n", fuel1, fuel2); +} + +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; +} |