diff options
| author | Mistivia <i@mistivia.com> | 2025-12-22 21:26:08 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-12-22 21:26:08 +0800 |
| commit | 43ed956fe8aec82a2b31833be210817186a0175d (patch) | |
| tree | 7f5c16e2cb4914f0b25d878eb54bb9a75ae2df2f /09 | |
| parent | a7565285666245625874c353be525fe835fabaa1 (diff) | |
day 9 part 1
Diffstat (limited to '09')
| -rw-r--r-- | 09/Makefile | 13 | ||||
| -rw-r--r-- | 09/part1.c | 112 |
2 files changed, 125 insertions, 0 deletions
diff --git a/09/Makefile b/09/Makefile new file mode 100644 index 0000000..7d325ba --- /dev/null +++ b/09/Makefile @@ -0,0 +1,13 @@ +all: part1 part2 + +part1: part1.c + gcc -Wall -g part1.c -o part1 -lalgds + +part2: part2.c + gcc -Wall -g part2.c -o part2 -lalgds + +1: part1 + cat input | ./part1 + +2: part2 + cat input | ./part2
\ No newline at end of file diff --git a/09/part1.c b/09/part1.c new file mode 100644 index 0000000..039d1b0 --- /dev/null +++ b/09/part1.c @@ -0,0 +1,112 @@ +#include <algds/sort.h> +#include <errno.h> +#include <ctype.h> + +#include <algds/vec.h> +#include <algds/str.h> +#include <stdio.h> + +typedef struct { + int x; + int y; +} Vec2; + +long rec_area(Vec2 a, Vec2 b) { + long dx = a.x - b.x; + long dy = a.y - b.y; + if (dx < 0) dx = -dx; + if (dy < 0) dy = -dy; + return (dx + 1) * (dy + 1); +} + +#define PANIC do { \ + fprintf(stderr, "panic at %s:%d\n", __FILE__, __LINE__); \ + abort(); \ +} while (0) + +void expect_char(char e) { + int c = fpeek(stdin); + if (c != e) { + PANIC; + } + fgetc(stdin); +} + +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; +} + +int64_t parse_integer() { + long ret; + 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'); + if (!string2long(sb.buf, &ret)) { + PANIC; + } + free(sb.buf); + return ret; +} + +Vec2 parse_vec2() { + int x = parse_integer(); + expect_char(','); + int y = parse_integer(); + return (Vec2){.x = x, .y = y}; +} + +void Vec2_show(Vec2 self, FILE *fp) { + fprintf(fp, "(%d,%d)", self.x, self.y); +} + +VECTOR_DEF(Vec2) +VECTOR_IMPL(Vec2) + +int main() { + Vec2Vector tiles; + Vec2Vector_init(&tiles); + while (1) { + int c = fpeek(stdin); + if (!isdigit(c)) { + break; + } + Vec2 v = parse_vec2(); + c = fpeek(stdin); + if (c == '\n') { + fgetc(stdin); + } + Vec2Vector_push_back(&tiles, v); + } + long max_area = 0; + for (int i = 0; i < tiles.size; i++) { + for (int j = i + 1; j < tiles.size; j++) { + long area = rec_area(tiles.buffer[i], tiles.buffer[j]); + if (area > max_area) { + max_area = area; + } + } + } + printf("%ld\n", max_area); + return 0; +}
\ No newline at end of file |
