advent-of-code/2018/01/day1_2.c

29 lines
967 B
C
Raw Normal View History

2018-12-01 19:22:26 +01:00
#include <stdio.h>
#include <stdlib.h>
// Originally, I used hashsets for this, which refused to work for me.
2018-12-02 10:55:17 +01:00
// I would then find out that the implementation I was using hashes based on the pointer, not the value, so I just wrote a stupid lookup table.
2018-12-01 19:22:26 +01:00
#define inputLength 1000000 // adjust accordingly
2018-12-01 19:22:26 +01:00
int main() {
int position = 0;
char line [10];
char positions [inputLength]; // all initialized to 0
2018-12-01 19:22:26 +01:00
while (1) {
FILE* inputFile = fopen("input", "r");
while (fgets(line, sizeof(line), inputFile)) {
position += strtol(line, NULL, 10);
2018-12-02 10:55:17 +01:00
// Because of part 1, we know that the numbers are steadily increasing.
// Negative numbers are therefore skipped, as we know the repeated number to be positive.
if (position > 0 && positions[position] == 1) {
2018-12-01 19:22:26 +01:00
printf("%d\n", position);
return 0;
}
2018-12-02 10:55:17 +01:00
positions[position] = 1;
2018-12-01 19:22:26 +01:00
}
}
}