Day1
This commit is contained in:
BIN
day1/elves
Executable file
BIN
day1/elves
Executable file
Binary file not shown.
49
day1/elves.c
Normal file
49
day1/elves.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 32
|
||||||
|
#define TOP_ELVES 3
|
||||||
|
|
||||||
|
unsigned n, sum = 0U, max[TOP_ELVES];
|
||||||
|
|
||||||
|
void update_maxs(unsigned long in) {
|
||||||
|
for (int i = 0; i < TOP_ELVES; i++) {
|
||||||
|
if (in > max[i]) {
|
||||||
|
for (int j = TOP_ELVES - 1; j > i; j--) {
|
||||||
|
max[j] = max[j-1];
|
||||||
|
}
|
||||||
|
max[i] = in;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
memset(max, 0, TOP_ELVES*sizeof(unsigned));
|
||||||
|
|
||||||
|
char *buf, *p, c;
|
||||||
|
buf = (char *)malloc(BUFFER_SIZE);
|
||||||
|
memset(buf, 0, BUFFER_SIZE);
|
||||||
|
p = buf;
|
||||||
|
|
||||||
|
while ((c = getchar()) != EOF) {
|
||||||
|
*p++ = c;
|
||||||
|
if (c == '\n') {
|
||||||
|
if (buf[0] == '\n') {
|
||||||
|
update_maxs(sum);
|
||||||
|
sum = 0;
|
||||||
|
} else {
|
||||||
|
sscanf(buf, "%u", &n);
|
||||||
|
sum += n;
|
||||||
|
}
|
||||||
|
memset(buf, 0, BUFFER_SIZE);
|
||||||
|
p = buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buf);
|
||||||
|
for (int i = 0; i < TOP_ELVES; i++) {
|
||||||
|
printf("%i. %u\n", i+1, max[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
2260
day1/input.txt
Normal file
2260
day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user