2022-01
This commit is contained in:
@ -1,10 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "lfinput.h"
|
||||
#include "lflinkedlist.h"
|
||||
|
||||
static int cmp(const void* a, const void* b) {
|
||||
return (*(int*)a - *(int*)b);
|
||||
}
|
||||
|
||||
void advent2022day01(void) {
|
||||
char *input = get_input("input/2022/01");
|
||||
printf("Solution for Day 01 of 2022 is not completed yet\n");
|
||||
free(input);
|
||||
char *found, *input = get_input("input/2022/01");
|
||||
const char *errstr;
|
||||
int t = 0;
|
||||
|
||||
List* totals = malloc(sizeof(List));
|
||||
ll_init(totals, free);
|
||||
|
||||
while ((found = strsep(&input, "\n")) != NULL) {
|
||||
if (strcmp(found, "") == 0) {
|
||||
int *i = malloc(sizeof(int));
|
||||
*i = t;
|
||||
ll_ins_next(totals, totals->tail, (void*)i);
|
||||
t = 0;
|
||||
} else {
|
||||
t += (int)(strtonum(found, 0, INT_MAX, &errstr));
|
||||
}
|
||||
}
|
||||
|
||||
int elves[totals->size];
|
||||
int i = 0;
|
||||
LL_ITER(totals) {
|
||||
elves[i++] = *(int*)(node->data);
|
||||
}
|
||||
|
||||
qsort(elves, totals->size, sizeof(int), cmp);
|
||||
|
||||
printf("%d\n", elves[totals->size - 1]);
|
||||
printf("%d\n", elves[totals->size - 1] + elves[totals->size - 2] + elves[totals->size - 3]);
|
||||
|
||||
ll_destroy(totals);
|
||||
free(input);
|
||||
}
|
||||
|
Reference in New Issue
Block a user