aoc

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

2b.c (596B)


      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 sum, x, maxc[3];
     12 
     13 	sum = 0;
     14 	while ((buf = fgets(line, N, stdin)) != NULL) {
     15 		maxc[0] = maxc[1] = maxc[2] = 0;
     16 		while (*buf++ != ':');
     17 		while (*buf++) {
     18 			if (*buf < '0' || *buf > '9') continue;
     19 			x = atoi(buf);
     20 			while (*buf < 'a' || *buf > 'z') buf++;
     21 			maxc[color[*buf-'a']] = MAX(maxc[color[*buf-'a']], x);
     22 		}
     23 		sum += maxc[0] * maxc[1] * maxc[2];
     24 	}
     25 
     26 	printf("%d\n", sum);
     27 	return 0;
     28 }