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