aoc

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

15b.c (852B)


      1 #include <inttypes.h>
      2 #include <stdio.h>
      3 
      4 #define N 100000
      5 
      6 char *b, line[N];
      7 int64_t c, l, s, n[256], box[256][N];
      8 
      9 int main() {
     10 	fgets(line, N, stdin);
     11 
     12 	for (b = line, c = 0; *b != '\n'; b++) {
     13 		switch (*b) {
     14 		case ',':
     15 			c = l = 0;
     16 			break;
     17 		case '=':
     18 			int64_t i;
     19 			for (i = 0; i < n[c]; i++)
     20 				if (box[c][i] / 10 == l)
     21 					break;
     22 			if (i == n[c]) n[c]++;
     23 			box[c][i] = l * 10 + (int)(*(++b)-'0');
     24 			break;
     25 		case '-':
     26 			for (int64_t i = 0; i < n[c]; i++) {
     27 				if (box[c][i] / 10 == l) {
     28 					for (int j = i+1; j < n[c]; j++)
     29 						box[c][j-1] = box[c][j];
     30 					n[c]--;
     31 				}
     32 			}
     33 			break;
     34 		default:
     35 			c = (c + (int)*b) * 17 % 256;
     36 			l = l*256 + (int)*b;
     37 			break;
     38 		}
     39 	}
     40 
     41 	for (int64_t i = 0; i < 256; i++)
     42 		for (int64_t j = 0; j < n[i]; j++)
     43 			s += (i+1) * (j+1) * (box[i][j]%10);
     44 
     45 	printf("%" PRId64 "\n", s);
     46 	return 0;
     47 }