advent2025/day1.c
2025-12-14 14:25:12 +01:00

46 lines
1 KiB
C

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int dial_translate(int pos, int offset) {
int final = pos + offset;
int fpos = final;
if (final < 0) {fpos = 100 + final;}
if (final >= 100) {fpos = final - 100;}
return fpos % 100;
}
int main() {
int pos = 50;
int count = 0;
int fd = open("./day1.input", O_RDONLY);
struct stat stat;
fstat(fd, &stat);
char charbuf[stat.st_size];
read(fd, charbuf, stat.st_size);
printf("Size: %d\n", stat.st_size);
int p;
for (p = 0; p < (sizeof(charbuf) - sizeof(char)); p++) {
char cur = charbuf[p];
int direction;
if (cur == 'L') {direction = -1;}
else if (cur == 'R') {direction = 1;}
p++;
int i = 0;
char numline[100] = {0};
while (charbuf[p] != '\n') {
numline[i] = charbuf[p];
i++;
p++;
}
int offset = atoi(numline) * direction;
printf("Offset: %d\n", offset);
pos = dial_translate(pos, offset);
if (pos == 0) { count++; }
printf("Dial at: %d\n", pos);
}
printf("Password: %d\n", count);
return 0;
}