aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

4b.c (805B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #define N 1000
      6 #define ISNUM(c) (c >= '0' && c <= '9')
      7 
      8 int main() {
      9 	char *buf, line[N];
     10 	int c, i, j, x, sum, nw, nr, w[100], r[100], sc[N];
     11 
     12 	sum = c = 0;
     13 	memset(sc, 0, N * sizeof(int));
     14 	while ((buf = fgets(line, N, stdin)) != NULL) {
     15 		nr = nw = 0;
     16 		sc[++c]++;
     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 					sc[(++x) + c] += sc[c];
     34 					break;
     35 				}
     36 			}
     37 		}
     38 		sum += sc[c];
     39 	}
     40 
     41 	printf("%d\n", sum);
     42 	return 0;
     43 }