Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created December 2, 2022 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maksverver/45516397874abbd7fcfdb4c1a32be798 to your computer and use it in GitHub Desktop.
Save maksverver/45516397874abbd7fcfdb4c1a32be798 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
static void update(int top[], int k, int value) {
if (value < top[k - 1]) return;
int i = k - 1;
while (i > 0 && value > top[i - 1]) {
top[i] = top[i - 1];
--i;
}
top[i] = value;
}
static char buf[16 << 20];
int main() {
int top[3] = {0, 0, 0};
bool start = true; // at start of line?
int num = 0; // current number
int sum = 0; // current sum
size_t len;
while ((len = fread(buf, 1, sizeof(buf), stdin)) != 0) {
for (char *p = buf, *end = buf + len; p != end; ++p) {
if (*p == '\n') {
if (start) {
// New group
update(top, 3, sum);
sum = 0;
} else {
// End of number.
sum += num;
num = 0;
start = true;
}
} else {
// Next digit.
//assert(*p >= '0' && *p <= '9');
num = 10*num + (*p - '0');
start = false;
}
}
}
assert(feof(stdin));
update(top, 3, sum);
printf("%d\n", top[0]); // part 1: top sum
printf("%d\n", top[0] + top[1] + top[2]); // part 2: sum of top 3 sums
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment