Syscall 2: Firmware, home

SignalChapter 4: Stars and hubs

29MEDIAN

The task

P must receive the middle value of X, Y, and Z each time.

Four cores, with one wire between each pair.

The board

The board of MEDIAN4 cores: k, m, s, and f. Inputs X, Y, and Z; outputs P.kSC-2 15 LINESmSC-2 15 LINESsSC-2 15 LINESfSC-2 15 LINESP0P1P2P0P1P2P0P1P0P1XYZP
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

A comparator chain: sort two, raise the third to the low, lower it to the high.

Hints

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

Hint 1Show

Sort X and Y into a low and a high first.

Hint 2Show

The median is Z held between the low and the high.

Hint 3Show

Raise Z to at least the low, then lower it to at most the high.

Solution

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

The reference program

The game's own solution. It passes every test and scores exactly par on every benchmark.

kSC-2 standard

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

mSC-2 standard

IN P0
MOV B, A
IN P1
SUB B
JG K
MOV A, 0
K: ADD B
OUT P2
IN P0
OUT P2

sSC-2 standard

IN P0
OUT P1

fSC-2 standard

IN P0
MOV B, A
IN P0
SUB B
JL K
MOV A, 0
K: ADD B
OUT P1
CYCLES
701
at par
ENERGY
6826
at par
NODES
4
at par
INSTRUCTIONS
32
at par