diff options
| author | Mistivia <i@mistivia.com> | 2025-12-24 17:33:14 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-12-24 17:33:14 +0800 |
| commit | aa5ad99237115f49b5793427482b4a672d6fd4b0 (patch) | |
| tree | 99329a507bd2a5892bc4684d9534a5cb125d659c /12/part1.c | |
| parent | 27241a8e82c7c76dcb36fbb63e15a83b671eecb4 (diff) | |
day 12
Diffstat (limited to '12/part1.c')
| -rw-r--r-- | 12/part1.c | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/12/part1.c b/12/part1.c new file mode 100644 index 0000000..e7f8fc2 --- /dev/null +++ b/12/part1.c @@ -0,0 +1,143 @@ +#include <algds/vec.h> +#include <stdint.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <errno.h> +#include <ctype.h> + +#include <algds/str.h> + +#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; +} + +void skip_to_line_end() { + while (1) { + int c = getchar(); + if (c != '\n' && c != EOF) { + continue; + } else { + break; + } + } +} + +int parse_piece() { + skip_to_line_end(); + int count = 0; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + int c = getchar(); + if (c == '#') count++; + } + expect_char('\n'); + } + expect_char('\n'); + return count; +} + +typedef struct { + int width; + int height; + int pieces[6]; +} Case; + +Case parse_case() { + Case ret; + ret.width = parse_integer(); + expect_char('x'); + ret.height = parse_integer(); + expect_char(':'); + expect_char(' '); + ret.pieces[0] = parse_integer(); + expect_char(' '); + ret.pieces[1] = parse_integer(); + expect_char(' '); + ret.pieces[2] = parse_integer(); + expect_char(' '); + ret.pieces[3] = parse_integer(); + expect_char(' '); + ret.pieces[4] = parse_integer(); + expect_char(' '); + ret.pieces[5] = parse_integer(); + int c = fpeek(stdin); + if (c == '\n') { + getchar(); + } else if (c == EOF) { + return ret; + } else { + PANIC; + } + return ret; +} + +int main () { + int pieces[6]; + for (int i = 0; i < 6; i++) { + pieces[i] = parse_piece(); + } + int ret = 0; + while (1) { + int c = fpeek(stdin); + if (c == EOF) { + break; + } + Case cur = parse_case(); + int need = 0; + for (int i = 0; i < 6; i++) { + need += cur.pieces[i] * pieces[i]; + } + if (cur.height * cur.width > need) { + ret++; + } + } + printf("%d\n", ret); +}
\ No newline at end of file |
