Created
December 1, 2025 15:19
-
-
Save tatut/04a38c6ea26e86d17bfd46aff77b693b to your computer and use it in GitHub Desktop.
AoC 2025, day1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <sys/mman.h> | |
| #include <sys/stat.h> | |
| int count(char *in, void (*rotate)(int*, int*, int)) { | |
| int count = 0; | |
| int current = 50; | |
| while(*in == 'R' || *in == 'L') { | |
| int dx = *in == 'R' ? 1 : -1; | |
| in++; | |
| int how_much = atoi(in); | |
| while(*in != '\n') in++; | |
| in++; | |
| rotate(&count, ¤t, dx*how_much); | |
| } | |
| return count; | |
| } | |
| void part1(int *count, int *current, int how_much) { | |
| *current += (how_much%100); | |
| if(*current < 0) *current = 100 - abs(*current); | |
| if(*current > 99) *current -= 100; | |
| if(*current == 0) { | |
| *count += 1; | |
| } | |
| } | |
| void part2(int *count, int *current, int how_much) { | |
| int d = how_much < 0 ? -1 : 1; | |
| int c = abs(how_much); | |
| while(c) { | |
| *current += d; | |
| if(*current == -1) *current = 99; | |
| else if(*current == 100) *current = 0; | |
| if(*current == 0) *count += 1; | |
| c--; | |
| } | |
| } | |
| int main(int argc, char **argv) { | |
| FILE *f = fopen("day1.txt", "r"); | |
| struct stat fs; | |
| int in = fileno(f); | |
| fstat(in, &fs); | |
| char *map = mmap(0,fs.st_size, PROT_READ, MAP_SHARED, in, 0); | |
| printf("Part1: %d\n", count(map, part1)); | |
| printf("Part2: %d\n", count(map, part2)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment