Syscall 2: Firmware, home

ShuffleChapter 8: Scarcity

S29PHOTO FINISH

The task

The race judge, swapped about. Swaps stay inside a core.

P must receive which of X, Y, and Z is largest: 0, 1, or 2.

On a tie the earlier stream wins.

The board

The board of PHOTO FINISH3 cores: sb, c1, and c2. Inputs X, Y, and Z; outputs P.sbSC-2 4 LINESc1SC-2 15 LINESc2SC-2 15 LINESP0P1P0P1P2P0P1P2YXZP
The board as the puzzle screen lays it out. Cyan pins read, lemon pins write, violet pins go both ways. On a narrow screen, scroll the board sideways to see all of it.

Key idea

Par is 6 swaps. Core c1 needs 3 instruction swaps and 1 operand swap; core c2 needs 2 operand swaps. Every one of them trades two tiles that hold each other's places, so each swap mends two.

Hints

Each hint goes a step further than the one before. Open them one at a time.

Hint 1Show

c1 compares X with Y and sends the leader's value, then its number.

Hint 2Show

After SUB B, ADD B gives the value in A back.

Hint 3Show

c2 compares the leader with Z. Only a larger Z wins, so that takes a JG.

Solution

Reveal the Full SolutionThe complete program for PHOTO FINISH. Open it only when you want the answer.

The board as dealt

This is the arrangement the game deals. Framed tiles are the ones out of place.

sbSC-2 standard

IN P0
OUT P1

c1SC-2 standard

OUT P1
MOV A, A
JMP P0
OUT B
JL H
ADD B
SUB P2
MOV A, 0
IN O
H: MOV A, B
OUT P2
MOV B, 1
O: IN P2

c2SC-2 standard

T: IN P0
MOV B, P2
IN A
SUB B
JG H
IN P0
JMP O
H: IN P0
MOV P1, 2
O: OUT A

6 swaps back

Make these swaps in order. Lines are numbered from 1 in each core.

  1. In core c1, swap the operand A on line 2 with B on line 12.
  2. In core c1, swap the instruction OUT on line 1 with IN on line 13.
  3. In core c1, swap the instruction JMP on line 3 with IN on line 9.
  4. In core c1, swap the instruction OUT on line 4 with SUB on line 7.
  5. In core c2, swap the operand P2 on line 2 with A on line 10.
  6. In core c2, swap the operand A on line 3 with P1 on line 9.

The finished program

Every tile back in place: the reference solution, which scores exactly par on every benchmark.

sbSC-2 standard

IN P0
OUT P1

c1SC-2 standard

IN P1
MOV B, A
IN P0
SUB B
JL H
ADD B
OUT P2
MOV A, 0
JMP O
H: MOV A, B
OUT P2
MOV A, 1
O: OUT P2

c2SC-2 standard

T: IN P0
MOV B, A
IN P1
SUB B
JG H
IN P0
JMP O
H: IN P0
MOV A, 2
O: OUT P2
CYCLES
513
at par
ENERGY
4566
at par
NODES
3
at par
INSTRUCTIONS
25
at par
SWAPS
6
at par