Made day 1 part 2 less shitty (C)
This commit is contained in:
parent
d06f055cdf
commit
552da81e32
26
01/day1_2.c
26
01/day1_2.c
|
@ -4,39 +4,27 @@
|
|||
|
||||
|
||||
// Originally, I used hashsets for this, which refused to work for me.
|
||||
// I would then find out that the implementation I was using hashes based on the pointer, not the value, so I just wrote an O(n²) solution. yay.
|
||||
// I hate myself for this. Could implement an ordered list to get O(log n) instead of O(n) for isIn, but who cares at this point.
|
||||
// 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.
|
||||
|
||||
#define inputLength 1024000 // adjust to length of input file
|
||||
|
||||
int isIn(int array[inputLength], int value) {
|
||||
for (int i=0; i<inputLength; i++) {
|
||||
if (array[i] == value) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#define inputLength 1000000 // adjust expected length
|
||||
|
||||
int main() {
|
||||
int position = 0;
|
||||
char line [10];
|
||||
int positions [inputLength] = {[0 ... 1023999] = -9999999};
|
||||
int currectLine = 0;
|
||||
int positions [inputLength]; // all initialized to 0
|
||||
|
||||
while (1) {
|
||||
FILE* inputFile = fopen("input", "r");
|
||||
while (fgets(line, sizeof(line), inputFile)) {
|
||||
position += strtol(line, NULL, 10);
|
||||
if (isIn(positions, position) == 1) {
|
||||
// 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) {
|
||||
printf("%d\n", position);
|
||||
return 0;
|
||||
}
|
||||
positions[currectLine] = position;
|
||||
currectLine++;
|
||||
positions[position] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user