CARDIAC › Tips and tricks › Comparisons
Store the sign, not the number
Say two players take turns, A and B, and you need to track whose turn it is.
Most people number them 1 and 2. Swapping is 3 minus the turn, which is fine, but finding out whose turn it is means subtracting 2 before you can TAC.
cell contents notes
10 030 INP 30 cells 10-13: whose turn, 1 for A and 2 for B; 3 - turn swaps them
11 132 CLA 32
12 730 SUB 30
13 630 STO 30
14 731 SUB 31 cells 14-19: which is it? a SUB first, then the TAC
15 318 TAC 18
16 531 OUT 31 B's turn: print 2
17 900 HRS 00
18 500 OUT 00 A's turn: print 1
19 900 HRS 00
30 000
31 002
32 003
Step through the natural way in Cardigan
Number them 0 and -1 instead. Swapping becomes -1 minus the turn, and finding out whose turn it is is the TAC on its own. Any time you're choosing between two things, you can store the choice as a sign.
cell contents notes
10 030 INP 30 cells 10-18: whose turn, 0 for A and -1 for B; -1 - turn swaps them
11 131 CLA 31
12 730 SUB 30
13 630 STO 30
14 317 TAC 17 one TAC reads it
15 500 OUT 00 A's turn: print 1
16 900 HRS 00
17 532 OUT 32 B's turn: print 2
18 900 HRS 00
30 000
31 -001
32 002
Step through the CARDIAC way in Cardigan
Both swap A's turn to B's and print 2. Numbering 1 and 2 takes 13 cells and runs 8 instructions; 0 and -1 takes 12 cells and runs 7.
From the tips in
cardiac-code on GitHub. All the tips.