瀏覽代碼

comments

master
red 4 年之前
父節點
當前提交
b11ebce381
共有 1 個檔案被更改,包括 15 行新增3 行删除
  1. +15
    -3
      day2/day2.c

+ 15
- 3
day2/day2.c 查看文件

@@ -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));

Loading…
取消
儲存