]> git.friedersdorff.com Git - max/aoc_2022.git/blob - 1_1.c
Do day 7
[max/aoc_2022.git] / 1_1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void set_calories(int* current, int* highest) {
5     if (*current > *highest) {
6         *highest = *current;
7     }
8
9     *current = 0;
10 }
11
12
13 int main()
14 {
15     char *buffer;
16     size_t bufsize;
17     size_t characters;
18
19     int current_calories = 0;
20     int highest_calories = 0;
21
22
23     while ((characters = getline(&buffer, &bufsize, stdin)) != -1) {
24         --characters;
25
26         buffer[characters] = 0;
27
28         if (characters == 0) {
29             set_calories(&current_calories, &highest_calories);
30         }
31
32         current_calories += atoi(buffer);
33     }
34
35     set_calories(&current_calories, &highest_calories);
36     printf("The elf with the highest calories has %d calories", highest_calories);
37 }
38
39