diff options
Diffstat (limited to '2023/03')
| -rw-r--r-- | 2023/03/3a.c | 30 | ||||
| -rw-r--r-- | 2023/03/3b.c | 16 |
2 files changed, 14 insertions, 32 deletions
diff --git a/2023/03/3a.c b/2023/03/3a.c index 13c8c8d..1d07a8e 100644 --- a/2023/03/3a.c +++ b/2023/03/3a.c | |||
| @@ -1,30 +1,23 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 1000 | 5 | #define N 1000 |
| 7 | 6 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 8 | bool isnum(char c) { return c >= '0' && c <= '9'; } | 7 | #define ISSYM(c) (c != '.' && c != 0 && c != '\n' && !ISNUM(c)) |
| 9 | bool issymbol(char c) { return c != '.' && c != 0 && c != '\n' && !isnum(c); } | ||
| 10 | 8 | ||
| 11 | int grab(char *buf) { | 9 | int grab(char *buf) { |
| 12 | int x; | 10 | if (!ISNUM(*buf)) return 0; |
| 13 | 11 | while (ISNUM(*buf)) buf--; | |
| 14 | if (!isnum(*buf)) | ||
| 15 | return 0; | ||
| 16 | |||
| 17 | while (isnum(*buf)) buf--; | ||
| 18 | buf++; | 12 | buf++; |
| 19 | x = atoi(buf); | 13 | int x = atoi(buf); |
| 20 | while (isnum(*buf)) *buf++ = '.'; | 14 | while (ISNUM(*buf)) *buf++ = '.'; |
| 21 | 15 | ||
| 22 | return x; | 16 | return x; |
| 23 | } | 17 | } |
| 24 | 18 | ||
| 25 | int main() { | 19 | int main() { |
| 26 | char line[N], prev[N]; | 20 | char line[N], prev[N]; |
| 27 | bool l, c, r, ul, uc, ur; | ||
| 28 | int i, sum; | 21 | int i, sum; |
| 29 | 22 | ||
| 30 | sum = 0; | 23 | sum = 0; |
| @@ -32,15 +25,10 @@ int main() { | |||
| 32 | memset(prev, '.', N); | 25 | memset(prev, '.', N); |
| 33 | while (fgets(&line[1], N-1, stdin) != NULL) { | 26 | while (fgets(&line[1], N-1, stdin) != NULL) { |
| 34 | for (i = 1; line[i]; i++) { | 27 | for (i = 1; line[i]; i++) { |
| 35 | l = issymbol(line[i-1]); | 28 | if (ISSYM(line[i-1]) || ISSYM(line[i+1]) || |
| 36 | c = issymbol(line[i]); | 29 | ISSYM(prev[i-1]) || ISSYM(prev[i]) || ISSYM(prev[i+1])) |
| 37 | r = issymbol(line[i+1]); | ||
| 38 | ul = issymbol(prev[i-1]); | ||
| 39 | uc = issymbol(prev[i]); | ||
| 40 | ur = issymbol(prev[i+1]); | ||
| 41 | if (l || r || ul || uc || ur) | ||
| 42 | sum += grab(&line[i]); | 30 | sum += grab(&line[i]); |
| 43 | if (l || c || r) | 31 | if (ISSYM(line[i-1]) || ISSYM(line[i]) || ISSYM(line[i+1])) |
| 44 | sum += grab(&prev[i]); | 32 | sum += grab(&prev[i]); |
| 45 | } | 33 | } |
| 46 | memcpy(prev, line, N); | 34 | memcpy(prev, line, N); |
diff --git a/2023/03/3b.c b/2023/03/3b.c index 27b5b7c..9bf4c27 100644 --- a/2023/03/3b.c +++ b/2023/03/3b.c | |||
| @@ -1,22 +1,16 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 1 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 4 | #include <string.h> | 3 | #include <string.h> |
| 5 | 4 | ||
| 6 | #define N 1000 | 5 | #define N 1000 |
| 7 | 6 | #define ISNUM(c) (c >= '0' && c <= '9') | |
| 8 | bool isnum(char c) { return c >= '0' && c <= '9'; } | ||
| 9 | 7 | ||
| 10 | int grab(char *buf) { | 8 | int grab(char *buf) { |
| 11 | int x; | 9 | if (!ISNUM(*buf)) return 0; |
| 12 | 10 | while (ISNUM(*buf)) buf--; | |
| 13 | if (!isnum(*buf)) | ||
| 14 | return 0; | ||
| 15 | |||
| 16 | while (isnum(*buf)) buf--; | ||
| 17 | buf++; | 11 | buf++; |
| 18 | x = atoi(buf); | 12 | int x = atoi(buf); |
| 19 | while (isnum(*buf)) *buf++ = '.'; | 13 | while (ISNUM(*buf)) *buf++ = '.'; |
| 20 | 14 | ||
| 21 | return x; | 15 | return x; |
| 22 | } | 16 | } |
