Files
aoc2025/day1.c
2025-12-01 21:16:23 -06:00

94 lines
2.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdcountof.h>
char input[] = {
#embed "day1_input"
};
char test[] = {
#embed "day1_test"
};
unsigned grabnum(const char *s, const char **end) {
unsigned r = 0;
for (; *s >= '0' && *s <= '9'; s++)
r = r * 10 + (*s - '0');
*end = s;
return r;
}
unsigned long do_part1(size_t src_len, const char src[src_len]) {
const char *s = src;
unsigned long count = 0;
unsigned long pos = 1'000'000'000'050;
while (s < &src[src_len]) {
const char *ns = s + 1;
unsigned r = grabnum(ns, &ns);
if (*s == 'L') {
pos -= r;
} else {
pos += r;
}
count += pos % 100 == 0;
s = ns + 1;
}
return count;
}
unsigned do_part2(size_t src_len, const char src[src_len]) {
const char *s = src, *ns = s + 1;
unsigned count = 0;
unsigned pos = 50;
for (;s < &src[src_len]; s = ns + 1, ns = s + 1) {
unsigned r = grabnum(ns, &ns);
if (*s == 'L') {
if (pos == 0) pos = 100;
unsigned to_zero = pos;
if (r < to_zero) {
pos -= r;
} else {
unsigned over = r - to_zero;
unsigned rots = over / 100;
unsigned adjust = over % 100;
count += 1 + rots;
pos = (100 - adjust) % 100;
}
} else {
unsigned to_zero = 100 - pos;
if (r < to_zero) {
pos += r;
} else {
unsigned over = r - to_zero;
unsigned rots = over / 100;
unsigned adjust = over % 100;
count += 1 + rots;
pos = adjust;
}
}
}
return count;
}
int main() {
printf("PART 1 TEST: ");
if (unsigned long v = do_part1(countof(test), test); v != 3) {
printf("FAILED (got %lu, expected 3)\n", v);
} else {
printf("PASSED\n");
}
printf("PART 1 RESULT: %lu\n",
do_part1(countof(input), input));
printf("PART 2 TEST: ");
if (unsigned v = do_part2(countof(test), test); v != 6) {
printf("FAILED (got %u, expected 6)\n", v);
} else {
printf("PASSED\n");
}
printf("PART 2 RESULT: %u\n",
do_part2(countof(input), input));
}