This is a problem from NCPC 2005. Roy has an apartment with only one single electrical outlet, but he has a bunch of power strips. Compute the maximum number of outlets he can have using the power strips he has. The number of outlets per power strip is given as input.

It turns out that if the number of outlets of the strips respectively are

$$p_1, p_2, \dots, p_n$$

then the number of outlets is $$1 - n + \sum_i p_i$$ ,


$$1 + p_1-1 + p_2-1 + \dots + p_n-1$$.

The input to the program or function is a non-empty series of positive integers.


2 3 4
> 7
2 4 6
> 10
1 1 1 1 1 1 1 1
> 1
100 1000 10000
> 11098
  • 17
    \$\begingroup\$ And I thought you weren't supposed to chain power strips ... \$\endgroup\$
    – Joey
    Feb 4 '16 at 20:00
  • \$\begingroup\$ 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: codegolf.stackexchange.com/questions/71047/electrical-outlet/… ... 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). \$\endgroup\$ Feb 5 '16 at 13:47
  • 7
    \$\begingroup\$ Because electricity is so expensive, your code should be as short as possible as to avoid using more energy \$\endgroup\$
    – cat
    Feb 6 '16 at 0:53
  • 1
    \$\begingroup\$ @cat Time to dig out the old hamster driven turing machine and mechanical computers. \$\endgroup\$
    – Pharap
    Feb 6 '16 at 8:39
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Feb 6 '16 at 9:04

39 Answers 39


PARI/GP, 17 bytes

Completely straightforward.


Pylons, 6

Takes a list of space separated numbers on the command line then sums them all and subtracts the length.


How it works:

1   # Push 1 to the stack.
(   # Start a list.
 i  # Get command line input.
  ) # End a list.
-   # Subtract the top two elements of the stack. In the case where one of the elements is
    # a list, it does matrix subtraction.
s   # Sum the stack.
    # Print the stack implicitly. 

R, 24 14 bytes


Edit: Fixed bug!

  • \$\begingroup\$ @PålGD it was a bug [untested code posted on phone] \$\endgroup\$
    – mnel
    Feb 5 '16 at 9:19

Python, 10+22=32 bytes


Main issue is not having a default input variable in Python, or implicit printing.


APL Nars 9 chars



  f 2 3 4
  f 2 4 6
  f 1 1 1 1 1 1 1 1
  f 100 1000 10000

TIS, 93 bytes




1 2 CC

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 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 ANYs with LEFTs.

The reason that the ANYs in node 0 are of no concern is that node 0 only ever has one neighbor to talk to.


K (oK), 6 bytes


Try it online!


Dodos, 27 bytes

	dot dip +
	dot dab dot

Try it online!


The main function (top line, implicitly named main) is called at the start of the program. It contains this command:

dot dip +

+ is called on the tuple given as separate command-line arguments. This is +:

dot dab dot

The empty command simply returns the argument as-is. The other command, dot dab dot, returns 0. It has to be able to convert any given tuple to zero, so here's how this does it, right-to-left:

First of all, dot returns a 1-element tuple that contains the sum of the input's elements. The actual sum doesn't matter here, what matters is that dot will always return a 1-element tuple, so that we can use dab on it to remove its first (i.e. only) element, since there's no built-in to empty a tuple. The sum of an empty tuple is 0, so we just dot this empty tuple to return the singleton 0.

After this process, the two results of the + function (the input and 0) are implicitly concatenated. We now return to main.

The dip command will decrement each positive number by 1, and convert each 0 to 1, in +'s result. This will decrement each number of available outlets, and also increment the final 0, so we don't miss a free outlet.

Finally, the tuple will be dotted again, so we're computing the number of available outlets over all of the power strips, minus the number of power strips, plus one (no strip occupies an outlet of the last strip).


Common Lisp, 40 bytes

(lambda(x)(1+(-(reduce'+ x)(length x))))

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.