aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2022/01/2.c
diff options
context:
space:
mode:
Diffstat (limited to 'advent-of-code/2022/01/2.c')
-rw-r--r--advent-of-code/2022/01/2.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/advent-of-code/2022/01/2.c b/advent-of-code/2022/01/2.c
new file mode 100644
index 0000000..1ab897f
--- /dev/null
+++ b/advent-of-code/2022/01/2.c
@@ -0,0 +1,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;
+}
+