aoc

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

1b.c (601B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 #define N 10000
      5 
      6 char *nums[10] = { "zero (unused)", "one", "two", "three", "four", "five",
      7 	"six", "seven", "eight", "nine" };
      8 
      9 int main() {
     10 	char *buf, line[N];
     11 	int j, first, last, sum;
     12 
     13 	sum = 0;
     14 	while ((buf = fgets(line, N, stdin)) != NULL) {
     15 		for (first = -1; *buf; buf++) {
     16 			for (j = 1; j < 10; j++)
     17 				if (!strncmp(buf, nums[j], strlen(nums[j])))
     18 					*buf = j + '0';
     19 			if (*buf < '0' || *buf > '9') continue;
     20 			first = first == -1 ? *buf - '0' : first;
     21 			last = *buf - '0';
     22 		}
     23 		sum += 10 * first + last;
     24 	}
     25 	printf("%d\n", sum);
     26 	return 0;
     27 }