TIS, 93 bytes
MOV ANY ANY
MOV UP ACC
MOV ACC DOWN
MOV ACC ANY
1 2 CC
I1 NUMERIC -
O1 NUMERIC -
Try it online!
Note that this solution requires the list to be terminated by a zero (or more accurately, a non-positive value).
With TIS, we'd need to choose one of the following a) a terminating character, b) a length-prefixed array, or c) a running output for every value. Otherwise, a program would never print any output because there (theoretically) could be more input on the way.
Each node has two accumulators, called
BAK can only be accessed by the
SWP instruction, which swaps the values in
BAK. In the following, I will use variables X and Y to track the values more directly. I'll also use Z as shorthand for the entire node 0.
Additionally, each node implicitly loops back to the top when it reaches the bottom.
| Input 0 | Input as whitespace-delimited numbers
| Node 0 | | Node 1 | |
Node 0 is just an +--------+ +--------+ |
extra memory cell | MOV ANY ANY | MOV UP ACC | Put input value into X
so we can perform | | JGZ G | If X>0, jump to label G
logic on input | | SWP | Swap focus to Y
values easier | | ADD 1 | Y++
| | MOV ACC DOWN | Output the value Y
| | | (end program -- see note 1 below)
| | G:SUB 1 | Label G: X--
| | MOV ACC ANY | Z = X (also see note 2 below)
| | SWP | Swap focus to Y
| | ADD ANY | Y += Z (also see note 2 below)
| | SWP | Swap focus to X
| Output 0 | Output as whitespace-delimited numbers
Note 1, regarding the termination of this program:
The TIS emulator I am using here will terminate the program once there is no more active processing. The most common reason for getting into such a state is to exhaust all available input, though things like deadlocks can also get us to this state.
Once the value in this program has been output, calculation will continue on at label G, mangling the data in the registers (it forgets which register is X and which is Y). However, it will quickly return to the line that reads input, and because the input will now be exhausted, the program will terminate.
Some minor modifications may be made to adjust the program to handle data after the terminator:
- Adding the instruction
HCF just before the label G will cause the program to forcibly terminate at this point instead of relying on input exhaustion.
- Adding the instructions
JRO -99 just before the label G will cause the program to return to a good state, then jump back to top (relative jumps can't wrap, so anything further than -15 will always go to the top line; offhand, I think -7 is the exact value needed here). This turns the terminator into a 'print' instruction instead.
- The value Y could be reset to zero, in addition to the jump described above, to treat extended input as two or more distinct invocations.
Note 2, regarding the use of
ANY in node 1:
The use of
ANY on the two marked lines of Node 1 is possible due to an implementation detail (this quirk is true in both this emulator and in the original Zachtronics game).
ANY is used as the source, it looks at the node's neighbors in this order:
ANY is used as the destination, it looks at the neighbors in this order:
DOWN. (The destination ordering is a consequence of both the source ordering and the order in which the nodes are run.)
Of course, the nodes not only need to be present but also be ready to send/receive a value to be selected.
LEFT appears before
UP as a source, and before
DOWN as a destination,
ANY will always be
LEFT in this case.
To make this program "implementation-independent", simply replace those two
The reason that the
ANYs in node 0 are of no concern is that node 0 only ever has one neighbor to talk to.