# Apply Kirchhoff's law

Kirchhoff's law says that when you sum up all the currents (positive for the currents going to a junction, and negative for current leaving a junction), you will always get as result 0.

Look at the following diagram: Using Kirchhoff's law, you can see that i1 + i4 - i2 - i3 = 0, so i1 + i4 = i2 + i3.

Given two lists, one with all the currents entering the junction and one with all the currents leaving the junction except one, output the last one.

Testcases:

[1, 2, 3], [1, 2] = 3
[4, 5, 6], [7, 8] = 0
[5, 7, 3, 4, 5, 2], [8, 4, 5, 2, 1] = 6


The second list always has one item less than the first list. The output cannot be negative. Smallest program wins.

• The puzzle would have been better if you actual gave resistor and current values. This Q seems like you have introduced the law just as a namesake. (The Q could have easily been stated without the law) Feb 7 '16 at 11:51
• Kirchoff's current law Feb 7 '16 at 13:35
• Feb 8 '16 at 8:07
• Can you specify is we can just create a function that returns the result or actually print/return the result. May 3 '17 at 17:43

# Jelly, 2 bytes

_S


Try it here!

Takes the entering currents in the first argument, and the leaving currents in the second argument. _ subtracts them pairwise, leaving the single element from the longer list as-is, and S sums the result.

(.sum).(-).sum


Usage example: ( (.sum).(-).sum ) [5,7,3,4,5,2] [8,4,5,2,1] -> 6.

Sum each list and take the difference.

## CJam, 8 6 bytes

q~.-:+


Input uses two CJam-style arrays.

Run all test cases. (This reads multiple test cases at once and includes a framework to process each line individually, discarding the expected result from the input.)

### Explanation

q~  e# Read and evaluate input.
.-  e# Elementwise difference.
:+  e# Get sum.


.- works reliably because we're guaranteed that the first list is always longer than the second. (Otherwise, the extraneous elements of the second list would be appended to the result which would add them to the sum instead of subtracting them.)

• Congrats on exactly 80k! Feb 7 '16 at 18:50

# MATL, 3 4.0 bytes

_hs


Inputs are: leaving currents first, then entering currents.

Try it online!

_     % implicitly input array with leaving currents (except one). Negate
h     % implicitly input array with entering currents. Concatenate
s     % sum of all elements in concatenated array

• The exact same solution as mine, but with different letters haha, +1 Feb 7 '16 at 13:39
• @Adnan I've reduced to 3 bytes changing input order and concatenating both arrays. Maybe that can be applied to your answer as well? Feb 7 '16 at 15:48
• Ahhh, I should really implement a concatenate function :p. Very nice answer! :) Feb 7 '16 at 15:52

# Javascript, 36 bytes

(a,b)=>eval(a.join++'-'+b.join-)


f=
(a,b)=>
eval(
a.join+
+'-'+
b.join-
)

document.body.innerHTML = '<pre>' +
'f([1,2,3],[1,2]) = ' + f([1,2,3],[1,2]) + '\n' +
'f([4,5,6],[7,8]) = ' + f([4,5,6],[7,8]) + '\n' +
'f([5,7,3,4,5,2],[8,4,5,2,1]) = ' + f([5,7,3,4,5,2],[8,4,5,2,1]) + '\n' +
'</pre>'

# 05AB1E, 4 bytes

Code:

OEO-


Explanation:

O     # Take the sum of the input list
E    # Evaluate input
O   # Take the sum of the input list
-  # Substract from each other


Thanks to Luis Mendo for reminding me that I need to implement a concatenate function. If I've had implemented it sooner, it would have been 3 bytes:

Non-competing version (3 bytes):

The first list is the leaving current list, the second is the entering current list. Code:

(«O


Explanation:

(    # Negate the list, e.g. [3, 4, 5] would become [-3, -4, -5]
«   # Concatenate the second list to the first
O  # Take the sum and implicitly output it


Uses CP-1252 encoding.

# Mathematica, 17 11 bytes

Tr@#-Tr@#2&


Quite simple.

## Common Lisp, 40

(lambda(x y)(-(reduce'+ x)(reduce'+ y)))


# Perl 6, 11 bytes

*.sum-*.sum


### Usage:

# give it a lexical name
my &code = *.sum-*.sum;

say code [1, 2, 3], [1, 2]; # 3
say code [4, 5, 6], [7, 8]; # 0
say code [5, 7, 3, 4, 5, 2], [8, 4, 5, 2, 1]; # 6


# Python 3, 24 bytes

lambda a,b:sum(a)-sum(b)


or

# Python 2, 19 bytes

print sum(a)-sum(b)


depending if I am required to print the result or just create a function that returns it.

## ES6, 39 bytes

(i,o)=>i.reduceRight((r,a,j)=>r+a-o[j])


Because I wanted to use reduceRight.

# Python 2, 30 bytes

a,b=map(sum,input());print a-b


## Pyth, 6 bytes

-.*sRQ


Explanation

       - autoassign Q = eval(input())
sRQ - map(sum, Q)
-.*    - imp_print(minus(*^))


Try it here

# K5, 5 bytes

-/+/'


Difference over (-/) sum over (+/) each (').

In action:

  (-/+/')'((1 2 3;1 2);(4 5 6;7 8);(5 7 3 4 5 2;8 4 5 2 1))
3 0 6


# Pyth, 5 bytes

-FsMQ


Map sum on both input lists, then Fold subtraction (-).

This could also be written as -sQsE, which takes the lists on two lines.

# 𝔼𝕊𝕄𝕚𝕟, 5 chars / 7 bytes

⨭î-⨭í


Try it here (Firefox only).

Wut.

# Explanation

sum(input1) - sum(input2)

# Common Lisp REPL, SBCL 28 24 bytes

write this into REPL:

#.(-(+ #1=,@(read))#1#)


then write input lists like this:

(2 3 4)
(2 3)


I hope it's okay to use such list format (instead of e.g. '(2 3 4)) I used coredump's answer as formula for my solution and then achieved his calculation effect in a different way.

### Explanation

Let e_1,...,e_n be elements of the first list and f_1,...,f_{n-1} be elements of second list. We want to evaluate expression (-(+ e_1 e_2 ... e_n)f_1 f_2 ...f_{n-1}) It would mean subtracting elements of second list from sum of elements of first list. The needed expression is constructed like so:

backqoute stops evaluation

#1= saves a bit of writting, remembering ,@(read)

,@ stops effects of backquote (so that (read) will be evaluated) and takes elements out of a list.

(read) asks for input

#1# "loads" Lisp object saved by #1=

#.` does evaluation of printed representation of an Lisp object