aoc

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

4a.c (711B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define N 1000
      5 #define ISNUM(c) (c >= '0' && c <= '9')
      6 
      7 int main() {
      8 	char *buf, line[N];
      9 	int i, j, x, sum, nw, nr, w[100], r[100];
     10 
     11 	sum = 0;
     12 	while ((buf = fgets(line, N, stdin)) != NULL) {
     13 		nr = nw = 0;
     14 		while (*buf != ':') buf++;
     15 		while (*buf != '|') {
     16 			buf++;
     17 			if (!ISNUM(*buf)) continue;
     18 			w[nw++] = atoi(buf);
     19 			while (ISNUM(*buf)) buf++;
     20 		}
     21 		while (*buf != '\n') {
     22 			buf++;
     23 			if (!ISNUM(*buf)) continue;
     24 			r[nr++] = atoi(buf);
     25 			while (ISNUM(*buf)) buf++;
     26 		}
     27 		for (x = 0, i = 0; i < nr; i++) {
     28 			for (j = 0; j < nw; j++) {
     29 				if (r[i] == w[j]) {
     30 					x++;
     31 					break;
     32 				}
     33 			}
     34 		}
     35 		sum += 1 << (x-1);
     36 	}
     37 
     38 	printf("%d\n", sum);
     39 	return 0;
     40 }