aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2022/01/2.c
blob: 1ab897f0900a8fb19ec01f830a31cef4ca0156aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSZ 1024

char buf[BUFSZ];

int maxheap[3] = {0, 0, 0};

void push(int val) {
    int minidx = 0;
    for (int i = 0; i < 3; i++) {
        if (maxheap[i] < maxheap[minidx]) {
            minidx = i;
        }
    }
    if (maxheap[minidx] < val) {
        maxheap[minidx] = val;
    }
}

int main() {
    int maxval = 0;
    int cur = 0;
    FILE* fp = fopen("input", "r");
    while (fgets(buf, BUFSZ, fp)) {
        int len = strlen(buf);
        char *end;
        if (len <= 1) {
            maxval = cur > maxval ? cur : maxval;
            push(cur);
            cur = 0;
        } else {
            cur += strtol(buf, &end, 10);
        }
    }
    printf("%d\n", maxheap[0] + maxheap[1] + maxheap[2]);
    return 0;
}