blob: 7afd96903743e210f6389b986d05abab4a4825e2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct Ins {
char op[4];
int arg;
} prog[1024];
static int len;
static int acc;
static int pc;
static void step(void) {
if (!strcmp(prog[pc].op, "acc")) {
acc += prog[pc].arg;
} else if (!strcmp(prog[pc].op, "jmp")) {
pc += prog[pc].arg;
return;
}
pc++;
}
static int terminates(void) {
acc = 0;
pc = 0;
int ran[1024] = {0};
while (!ran[pc]++ && pc < len) {
step();
}
return pc == len;
}
int main(void) {
while (EOF != scanf("%s %d\n", prog[len].op, &prog[len].arg)) {
len++;
}
terminates();
printf("%d\n", acc);
for (int i = 0; i < len; ++i) {
if (!strcmp(prog[i].op, "jmp")) {
strcpy(prog[i].op, "nop");
if (terminates()) break;
strcpy(prog[i].op, "jmp");
} else if (!strcmp(prog[i].op, "nop")) {
strcpy(prog[i].op, "jmp");
if (terminates()) break;
strcpy(prog[i].op, "nop");
}
}
printf("%d\n", acc);
}
|