diff options
Diffstat (limited to '2023/12/12a.c')
| -rw-r--r-- | 2023/12/12a.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/2023/12/12a.c b/2023/12/12a.c new file mode 100644 index 0000000..d88ed9a --- /dev/null +++ b/2023/12/12a.c | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #define N 1000 | ||
| 8 | |||
| 9 | #define MAX(a,b) ((a)>(b)?(a):(b)) | ||
| 10 | #define MIN(a,b) ((a)<(b)?(a):(b)) | ||
| 11 | #define ABS(a) MAX((a),-(a)) | ||
| 12 | |||
| 13 | #define isnum(c) (c == '-' || (c >= '0' && c <= '9')) | ||
| 14 | |||
| 15 | char *buf, line[N]; | ||
| 16 | int s, n, a[N]; | ||
| 17 | |||
| 18 | int readl(int nums[], char *buf) { | ||
| 19 | int64_t i; | ||
| 20 | for (i = 0; *buf; buf++) { | ||
| 21 | if (!isnum(*buf)) continue; | ||
| 22 | nums[i++] = atoll(buf); | ||
| 23 | while (isnum(*buf)) buf++; | ||
| 24 | } | ||
| 25 | return i; | ||
| 26 | } | ||
| 27 | |||
| 28 | int isgood(char *line, int a[], int n) { | ||
| 29 | int b[N]; | ||
| 30 | memset(b, 0, N * sizeof(int)); | ||
| 31 | memcpy(b, a, n * sizeof(int)); | ||
| 32 | for (int j = 0, c = 0; *line != ' '; line++) { | ||
| 33 | if (*line == '?') printf("Error!\n"); | ||
| 34 | if (*line == '.') { j += c; c = 0; } | ||
| 35 | if (*line == '#') { c = 1; b[j]--; } | ||
| 36 | } | ||
| 37 | for (int j = 0; j < N; j++) | ||
| 38 | if (b[j]) | ||
| 39 | return 0; | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | int arrange(char *line, char *buf, int a[], int n) { | ||
| 44 | while (*buf != '?' && *buf != ' ') buf++; | ||
| 45 | if (*buf == ' ') | ||
| 46 | return isgood(line, a, n); | ||
| 47 | *buf = '.'; | ||
| 48 | int x = arrange(line, buf, a, n); | ||
| 49 | *buf = '#'; | ||
| 50 | int y = arrange(line, buf, a, n); | ||
| 51 | *buf = '?'; | ||
| 52 | return x + y; | ||
| 53 | } | ||
| 54 | |||
| 55 | int main() { | ||
| 56 | while ((buf = fgets(line, N, stdin)) != NULL) { | ||
| 57 | n = readl(a, buf); | ||
| 58 | s += arrange(line, buf, a, n); | ||
| 59 | } | ||
| 60 | |||
| 61 | printf("%d\n", s); | ||
| 62 | return 0; | ||
| 63 | } | ||
