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