common.c (451B)
1 #include "common.h" 2 3 #define MAX_CYCLES 240 4 5 int *parse(char **lines, size_t nlines) { 6 int *r = malloc(MAX_CYCLES * sizeof(*r) + 2); 7 8 r[0] = 1; 9 int t; 10 size_t i, j, c = 0; 11 for (i = 0; i < nlines; i++) { 12 if (lines[i][0] == 'n') { 13 r[c + 1] = r[c]; 14 c++; 15 } else { 16 sscanf(lines[i], "addx %d\n", &t); 17 r[c + 1] = r[c]; 18 r[c + 2] = r[c] + t; 19 c += 2; 20 } 21 22 if (c >= 240) 23 break; 24 } 25 26 return r; 27 }