aoc

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

2a.c (638B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define N 10000
      5 #define MAX(x,y) ((x)>(y)?(x):(y))
      6 
      7 int color[26] = { ['r'-'a'] = 0, ['g'-'a'] = 1, ['b'-'a'] = 2 };
      8 
      9 int main() {
     10 	char *buf, line[N];
     11 	int i, sum, x, maxc[3];
     12 
     13 	for (i = 1, sum = 0; (buf = fgets(line, N, stdin)) != NULL; i++) {
     14 		maxc[0] = maxc[1] = maxc[2] = 0;
     15 		while (*buf++ != ':');
     16 		while (*buf++) {
     17 			if (*buf < '0' || *buf > '9') continue;
     18 			x = atoi(buf);
     19 			while (*buf < 'a' || *buf > 'z') buf++;
     20 			maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x);
     21 		}
     22 		if (maxc[0] <= 12 && maxc[1] <= 13 && maxc[2] <= 14)
     23 			sum += i;
     24 	}
     25 
     26 	printf("%d\n", sum);
     27 	return 0;
     28 }