commit 55331ee2a7e8a73468eb6d1015e4d9d1a4403d9f Author: Lucia Ceionia Date: Mon Dec 1 21:16:23 2025 -0600 day 1 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5cbced4 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +all: day1 + +%: %.c + gcc-tree -o $@ -Wall -Werror -pedantic -D_GNU_SOURCE -std=gnu2y -O3 -g $< diff --git a/day1.c b/day1.c new file mode 100644 index 0000000..011eb41 --- /dev/null +++ b/day1.c @@ -0,0 +1,93 @@ +#include +#include +#include + +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)); +}