From a0ee3c44393a8f24153fe48727ea312f93ec6afa Mon Sep 17 00:00:00 2001 From: Mistivia Date: Sun, 14 Dec 2025 22:50:15 +0800 Subject: day 2 part 1 --- 02/Makefile | 11 ++++ 02/part1.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ 3 files changed, 180 insertions(+) create mode 100644 02/Makefile create mode 100644 02/part1.c create mode 100644 README.md diff --git a/02/Makefile b/02/Makefile new file mode 100644 index 0000000..f69d8a5 --- /dev/null +++ b/02/Makefile @@ -0,0 +1,11 @@ +part1: part1.c + gcc part1.c -o part1 -lalgds + +part2: part2.c + gcc part2.c -o part2 -lalgds + +1: part1 + cat input | ./part1 + +2: part2 + cat input | ./part2 \ No newline at end of file diff --git a/02/part1.c b/02/part1.c new file mode 100644 index 0000000..6d08d4b --- /dev/null +++ b/02/part1.c @@ -0,0 +1,166 @@ +#include +#include +#include +#include + +#include +#include + +#define PANIC do { \ + fprintf(stderr, "panic at %s:%d\n", __FILE__, __LINE__); \ + abort(); \ +} while (0) + +typedef struct { + char *first; + char *second; +} StrPair; + +void StrPair_show(StrPair self, FILE *fp) { + fprintf(fp, "{%s-%s}", self.first, self.second); +} + +VECTOR_DEF(StrPair); +VECTOR_IMPL(StrPair); + +void expect_char(char e) { + int c = fpeek(stdin); + if (c != e) { + PANIC; + } + fgetc(stdin); +} + +void skip_space() { + while (1) { + int c = fpeek(stdin); + if (c == EOF) return; + if (isspace(c)) { + fgetc(stdin); + } else { + return; + } + } +} + +void sep_or_end() { + int c = fpeek(stdin); + if (c == ',') { + fgetc(stdin); + skip_space(); + return; + } else if (c == EOF) { + return; + } + PANIC; +} + +char *parse_integer() { + StrBuilder sb; + StrBuilder_init(&sb); + while (1) { + int c = fpeek(stdin); + if (isdigit(c)) { + fgetc(stdin); + StrBuilder_append_char(&sb, c); + } else { + break; + } + } + if (strlen(sb.buf) == 0) { + PANIC; + } + StrBuilder_append_char(&sb, '\0'); + return sb.buf; +} + +StrPair parse_product() { + StrPair ret; + ret.first = parse_integer(); + expect_char('-'); + ret.second = parse_integer(); + sep_or_end(); + return ret; +} + +StrPairVector parse() { + StrPairVector res; + StrPairVector_init(&res); + while(1) { + int c = fpeek(stdin); + if (c == EOF) { + return res; + } else if (isdigit(c)) { + StrPair sp = parse_product(); + StrPairVector_push_back(&res, sp); + } else { + PANIC; + } + } + return res; +} + +bool string2long(const char *s, long *num) { + char *end; + errno = 0; + *num = strtol(s, &end, 10); + if (end == s) { + return false; + } + if (errno != 0) { + return false; + } + return true; +} + +static int numlen(long num) { + if (num < 0) return -1; + int count = 1; + while (num >= 10) { + num /= 10; + count++; + } + return count; +} + +static int pow10halflen(long num) { + int l = numlen(num); + l /= 2; + int res = 1; + for (int i = 0; i < l; i++) { + res *= 10; + } + return res; +} + +static int is_invalid_id(long num) { + long p = pow10halflen(num); + if (num % p == num / p) { + return 1; + } + return 0; +} + +int main() { + StrPairVector spv = parse(); + StrPairVector_show(spv, stdout); + putchar('\n'); + StrPair * iter = StrPairVector_begin(&spv); + int64_t sum = 0; + for (; iter != StrPairVector_end(&spv); iter++) { + long start, end; + if (!string2long(iter->first, &start)) { + PANIC; + } + if (!string2long(iter->second, &end)) { + PANIC; + } + for (long t = start; t <= end; t++) { + if (is_invalid_id(t)) { + sum += t; + } + } + } + printf("%ld\n", sum); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bbec624 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Advent of Code 2025 + +Solutions are written in C, with [algds](https://github.com/mistivia/algds). \ No newline at end of file -- cgit v1.0