9 typedef struct state state
;
10 typedef struct elt elt
;
24 char *program
[50]; // sized based on pre-inspecting file
28 get (state
*s
, const char *value
)
31 return s
->regs
[*value
- 'a'];
36 set (state
*s
, const char *reg
, long long value
)
38 s
->regs
[*reg
- 'a'] = value
;
40 printf (" %s=%lld\n", reg
, value
);
44 run (state
*s
, int id
)
47 while (s
->pc
< instr
) {
48 char arg1
[10], arg2
[10];
50 printf ("p%d instr %d at pc %d: %s\n", id
, s
->count
, s
->pc
,
52 sscanf (program
[s
->pc
], "%*s %9s %9s", arg1
, arg2
);
53 switch ((program
[s
->pc
][0] << 8) + program
[s
->pc
][1]) {
54 case ('s' << 8) + 'n': // snd X
55 e
= malloc (sizeof *e
);
65 case ('s' << 8) + 'e': // set X Y
66 set (s
, arg1
, get (s
, arg2
));
68 case ('a' << 8) + 'd': // add X Y
69 set (s
, arg1
, get (s
, arg1
) + get (s
, arg2
));
71 case ('m' << 8) + 'u': // mul X Y
72 set (s
, arg1
, get (s
, arg1
) * get (s
, arg2
));
74 case ('m' << 8) + 'o': // mod X Y
75 set (s
, arg1
, get (s
, arg1
) % get (s
, arg2
));
77 case ('r' << 8) + 'c': // rcv X
78 if (!states
[!id
].head
)
81 states
[!id
].head
= e
->next
;
83 states
[!id
].tail
= NULL
;
87 case ('j' << 8) + 'g': // jgz X Y
88 if (get (s
, arg1
) > 0)
89 s
->pc
+= get (s
, arg2
) - 1;
100 int main(int argc
, char **argv
)
102 char buf
[400]; // sized based on pre-inspecting file
103 int nread
= fread (buf
, 1, sizeof buf
, stdin
);
105 while (p
< buf
+ nread
) {
106 program
[instr
++] = p
;
107 p
= strchr (p
, '\n');
110 printf ("program consists of %d instructions\n", instr
);
111 states
[1].regs
['p' - 'a'] = 1;
113 while ((run (&states
[0], 0) + run (&states
[1], 1)) &&
114 (states
[0].head
|| states
[1].head
)) {
116 printf ("%d+%d operations, %d swaps, %d+%d sent\n", states
[0].count
,
117 states
[1].count
, swap
, states
[0].sent
, states
[1].sent
);
119 printf ("after %d+%d operations and %d swaps, p1 sent %d times\n",
120 states
[0].count
, states
[1].count
, swap
, states
[1].sent
);