diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-25 17:56:34 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-25 17:56:34 +0100 |
| commit | 94d0033ed89bb5bfb6500051296892fb7cea6c29 (patch) | |
| tree | 245eac2218310c958c2df2fad5109ea4f7b2a9d9 /2023/03/3a.c | |
| parent | 6a480c4eb9c96c82a8fd3663f0fbee4d32e56f19 (diff) | |
| download | aoc-94d0033ed89bb5bfb6500051296892fb7cea6c29.tar.gz aoc-94d0033ed89bb5bfb6500051296892fb7cea6c29.zip | |
Small cleanup
Diffstat (limited to '2023/03/3a.c')
| -rw-r--r-- | 2023/03/3a.c | 30 |
1 files changed, 9 insertions, 21 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); |
