Electrical outlet

23

2

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$$ ,

or

$$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.

Examples

2 3 4
> 7
2 4 6
> 10
1 1 1 1 1 1 1 1
> 1
100 1000 10000
> 11098

Pål GD

Posted 2016-02-04T12:17:32.387

Reputation: 383

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

Answers

24

Jelly, 3 bytes

’S‘

Decrement (all), sum, increment. Try it here.

Lynn

Posted 2016-02-04T12:17:32.387

Reputation: 55 648

29

Retina, 3 bytes

 1

The trailing linefeed is significant.

Input is a space-separated list of unary numbers.

Try it online!

Explanation

The code simply removes all spaces as well as the 1 after them from the string. Here is why that works:

Addition in unary is simple: just concatenate the numbers which is the same as removing the delimiters. Decrementing by 1 is also simple: just remove a 1 from each number. We want 1 more than the sum of the decremented inputs though, so we simply only remove the 1s we find after spaces, thereby decrementing all but the first input.

Martin Ender

Posted 2016-02-04T12:17:32.387

Reputation: 184 808

1I'm wondering if input in unary should be allowed. – John Dvorak – 2016-02-05T11:39:05.000

@JanDvorak it is by default, unless the challenge explicitly specifies decimal input. (See the link in the answer.) Doesn't matter though, Jelly is winning anyway. – Martin Ender – 2016-02-05T12:15:42.653

@MartinBüttner There's sample data both in this question and the original assignment. Don't you think (unless otherwise stated) that it should be a necessary (though not sufficient) criterion for passing, that the code works with the verbatim sample data? – nitro2k01 – 2016-02-05T12:52:35.640

1

@nitro2k01 No (in that case most answers would probably be invalid). Unless the challenge explicitly specifies one particular input format we normally assume that lists can be taken in any native list format. Same goes for number formats (at least unary and taking integers as byte values are allowed by consensus unless the challenge forbids them). It's pretty much impossible to include sample data in every thinkable native input format in the challenge.

– Martin Ender – 2016-02-05T12:56:55.210

@MartinBüttner Imo, that's not the problem that the recommendation is addressing. What still speaks against this is that (unless I'm mistaken) this doesn't work because unary is a supported or native number format in Retina but it happens to work when you process the string as string data. It's a hack. It's even a clever hack, but I'm still not convinced that's it's according to the rules. If space-separated unary numbers was a native format in Retina in the same way that a list of bytes is a native format in bf, I would agree the recommendation applies and I would have a different opinion. – nitro2k01 – 2016-02-05T13:33:30.410

For completeness, perhaps its worth adding to- and from-unary conversion, just because it can be done. Of course a score of 22 is nowhere near as spectacular a 3 ...

– Digital Trauma – 2016-02-06T00:02:30.207

If a space-separated list of unary numbers is okay, how about a backspace-separated list of unary numbers? – user253751 – 2016-02-06T06:34:13.030

9

Hexagony, 18 14 bytes

