11 char *program
[50]; // sized based on pre-inspecting file
14 get (const char *value
)
17 return regs
[*value
- 'a'];
22 set (const char *reg
, int value
)
25 printf (" setting %s to %d\n", reg
, value
);
26 regs
[*reg
- 'a'] = value
;
29 int main(int argc
, char **argv
)
32 char buf
[250]; // sized based on pre-inspecting file
33 int nread
= fread (buf
, 1, sizeof buf
, stdin
);
35 while (p
< buf
+ nread
) {
40 printf ("program consists of %d instructions\n", instr
);
44 memset (cache
, 0, sizeof cache
);
46 printf ("attempting execution with a=%d\n", initial
);
50 while (pc
< instr
&& expected
< 50) {
51 char arg1
[10], arg2
[10];
55 printf ("count=%lld pc=%d a=%d b=%d c=%d d=%d, executing %s\n", count
,
56 pc
, regs
[0], regs
[1], regs
[2], regs
[3], program
[pc
]);
57 sscanf (program
[pc
], "%*s %9s %9s", arg1
, arg2
);
58 switch (program
[pc
][0]) {
61 set (arg2
, get (arg1
));
64 set (arg1
, get (arg1
) + 1);
67 set (arg1
, get (arg1
) - 1);
74 line
= get (arg1
) + pc
;
77 printf (" rewriting instruction %d, was %s\n", line
, program
[line
]);
78 switch (*program
[line
]) {
80 memcpy (program
[line
], "dec", 3);
84 memcpy (program
[line
], "inc", 3);
87 memcpy (program
[line
], "jnz", 3);
90 memcpy (program
[line
], "cpy", 3);
99 if (line
!= (expected
& 1)) {
100 //if (getenv ("DEBUG"))
101 printf ("after %lld ops and %d out, got %d, with initial a=%d\n",
102 count
, expected
, line
, initial
);
106 printf (" %d %d %d %d: %d\n", regs
[0], regs
[1], regs
[2], regs
[3], line
);
108 if (!memcmp (regs
, cache
, sizeof regs
))
109 printf ("same state seen, looping sequence found?\n");
110 memcpy (cache
, regs
, sizeof regs
);
121 printf ("potential candidate for start %d, with %d out\n", initial
,
123 if (potential
++ < 10)
126 printf ("after %lld operations, final content of register a: %d\n",