-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.c
50 lines (42 loc) · 1.26 KB
/
day01.c
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
42
43
44
45
46
47
48
49
50
#include "advent_of_code.h"
void solve_day01(AdventOfCode* aoc, Stream* file_stream) {
FuriString* line = furi_string_alloc();
int max[3] = {0};
int elf = 0;
while(true) {
if(!stream_read_line(file_stream, line)) break;
furi_string_trim(line);
if(furi_string_empty(line)) {
if(elf > max[0]) {
max[2] = max[1];
max[1] = max[0];
max[0] = elf;
} else if(elf > max[1]) {
max[2] = max[1];
max[1] = elf;
} else if(elf > max[2]) {
max[2] = elf;
}
elf = 0;
continue;
}
unsigned long n = strtoul(furi_string_get_cstr(line), NULL, 10);
elf += n;
}
if(elf > max[0]) {
max[2] = max[1];
max[1] = max[0];
max[0] = elf;
} else if(elf > max[1]) {
max[2] = max[1];
max[1] = elf;
} else if(elf > max[2]) {
max[2] = elf;
}
FuriString* part_1 = furi_string_alloc_printf("%d", max[0]);
FuriString* part_2 = furi_string_alloc_printf("%d", max[0] + max[1] + max[2]);
update_results(aoc, part_1, part_2);
furi_string_free(part_2);
furi_string_free(part_1);
furi_string_free(line);
}