TIS, 93 bytes
Code:
@0
MOV ANY ANY
@1
MOV UP ACC
JGZ G
SWP
ADD 1
MOV ACC DOWN
G:SUB 1
MOV ACC ANY
SWP
ADD ANY
SWP
Layout:
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.
Explanation:
Each node has two accumulators, called ACC
and BAK
. BAK
can only be accessed by the SWP
instruction, which swaps the values in ACC
and 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
SWP
and 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).
When ANY
is used as the source, it looks at the node's neighbors in this order: LEFT
, RIGHT
, UP
, DOWN
. When ANY
is used as the destination, it looks at the neighbors in this order: UP
, LEFT
, RIGHT
, 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.
Since 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 ANY
s with LEFT
s.
The reason that the ANY
s in node 0 are of no concern is that node 0 only ever has one neighbor to talk to.
17And I thought you weren't supposed to chain power strips ... – Joey – 2016-02-04T20:00:44.987
As far as I can tell my Retina answer is the only answer using unary input. You might want to have a look at the comment discussion there: http://codegolf.stackexchange.com/questions/71047/electrical-outlet/71062#comment174286_71059 ... If you think that the unary solution is too much of a hack that's not in the spirit of the challenge, I'm happy for you to specify that the input should be in decimal (and will then fix my answer accordingly).
– Martin Ender – 2016-02-05T13:47:34.343@PålGD The tied Jelly answer was posted earlier anyway. – Martin Ender – 2016-02-05T14:21:05.353
7Because electricity is so expensive, your code should be as short as possible as to avoid using more energy – cat – 2016-02-06T00:53:02.383
If a space-separated list of unary numbers is okay, how about a backspace-separated list of unary numbers? – user253751 – 2016-02-06T06:34:02.477
1@cat Time to dig out the old hamster driven turing machine and mechanical computers. – Pharap – 2016-02-06T08:39:22.433
1@immibis sure, but the output would be treated as the information contained in the byte stream not as what happens to by rendered by your terminal. – Martin Ender – 2016-02-06T09:04:34.813