diff options
author | Adam Spragg <adam@spra.gg> | 2022-12-04 16:47:21 +0000 |
---|---|---|
committer | Adam Spragg <adam@spra.gg> | 2022-12-04 16:47:21 +0000 |
commit | e0c2037ee5cede636e907e8af462371ee0e775db (patch) | |
tree | a2546a2e5cafe43ef67737d774e7ad0fd8d1d781 /1b.c |
Advent of code 2022 problems 1-4
Diffstat (limited to '1b.c')
-rw-r--r-- | 1b.c | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +#define arrlen(x) (sizeof(x) / sizeof(x[0])) + + +long +checkmax(long * maxcals, long * maxelfs, int n, int cal, int elf) +{ + long i, j; + + for (i = 0; i < n; ++i) { + if (cal > maxcals[i]) { + if ((j = i + 1) < n) { + memmove(maxcals + j, maxcals + i, (n - j) * sizeof(long)); + memmove(maxelfs + j, maxelfs + i, (n - j) * sizeof(long)); + } + maxcals[i] = cal; + maxelfs[i] = elf; + break; + } + } + + return i; +} + + +int +main() +{ + char buf[BUFSIZ]; + long cal = 0, maxcal[3] = {0}, elf = 0, maxelf[3] = {0}; + + while (fgets(buf, sizeof(buf), stdin)) { + char * pbuf = buf; + long n; + if ((n = strtol(buf, &pbuf, 10)) == 0 && pbuf == buf) { + checkmax(maxcal, maxelf, arrlen(maxcal), cal, elf); + cal = 0; + ++elf; + } + else { + cal += n; + } + } + checkmax(maxcal, maxelf, arrlen(maxcal), cal, elf); + + printf("maxcal: %ld (elf: %ld/%ld/%ld)\n", + maxcal[0] + maxcal[1] + maxcal[2], + maxelf[0], maxelf[1], maxelf[2]); + + return 0; +} + |