Syscall 2: Firmware, home

ShuffleChapter 6: Trees

S22LOSING STREAK

The task

The streak counter, swapped about. Swap its pieces back.

For each value, P must receive how many equal values in a row end with it.

The board

The board of LOSING STREAK3 cores: c0, c1, and c2. Inputs X; outputs P.c0SC-2 15 LINESc1SC-2 15 LINESc2SC-2 15 LINESP0P1P0P1P0P1XP
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 4 swaps. Core c1 needs 1 operand swap; core c2 needs 1 instruction swap and 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 sends each value minus the one before it, then keeps the value in B.

Hint 2Show

A difference of 0 means the streak goes on. Anything else starts it again at 1.

Hint 3Show

c2 keeps the count in B, and XCH brings it into A to add 1.

Solution

Reveal the Full SolutionThe complete program for LOSING STREAK. 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.

c0SC-2 standard

IN P0
OUT P1

c1SC-2 standard

IN P0
SUB B
OUT P1
ADD A
MOV B, B

c2SC-2 standard

T: IN P0
JZ S
XCH A, A
MOV A, 1
JMP O
S: MOV B, B
ADD 1
MOV B, A
O: OUT P1

4 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 4 with B on line 5.
  2. In core c2, swap the operand A on line 3 with 1 on line 4.
  3. In core c2, swap the operand A on line 4 with B on line 6.
  4. In core c2, swap the instruction XCH on line 3 with MOV on line 6.

The finished program

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

c0SC-2 standard

IN P0
OUT P1

c1SC-2 standard

IN P0
SUB B
OUT P1
ADD B
MOV B, A

c2SC-2 standard

T: IN P0
JZ S
MOV A, 1
MOV B, A
JMP O
S: XCH A, B
ADD 1
MOV B, A
O: OUT P1
CYCLES
279
at par
ENERGY
3042
at par
NODES
3
at par
INSTRUCTIONS
16
at par
SWAPS
4
at par