|
|
@@ -13,18 +13,19 @@ long run(const long* input, size_t n, long noun, long verb); |
|
|
|
int main() { |
|
|
|
char input_str[350]; |
|
|
|
memset(input_str, 0, 350*sizeof(char)); |
|
|
|
scanf("%s", input_str); |
|
|
|
long input[140]; |
|
|
|
char* ptr = input_str; |
|
|
|
size_t i = 0; |
|
|
|
scanf("%s", input_str); |
|
|
|
while (*ptr != 0 && input_str - ptr < 350) { |
|
|
|
while (*ptr != 0) { //read until null |
|
|
|
input[i] = strtol(ptr, &ptr, 10); |
|
|
|
i++; |
|
|
|
ptr++; |
|
|
|
ptr++; //skip the separator character (',') |
|
|
|
} |
|
|
|
size_t size = i; |
|
|
|
long out = run(input, size, 12, 2); |
|
|
|
printf("Value of mem[0] with noun=12, verb=2: %ld\n", out); |
|
|
|
//try noun=0..99, verb=0..99 |
|
|
|
for (i = 0; i < 100; i++) { |
|
|
|
for (long j = 0; j < 100; j++) { |
|
|
|
long out = run(input, size, i, j); |
|
|
@@ -36,6 +37,17 @@ int main() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief run the Intcode state machine with given initial tape |
|
|
|
* |
|
|
|
* replace the values of mem[1] and mem[2] with noun and verb respectively before running |
|
|
|
* |
|
|
|
* @param input is a pointer to the tape |
|
|
|
* @param n is the memory allocated |
|
|
|
* @param noun replaces mem[1] |
|
|
|
* @param verb replaces mem[2] |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
long run(const long* input, size_t n, long noun, long verb) { |
|
|
|
long mem[n]; |
|
|
|
memcpy((void*) mem, (const void*) input, n*sizeof(long)); |