diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-11 15:14:47 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-11 15:14:47 +0100 |
| commit | 554ad47f09974f0131df285fda8ea50a1fdad8f5 (patch) | |
| tree | d23caeb85e7fce6290664a0ebf3c96f513efbe94 /2023/04/4b.c | |
| download | aoc-554ad47f09974f0131df285fda8ea50a1fdad8f5.tar.gz aoc-554ad47f09974f0131df285fda8ea50a1fdad8f5.zip | |
Initial commit, some solutions
Diffstat (limited to '2023/04/4b.c')
| -rw-r--r-- | 2023/04/4b.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/2023/04/4b.c b/2023/04/4b.c new file mode 100644 index 0000000..c122f8b --- /dev/null +++ b/2023/04/4b.c | |||
| @@ -0,0 +1,45 @@ | |||
| 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 main() { | ||
| 11 | char *buf, line[N]; | ||
| 12 | int c, i, j, x, sum, nw, nr, w[100], r[100], sc[N]; | ||
| 13 | |||
| 14 | sum = c = 0; | ||
| 15 | memset(sc, 0, N * sizeof(int)); | ||
| 16 | while ((buf = fgets(line, N, stdin)) != NULL) { | ||
| 17 | nr = nw = 0; | ||
| 18 | sc[++c]++; | ||
| 19 | while (*buf != ':') buf++; | ||
| 20 | while (*buf != '|') { | ||
| 21 | buf++; | ||
| 22 | if (!isnum(*buf)) continue; | ||
| 23 | w[nw++] = atoi(buf); | ||
| 24 | while (isnum(*buf)) buf++; | ||
| 25 | } | ||
| 26 | while (*buf != '\n') { | ||
| 27 | buf++; | ||
| 28 | if (!isnum(*buf)) continue; | ||
| 29 | r[nr++] = atoi(buf); | ||
| 30 | while (isnum(*buf)) buf++; | ||
| 31 | } | ||
| 32 | for (x = 0, i = 0; i < nr; i++) { | ||
| 33 | for (j = 0; j < nw; j++) { | ||
| 34 | if (r[i] == w[j]) { | ||
| 35 | sc[(++x) + c] += sc[c]; | ||
| 36 | break; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | sum += sc[c]; | ||
| 41 | } | ||
| 42 | |||
| 43 | printf("%d\n", sum); | ||
| 44 | return 0; | ||
| 45 | } | ||
