Syscall 2: Firmware, home

SignalChapter 6: Trees

46NEAREST

The task

P must receive 0 when Y is nearer to X than Z is, and 1 when Z is nearer.

On a tie, send 0.

The board

The board of NEAREST4 cores: s, lb, lc, and j. Inputs X, Y, and Z; outputs P.sSC-2 5 LINESlbSC-2 15 LINESlcSC-2 15 LINESjSC-2 15 LINESP0P1P2P0P1P2P0P1P2P0P1P2XYZP
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

Two distances from one shared value compared where the paths meet.

Hints

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

Hint 1Show

A distance is a difference with its sign dropped.

Hint 2Show

Y and Z each need their own copy of X.

Hint 3Show

Compare the two distances where the paths meet.

Solution

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

sSC-2 standard

IN P0
OUT P1
OUT P2

lbSC-2 standard

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

lcSC-2 standard

IN P1
MOV B, A
IN P0
SUB B
JL K
NEG
K: OUT P2

jSC-2 standard

IN P0
MOV B, A
IN P1
ADD B
JG O
MOV A, 0
JMP F
O: MOV A, 1
F: OUT P2
CYCLES
341
at par
ENERGY
5496
at par
NODES
4
at par
INSTRUCTIONS
26
at par

Another way: Both positive

A second program that also passes, by a different road, at a cost on CYCLES and ENERGY.

With both distances sent as positive and the judge subtracting them.

sSC-2 standard

IN P0
OUT P1
OUT P2

lbSC-2 standard

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

lcSC-2 standard

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

jSC-2 standard

IN P1
MOV B, A
IN P0
SUB B
JG O
MOV A, 0
JMP F
O: MOV A, 1
F: OUT P2
CYCLES
459
over par
ENERGY
5504
over par
NODES
4
at par
INSTRUCTIONS
26
at par