A well-known psychology experiment has two players repeatedly play a game in which, on each encounter, each player independently chooses to either TRADE with the other player or CHEAT them. The score for a single encounter is:
Many people cannot find a winning strategy, or fail to stick to one, so it is fairer to compare strategies by simulating them on a computer. Each strategy is played by an automaton with three parts: a program that encodes the strategy, a memory of previous encounters, and a running score. The score starts at 0 and is updated after every encounter using the rules above. The memory can look up what happened in up to the last two encounters against the current opponent.
Read up to 10 strategies. Play every strategy against every other strategy (never against itself) for exactly 10 encounters, keeping a separate memory for each pair of opponents. On every encounter both automata choose their move simultaneously from the memory of earlier encounters, then both memories are updated. After all matches, report each strategy's final score.
Each strategy is a small program in the following grammar:
<program> ::= <statement>.
<statement> ::= <command> | <ifstat>
<ifstat> ::= IF <condition> THEN <statement> ELSE <statement>
<condition> ::= <cond> | <cond> <op> <condition>
<op> ::= AND | OR
<cond> ::= <memory> {= | #} {<command> | NULL}
<memory> ::= {MY | YOUR} LAST {1 | 2}
<command> ::= TRADE | CHEAT
a op b op c means a op (b op c)).For example, these are all valid programs:
CHEAT.
IF MY LAST1 = CHEAT THEN TRADE ELSE CHEAT.
IFYOURLAST2=NULLTHENTRADEELSEIFYOURLAST1=TRADETHENTRADE
ELSECHEAT.
The input is a series of programs. Each program is at most 255 characters long and may be split across several lines for convenience. There are at most 10 programs. The input ends with a line that contains only a single '#'.
Print one line per program, in input order. Each line contains that program's final score, right-justified in a field of width 3.