49 lines
964 B
C
49 lines
964 B
C
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
int part_1(FILE *input) {
|
|
rewind(input);
|
|
|
|
int frequency = 0;
|
|
int current_line = 0;
|
|
while(fscanf(input, "%d", ¤t_line) > 0) frequency += current_line;
|
|
|
|
return frequency;
|
|
}
|
|
|
|
int part_2(FILE *input) {
|
|
rewind(input);
|
|
|
|
bool frequencies[2000000] = {0};
|
|
int frequency = 0;
|
|
int current_line = 0;
|
|
bool found = false;
|
|
while(!found) {
|
|
while(fscanf(input, "%d", ¤t_line) > 0) {
|
|
frequency += current_line;
|
|
if (frequencies[frequency + 1000000]) {
|
|
found = true;
|
|
break;
|
|
}
|
|
frequencies[frequency + 1000000] = true;
|
|
}
|
|
rewind(input);
|
|
}
|
|
|
|
return frequency;
|
|
}
|
|
|
|
int main() {
|
|
char *input_file_path = "../data/2018/1/input.txt";
|
|
FILE *input = fopen(input_file_path, "r");
|
|
if (input == NULL) {
|
|
printf("Can't open file %s\n", input_file_path);
|
|
return 1;
|
|
}
|
|
|
|
printf("Part 1: %d\n", part_1(input));
|
|
printf("Part 2: %d\n", part_2(input));
|
|
|
|
fclose(input);
|
|
}
|