aoc_2019_2_1.c (990B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 long perform(char *data) { 6 int len = strlen(data); 7 int size = 1; 8 for (int i = 0; i < len; i++) { 9 char c = data[i]; 10 if (c == ',') { 11 size++; 12 } 13 } 14 int mem[size]; 15 int set = 0; 16 int idx = 0; 17 for (int i = 0; i < len; i++) { 18 char c = data[i]; 19 if (c == ',') { 20 mem[idx] = strtol(data + set, NULL, 10); 21 set = i + 1; 22 idx++; 23 } 24 } 25 if (set < len - 1) { 26 mem[idx] = strtol(data + set, NULL, 10); 27 } 28 mem[1] = 12; 29 mem[2] = 2; 30 for (int i = 0; i < size; i++) { 31 int op = mem[i]; 32 if (op == 1) { 33 int a = mem[i + 1]; 34 int b = mem[i + 2]; 35 int c = mem[i + 3]; 36 mem[c] = mem[a] + mem[b]; 37 i += 3; 38 continue; 39 } 40 if (op == 2) { 41 int a = mem[i + 1]; 42 int b = mem[i + 2]; 43 int c = mem[i + 3]; 44 mem[c] = mem[a] * mem[b]; 45 i += 3; 46 continue; 47 } 48 if (op == 99) { 49 break; 50 } 51 } 52 return mem[0]; 53 }