Syscall 2: Firmware, home

SignalChapter 10: Meshes

80SORT THREE

The task

X arrives in groups of three. P must receive each group from the smallest to the largest.

c1, c2, and c3 are SC-2Ls: 2 cycles an instruction, at half the energy.

h and j are standard cores: 1 cycle an instruction, at twice the energy.

The board

The board of SORT THREE5 cores: h, c1, c2, c3, and j. Inputs X; outputs P.hSC-2 15 LINESc1SC-2L 15 LINESc2SC-2L 15 LINESc3SC-2L 15 LINESjSC-2 15 LINESP0P1P2P0P1P2P0P1P2P3P0P1P2P0P1P2XP
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

Three comparators in a network sort three values, and the last comparison runs faster on the collector.

Hints

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

Hint 1Show

Three comparisons sort three values: a network of cores, each putting two values in order.

Hint 2Show

c1 orders the first two, and c2 finds the largest. The two smaller values are left to order.

Hint 3Show

Send each value first to the core that is waiting on it.

Solution

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

hSC-2 standard

IN P0
OUT P1
IN P0
OUT P1
IN P0
OUT P2

c1SC-2L low power

IN P0
MOV B, A
IN P0
SUB B
JL S
ADD B
JMP T
S: ADD B
XCH A, B
T: OUT P2
XCH A, B
OUT P1

c2SC-2L low power

IN P0
MOV B, A
IN P1
SUB B
JL S
ADD B
XCH A, B
JMP T
S: ADD B
T: OUT P2
XCH A, B
OUT P3

c3SC-2L low power

IN P0
OUT P2
IN P1
OUT P2

jSC-2 standard

IN P0
MOV B, A
IN P0
SUB B
JL S
ADD B
XCH A, B
JMP T
S: ADD B
T: OUT P2
XCH A, B
OUT P2
IN P1
OUT P2
CYCLES
427
at par
ENERGY
2381
at par
NODES
5
at par
INSTRUCTIONS
48
at par

Another way: All low power

A second program that also passes. It beats par on ENERGY and gives some back on CYCLES.

C3 orders the two smaller values itself, so every comparison runs on a 1 energy core: less energy, but the slow c3 holds the line back.

hSC-2 standard

IN P0
OUT P1
IN P0
OUT P1
IN P0
OUT P2

c1SC-2L low power

IN P0
MOV B, A
IN P0
SUB B
JL S
ADD B
JMP T
S: ADD B
XCH A, B
T: OUT P2
XCH A, B
OUT P1

c2SC-2L low power

IN P0
MOV B, A
IN P1
SUB B
JL S
ADD B
XCH A, B
JMP T
S: ADD B
T: OUT P2
XCH A, B
OUT P3

c3SC-2L low power

IN P0
MOV B, A
IN P1
SUB B
JL S
ADD B
XCH A, B
JMP T
S: ADD B
T: OUT P2
XCH A, B
OUT P2

jSC-2 standard

IN P0
OUT P2
IN P0
OUT P2
IN P1
OUT P2
CYCLES
507
over par
ENERGY
2132
under par
NODES
5
at par
INSTRUCTIONS
48
at par