feat: 1: safe rotating lock
This commit is contained in:
commit
f8c9aba703
2 changed files with 47 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
**/*.input
|
||||
46
day1.c
Normal file
46
day1.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue