15
\$\begingroup\$

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:

enter image description here

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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ Feb 7 '16 at 11:51
  • 6
    \$\begingroup\$ Kirchoff's current law \$\endgroup\$
    – Luis Mendo
    Feb 7 '16 at 13:35
  • \$\begingroup\$ Related. \$\endgroup\$ Feb 8 '16 at 8:07
  • \$\begingroup\$ Can you specify is we can just create a function that returns the result or actually print/return the result. \$\endgroup\$ May 3 '17 at 17:43

17 Answers 17

14
\$\begingroup\$

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.

\$\endgroup\$
9
\$\begingroup\$

Haskell, 14 bytes

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

\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Congrats on exactly 80k! \$\endgroup\$ Feb 7 '16 at 18:50
4
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ The exact same solution as mine, but with different letters haha, +1 \$\endgroup\$
    – Adnan
    Feb 7 '16 at 13:39
  • \$\begingroup\$ @Adnan I saw! (+1 already) \$\endgroup\$
    – Luis Mendo
    Feb 7 '16 at 15:45
  • \$\begingroup\$ @Adnan I've reduced to 3 bytes changing input order and concatenating both arrays. Maybe that can be applied to your answer as well? \$\endgroup\$
    – Luis Mendo
    Feb 7 '16 at 15:48
  • \$\begingroup\$ Ahhh, I should really implement a concatenate function :p. Very nice answer! :) \$\endgroup\$
    – Adnan
    Feb 7 '16 at 15:52
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 17 11 bytes

Tr@#-Tr@#2&

Quite simple.

\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 40

(lambda(x y)(-(reduce'+ x)(reduce'+ y)))
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

ES6, 39 bytes

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

Because I wanted to use reduceRight.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 30 bytes

a,b=map(sum,input());print a-b
\$\endgroup\$
1
\$\begingroup\$

Pyth, 6 bytes

-.*sRQ

Explanation

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

Try it here

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Pyth, 5 bytes

-FsMQ

Try it online. Test suite.

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

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

\$\endgroup\$
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 5 chars / 7 bytes

⨭î-⨭í

Try it here (Firefox only).

Wut.

Explanation

sum(input1) - sum(input2)

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

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.