Added: day 1 part 1

This commit is contained in:
2025-12-07 01:07:27 +01:00
commit cdb178f2cd
4 changed files with 4570 additions and 0 deletions

61
day1/day1.c Normal file
View File

@@ -0,0 +1,61 @@
/*
Dial from 099, start at 50.
You get lines like L68 or R5:
L = move left (subtract)
R = move right (add)
Wrap around with modulo 100.
After each move, check if the dial is on 0.
The password = how many times you land on 0 during the whole sequence.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
printf("Advent Of Code Day 1\n\n");
// remove argc so gcc wont cry
(void)argc;
// base vars
int start = 50;
int pos = start;
int counter = 0;
// read the input file
char* inpf = NULL;
inpf = argv[1];
if (inpf == NULL) inpf = "input.txt";
FILE* ptr = fopen(inpf, "r");
if(ptr == NULL) {
printf("error: no such file %s\n", inpf);
return 1;
}
// process instructions
char line[9]; // 8 chars + eol
while(fscanf(ptr, "%4s", line) == 1) {
//printf("current position: %d\n", pos);
char drc = line[0];
int num = atoi(&line[1]);
//printf("%c %d\n", drc, num);
if (drc == 'R') pos += num;
if (drc == 'L') pos -= num;
while (pos > 99) {
pos -= 100;
}
while (pos < 0){
pos += 100;
}
if (pos == 0) counter++;
}
//printf("current position: %d\n", pos);
printf("\npassword is: %d\n", counter);
return 0;
}