aoc/2015/day01/answer02.c

48 lines
768 B
C
Raw Permalink Normal View History

2024-07-05 19:55:39 -04:00
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
int main() {
int32_t floor = 0;
uint32_t pos = 0;
FILE *file = fopen("input", "r");
if (file == NULL) {
perror("Error opening file.");
return 1;
}
char buffer[256];
bool breakout = false;
while (fgets(buffer, sizeof(buffer), file) != NULL) {
uint16_t length = strlen(buffer);
if (breakout) break;
for (uint16_t i = 0; i < length; i++) {
if (buffer[i] == '(') {
floor++;
pos++;
}
else if (buffer[i] == ')') {
floor--;
pos++;
}
if (floor == -1) {
breakout = true;
break;
}
}
}
if (ferror(file)) {
perror("Error reading form file.");
return 1;
}
fclose(file);
printf("position: %i\n", pos);
return 0;
}