C 2022 day 1
This commit is contained in:
parent
70348ba551
commit
c343871a2e
61
c/2022/1/problem.c
Normal file
61
c/2022/1/problem.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../lib/aoc.h"
|
||||
|
||||
int main() {
|
||||
char *input = aoc_read_input();
|
||||
char *seek = input;
|
||||
|
||||
int current_cals = 0;
|
||||
int current_elf = 0;
|
||||
int max_elf = 0;
|
||||
int second_max_elf = 0;
|
||||
int third_max_elf = 0;
|
||||
|
||||
while (seek != NULL) {
|
||||
char *double_line_break = strstr(seek, "\n\n");
|
||||
if (double_line_break == NULL) {
|
||||
seek = NULL;
|
||||
} else {
|
||||
*double_line_break = '\0';
|
||||
char *line = strtok(seek, "\n");
|
||||
while (line != NULL) {
|
||||
current_cals = atoi(line);
|
||||
current_elf += current_cals;
|
||||
line = strtok(NULL, "\n");
|
||||
}
|
||||
|
||||
if (current_elf > max_elf) {
|
||||
int z = current_elf;
|
||||
current_elf = max_elf;
|
||||
max_elf = z;
|
||||
}
|
||||
|
||||
if (current_elf > second_max_elf) {
|
||||
int z = current_elf;
|
||||
current_elf = second_max_elf;
|
||||
second_max_elf = z;
|
||||
}
|
||||
|
||||
if (current_elf > third_max_elf) {
|
||||
int z = current_elf;
|
||||
current_elf = third_max_elf;
|
||||
third_max_elf = z;
|
||||
}
|
||||
|
||||
current_elf = 0;
|
||||
|
||||
seek = double_line_break + 1;
|
||||
*seek = '\0';
|
||||
seek = seek + 1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Part 1: %d\n", max_elf);
|
||||
printf("Part 2: %d\n", max_elf + second_max_elf + third_max_elf);
|
||||
|
||||
free(input);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user