aoc

Thing1's 2025 aoc
Log | Files | Refs

1a.c (493B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define ABS(n) ((n < 0) ? -n : n)
      5 
      6 int
      7 rotate(int pos, int dis) {
      8 	int dir = (dis < 0) ? -1 : 1;
      9 
     10 	for (int i = 0; i < ABS(dis); i++) {
     11 		pos += dir;
     12 		if (pos > 99) pos = 0;
     13 		if (pos < 0) pos = 99;
     14 	}
     15 	return pos;
     16 }
     17 
     18 int
     19 main() {
     20 	char line[20];
     21 	int pos = 50, off = 0, zeros = 0;
     22 
     23 	while (fgets(line, 20, stdin)) {
     24 		off = atoi(line + 1) * ((line[0] == 'L') ? -1 : 1);
     25 		pos = rotate(pos, off);
     26 		if (pos == 0) zeros++;
     27 
     28 	}
     29 
     30 	printf("%d\n", zeros);
     31 }