Advent of code d1
I never manage to finish advent of code. Here is my C solution to day 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 99
int main() {
int result = 50;
int password = 0;
FILE *f;
char line[MAX_LINE_LENGTH];
const char *filename = "day1-input.txt";
f = fopen(filename, "r");
if(f == NULL) {
perror("error opening");
return 1;
}
while(fgets(line, MAX_LINE_LENGTH, f) != NULL) {
size_t len = strlen(line);
if(len > 1 && (line[0] == 'L' || line[0] == 'R')) {
int n = atoi(line + 1);
for(int i = 0; i < n; i++) {
if(line[0] == 'L') {
result--;
if(result < 0) result = 99;
} else {
result++;
if(result > 99) result = 0;
}
// increment here for second phase
// if(result == 0) password++;
}
// increment here for first phase
if(result == 0) password++;
}
}
fclose(f);
printf("%d\n", password);
return 0;
}