SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Comparisons

Encode so one comparison decides

Say two cards meet, and the winner is the trump if exactly one of them is a trump, and otherwise the higher card.

You'd probably keep each card as a value plus a trump flag, and then ask three questions: is only b a trump, is only a a trump, and which value is higher?

cell  contents  notes
 10      040    INP 40  cells 10-13: each card as a value, 0-9, and a trump flag, 1 or 0
 11      041    INP 41
 12      042    INP 42
 13      043    INP 43
 14      141    CLA 41  cells 14-16: b is a trump and a isn't: b wins
 15      743    SUB 43
 16      325    TAC 25
 17      143    CLA 43  cells 17-19: a is a trump and b isn't: a wins
 18      741    SUB 41
 19      323    TAC 23
 20      140    CLA 40  cells 20-26: both or neither: the higher value wins
 21      742    SUB 42
 22      325    TAC 25
 23      540    OUT 40
 24      900    HRS 00
 25      542    OUT 42
 26      900    HRS 00
 40      000
 41      000
 42      000
 43      000

Step through the natural way in Cardigan

Instead, store a trump with 10 added. Now every trump is higher than every plain card, "higher wins" is the whole rule, and one compare settles it. It's a nice example of choosing your numbers so the comparison you'd make anyway gives the answer you want.

cell  contents  notes
 10      040    INP 40  cells 10-18: a plain card is 0-9 and a trump is stored with 10 added; one compare decides
 11      041    INP 41
 12      140    CLA 40
 13      741    SUB 41
 14      317    TAC 17
 15      540    OUT 40
 16      900    HRS 00
 17      541    OUT 41
 18      900    HRS 00
 40      000
 41      000

Step through the CARDIAC way in Cardigan

A plain 9 meets the trump 2, and the trump wins: the first version prints 2, and the second prints the trump the way it stores it, 12. The flags take 21 cells and run 9 instructions; the encoding takes 11 cells and runs 7.

From the tips in cardiac-code on GitHub. All the tips.