.?<_(@'")>{+.!

Unfolded:

  . ? <
 _ ( @ '
" ) > { +
 . ! . .
  . . .

Try it online!

I don't think side-length 2 is possible, but there must might be a more efficient side-length 3 solution that this.

This is the usual "decrement all, sum, increment" approach, but I'll have to add diagrams later to show how exactly it works in Hexagony.

Martin Ender

Posted 2016-02-04T12:17:32.387

Reputation: 184 808

7

Python, 24 bytes

lambda*n:1-len(n)+sum(n)

Try it online

Mego

Posted 2016-02-04T12:17:32.387

Reputation: 32 998

1This assumes that the function is first assigned, then applied to the input from other variable. – juandesant – 2016-02-05T10:42:44.040

1@juandesant ...which is perfectly fine. It's a function literal, which is a valid form of submission. – FlipTack – 2017-12-03T17:48:26.943

7

Mathematica, 9 bytes

Tr[#-1]+1&

A Simmons

Posted 2016-02-04T12:17:32.387

Reputation: 4 005

7

Haskell, 17 15 bytes

foldl1$(+).pred

Usage example: ( foldl1$(+).pred ) [2,4,6] -> 10.

Old version, different approach, 17 bytes: succ.sum.map pred.

nimi

Posted 2016-02-04T12:17:32.387

Reputation: 34 639

6

J, 6 bytes

+/+1-#

Sum plus one minus length. Parenthesize and apply it, like so:

   (+/+1-#) 2 3 4
7

Lynn

Posted 2016-02-04T12:17:32.387

Reputation: 55 648

6

Labyrinth, 9 bytes

"?;)!@
+(

Try it online!

The usual primer:

  • Labyrinth is 2D and stack-based. Stacks have an infinite number of zeroes on the bottom.
  • When the instruction pointer reaches a junction, it checks the top of the stack to determine where to turn next. Negative is left, zero is forward and positive is right.

Here we start at the top left ", a no-op, heading rightward. Next is ?, which reads an int from STDIN (throwing away chars it can't parse as an integer, e.g. spaces). Now we have two cases:

If the input is positive, we turn right, performing:

(            decrement top of stack
+            add top two stack elements
             [continue loop]

If the input is zero (which occurs at EOF), we go straight ahead, performing:

;            pop zero from EOF
)            increment top of stack
!            output top of stack as number
@            halt program

Sp3000

Posted 2016-02-04T12:17:32.387

Reputation: 58 729

5

Pyth, 5 bytes

hstMQ

increment(sum(map(decrement, input)))

Lynn

Posted 2016-02-04T12:17:32.387

Reputation: 55 648

5

ES6, 25 bytes

a=>a.map(n=>r+=n-1,r=1)|r

Neil

Posted 2016-02-04T12:17:32.387

Reputation: 95 035

4I was going to post: "One of the rare cases when reduce wins the game" ... and it's 25 too l=>l.reduce((a,b)=>a+b-1). – edc65 – 2016-02-04T14:21:52.310

@edc65 Yeah, the (,b) is costly, but I like that version too. – Neil – 2016-02-04T15:46:00.307

5

MATL, 3 bytes

qsQ

Try it online.

Explanation

qsQ
q      thread decrement over the input array
  s    sum
   Q   increment

a spaghetto

Posted 2016-02-04T12:17:32.387

Reputation: 10 647

4

Starry, 26 24 bytes

, + '`      + ** `, +'*.

Expects newline-separated integers. Try it online!

Thanks to @MartinBüttner for -2 bytes.

,           Read line as integer
 + '        Dupe and jump to label 1 if nonzero
`           Set label 0
      +     Push 1
 *          Sub
*           Add
 `          Set label 1
,           Read line as integer
 + '        Dupe and jump to label 0 if nonzero
*           Add
.           Output as integer

The loop is unrolled so that the first number is not decremented, negating the need to increment. Pushing numbers is expensive in Starry...

Sp3000

Posted 2016-02-04T12:17:32.387

Reputation: 58 729

I count only 20 bytes. – Addison Crump – 2016-02-04T21:11:57.627

1@VoteToClose Did you count the leading spaces? (I'm assuming you're talking about the 26 byte) – Sp3000 – 2016-02-04T21:33:06.680

4

05AB1E, 4 bytes

Code:

E<O>

Explanation:

E     # Evaluates input
 <    # Decrement on list
  O   # Compute the total sum
   >  # Increment on the sum
      # Implicit: output top of the stack

Takes input like an array (e.g. [3, 4, 5]).

Adnan

Posted 2016-02-04T12:17:32.387

Reputation: 41 965

Very elegant for a golfing lang – Pharap – 2016-02-06T08:45:20.247

4

Bash + GNU utilities, 16

If there are N power strips, then there should be N-1 separators in the comma-separated input list. All we need to do is replace the separators with - 1 + and arithmetically evaluate:

sed s/,/-1+/g|bc

Or using the same trick:

Pure Bash (no external utilities), 19

echo $[${1//,/-1+}]

Digital Trauma

Posted 2016-02-04T12:17:32.387

Reputation: 64 644

3

APL (NARS 2000), 13 10 bytes

{1+(+/⍵)-⍴∊⍵}

Edit: Down to 10 with Lynn's (better) approach.

{1++/1-⍨⍵}

Koneke

Posted 2016-02-04T12:17:32.387

Reputation: 121

3

CJam, 7 bytes

q~:(:+)

Test it here.

Same approach as Lynn's (decrement all, sum, increment). This also works for 8 bytes (and is maybe a bit more interesting):

q~{(+}*

This folds "decrement, add" over the list. By doing that, the decrement is only applied to all elements except the first, such that we don't need to take care of the increment separately.

Martin Ender

Posted 2016-02-04T12:17:32.387

Reputation: 184 808

3

gs2, 5 bytes

(CP437-encoded.)

W&Φd'

That’s read-nums dec m1 sum inc.

Lynn

Posted 2016-02-04T12:17:32.387

Reputation: 55 648

3

Seriously, 7 bytes

,;l@Σ-u

Try it online!

Explanation:

,;l@Σ-u
,        push input
 ;       dupe
  l@     push length (n), swap
    Σ-u  push sum, subtract n, add one

Mego

Posted 2016-02-04T12:17:32.387

Reputation: 32 998

3

C, 60 59 55 bytes

x;main(s){while(~scanf("%i",&x))s+=x-1;printf("%i",s);}

stmbgr1

Posted 2016-02-04T12:17:32.387

Reputation: 41

3

Perl 6, 14 bytes

{1+[+] --«@_}

usage

my &f = {1+[+] --«@_}

say f([2,3,4]) # 7
say f([2,4,6]) # 10
say f([1,1,1,1,1,1,1,1]) # 1
say f([100,1000,10000]) # 11098

Hotkeys

Posted 2016-02-04T12:17:32.387

Reputation: 1 015

I fully intended to edit my answer to the very same thing ( see the html comment)

– Brad Gilbert b2gills – 2016-02-05T04:36:16.530

11 bytes: {.sum-$_+1} – nwellnhof – 2018-11-22T23:58:06.000

2

Perl 6, 20 bytes

put 1+sum --«@*ARGS

( You can use << instead of « )

Usage:

$ perl6 -e 'put 1+sum --«@*ARGS' 100 1000 10000
11098

Brad Gilbert b2gills

Posted 2016-02-04T12:17:32.387

Reputation: 12 713

« is a Perl operator? – user253751 – 2016-02-04T19:56:04.553

@immibis Actually it's part of several Perl 6 operators @arraya »+« @arrayb ++«@array @array».method @array»++ « a 'space separated' list of words » Several of those are what is known as Meta operators, in that they combine with other operators. ( Perl 5 doesn't have those operators currently. ) – Brad Gilbert b2gills – 2016-02-04T21:37:39.750

2

Perl 5 23+2=25 or 19+2=21

Requires -ap flags:

map{$.+=($_-1)}@F;$_=$.

Saved in a file and run as

perl -ap file.pl

EDIT: Another answer, smaller (19+2) but basically copied from dev-null answer:

$.+=$_-1for@F;$_=$.

ChatterOne

Posted 2016-02-04T12:17:32.387

Reputation: 171

2

F#, 25 bytes

Seq.fold(fun s n->s+n-1)1

This is a function that takes in an array/list/sequence of integers and returns the required result.

How it works:

Seq.fold allows you to apply a function to every element of a sequence while carrying some state around while it does so. The result of the function as applied to the first element will give the state that will be put into the function for the second element, and so forth. For example, to sum up the list [1; 3; 4; 10], you'd write it like this:

Seq.fold (fun sum element -> sum + element) 0 [1; 3; 4; 10]
         (       function to apply        ) ^ (sequence to process)
                                     ( initial state )

Which would be applied like so:

// First, initial state  + first element
0 + 1  = 1
// Then, previous state + next element until the end of the sequence
1 + 3  = 4
4 + 4  = 8
8 + 10 = 18

With the last state being the return value of Seq.fold.

Roujo

Posted 2016-02-04T12:17:32.387

Reputation: 353

2

, 5 chars / 7 bytes

ï⒭+‡_

Try it here (Firefox only).

Uses a custom encoding with 10-bit chars (thx @Dennis!). Run encode('ï⒭+‡_') in the JS console to get encoded form, and decode(/*ENCODED TEXT HERE*/) to decode encoded form.

Explanation

Translates to Javascript ES6 as:

i=>i.reduce(($,_)=>$+--_)

Mama Fun Roll

Posted 2016-02-04T12:17:32.387

Reputation: 7 234

Interesting encoding. – lirtosiast – 2016-02-04T22:56:38.030

It works quite nicely, too. – Mama Fun Roll – 2016-02-05T02:19:17.757

2

Mornington Crescent, 1909 1873 1839 bytes

Take Northern Line to Stockwell
Take Victoria Line to Seven Sisters
Take Victoria Line to Victoria
Take Circle Line to Victoria
Take Circle Line to Bank
Take Circle Line to Hammersmith
Take Circle Line to Cannon Street
Take Circle Line to Hammersmith
Take Circle Line to Cannon Street
Take Circle Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Hammersmith
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Turnham Green
Take District Line to Hammersmith
Take District Line to Turnham Green
Take District Line to Notting Hill Gate
Take Circle Line to Notting Hill Gate
Take Circle Line to Bank
Take Circle Line to Embankment
Take Northern Line to Stockwell
Take Northern Line to Embankment
Take Circle Line to Temple
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Blackfriars
Take Circle Line to Embankment
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Upminster
Take District Line to Becontree
Take District Line to Parsons Green
Take District Line to Embankment
Take Circle Line to Blackfriars
Take Circle Line to Bank
Take Northern Line to Angel
Take Northern Line to Bank
Take Circle Line to Bank
Take District Line to Upminster
Take District Line to Bank
Take Circle Line to Bank
Take Northern Line to Mornington Crescent

Try it online!

pppery

Posted 2016-02-04T12:17:32.387

Reputation: 3 987

"90% of all instructions involve takeing the District Line." It's because District is where all the arithmetic stations are. However, on TIO, it doesn't seem to work for any of the examples.

– NieDzejkob – 2018-03-09T14:17:41.273

1873 bytes by using shorter line names where possible – NieDzejkob – 2018-03-15T17:04:27.633

TIO's interpreter has a bug and doesn't implement Turnham Green – pppery – 2018-07-29T02:04:18.997

Nice catch. I've sent a PR that fixes it upstream. – NieDzejkob – 2018-07-29T08:33:08.703

1

APL Nars 9 chars

{1++/⍵-1}

test

f←{1++/⍵-1}
  f 2 3 4
7
  f 2 4 6
10
  f 1 1 1 1 1 1 1 1
1
  f 100 1000 10000
11098

RosLuP

Posted 2016-02-04T12:17:32.387

Reputation: 3 036

1

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 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.

Phlarx

Posted 2016-02-04T12:17:32.387

Reputation: 1 366

1

K (oK), 6 bytes

1+/-1+

Try it online!

streetster

Posted 2016-02-04T12:17:32.387

Reputation: 3 635

1

Python 3, 79 bytes

import sys
print(sum(map(lambda x: int(x)-1, sys.stdin.readline().split()))+1)

Pål GD

Posted 2016-02-04T12:17:32.387

Reputation: 383

Looks like you're counting a newline as two bytes. Maybe replace it with a semi-colon to save a byte. A few spaces can be removed too. – Daffy – 2016-02-06T03:06:19.697

1

Perl, 21 + 2 = 23 bytes

$a+=$_-1for@F;say++$a

Requires -a and -E:

$ perl -aE'$a+=$_-1for@F;say++$a'<<<'2 3 4'
7

andlrc

Posted 2016-02-04T12:17:32.387

Reputation: 1 613

You can use the -a flag to get @F variable with already split elements, and replace -n with -p so you dont'need say, reducing it to 21+2: $a+=$_-1for@F;$_=++$a – ChatterOne – 2016-02-04T16:02:14.640

Using -p instead of say is the same because I need to use $_= anyway. – andlrc – 2016-02-04T17:10:09.897

@ChatterOne -a is a good idea! – andlrc – 2016-02-04T17:17:57.130

1

Ruby, 30 bytes

$*.inject(1){|s,v|s+=v.to_i-1}

Simple enough - starting from 1, add up the supplied numbers, each -1 (command line args are in $*). Shame inject is such a long word.

Chowlett

Posted 2016-02-04T12:17:32.387

Reputation: 221

1

PowerShell, 19 bytes

$args-join'-1+'|iex

Note that 1 + p1-1 + p2-1 + ... + pn-1 is equivalent to p1-1 + p2-1 + ... + pn.

Takes input as separate command-line arguments with $args. We -join those together with a -1+ delimiter to create a string, such as 2-1+3-1+4. The string is then piped to Invoke-Expression (similar to eval), and outputs the result.

AdmBorkBork

Posted 2016-02-04T12:17:32.387

Reputation: 41 581

1

Brainfuck, 15 bytes

Assumption: The , operator returns 0 once all input has been exhausted, and there are no extension cords with 0 plugs. Also, the IO needs to be in byte values instead of ASCII character codes.

+>,[-[-<+>],]<.

Explanation: This uses 2 registers. A "Value" accumulator register, representing the number of devices that can be plugged in, and a "current cord" register that keeps track of the value of the current cord. It starts off by incrementing the value by 1, for the existing outlet. Then, for each extension cord, it subtracts one from the value since a plug is being taken up, then increments the value by the number of plugs.

Most online interpreters don't operate in raw byte input mode. To test it online, use this code:

+>,[->-[>+<-----]>---[-<+>]<[-<->]<[-<+>],]<.

Ethan

Posted 2016-02-04T12:17:32.387

Reputation: 271

Can I test the program somewhere? – Pål GD – 2016-02-04T18:12:22.750

Thanks, corrected those mistakes. I'm not aware of any online interpreters that operate in byte mode. I can throw together an implementation that subtracts '0' from the inputs that will run on any online interpreter. – Ethan – 2016-02-04T18:21:52.217

If you want to test out the code, run it here: https://copy.sh/brainfuck/ Don't put spaces in between the numerical values. Unfortunately, since it's operating in ASCII mode, the demo code will only work on single digit values. However, the 15 byte version will work properly on an value <= 255. After you run it, view the memory dump to view the final value.

– Ethan – 2016-02-04T18:29:54.727

One day BF will have proper standards for expected IO and we'll just be able to say 'using standard 3' instead of e.g. 'input and output are all asciii terminated by null char'. – Pharap – 2016-02-06T08:48:34.357

1

PARI/GP, 17 bytes

Completely straightforward.

v->vecsum(v)-#v+1

Charles

Posted 2016-02-04T12:17:32.387

Reputation: 2 435

1

Pylons, 6

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

1(i)-s

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. 

Morgan Thrapp

Posted 2016-02-04T12:17:32.387

Reputation: 3 574

1

R, 24 14 bytes

sum(1,scan()-1)

Edit: Fixed bug!

mnel

Posted 2016-02-04T12:17:32.387

Reputation: 826

@PålGD it was a bug [untested code posted on phone] – mnel – 2016-02-05T09:19:53.983

1

Python, 10+22=32 bytes

s=input();print(sum(s)-len(s)+1)

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

juandesant

Posted 2016-02-04T12:17:32.387

Reputation: 111

0

Dodos, 27 bytes

	dot dip +
+
	
	dot dab dot

Try it online!

Explanation:

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).

Erik the Outgolfer

Posted 2016-02-04T12:17:32.387

Reputation: 38 134

0

Common Lisp, 40 bytes

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

Try it online!

Renzo

Posted 2016-02-04T12:17:32.387

Reputation: 2 260