Apply Kirchhoff's law

15

1

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.

Sixty-six

Posted 2016-02-07T11:33:34.367

Reputation: 167

1The 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) – ghosts_in_the_code – 2016-02-07T11:51:49.917

5Kirchoff's current law – Luis Mendo – 2016-02-07T13:35:03.800

Related. – Stewie Griffin – 2016-02-08T08:07:08.440

Can you specify is we can just create a function that returns the result or actually print/return the result. – tpvasconcelos – 2017-05-03T17:43:19.377

Answers

14

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.

Lynn

Posted 2016-02-07T11:33:34.367

Reputation: 55 648

9

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.

nimi

Posted 2016-02-07T11:33:34.367

Reputation: 34 639

5

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

Martin Ender

Posted 2016-02-07T11:33:34.367

Reputation: 184 808

1Congrats on exactly 80k! – ETHproductions – 2016-02-07T18:50:02.443

4

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

Luis Mendo

Posted 2016-02-07T11:33:34.367

Reputation: 87 464

The exact same solution as mine, but with different letters haha, +1 – Adnan – 2016-02-07T13:39:28.250

@Adnan I saw! (+1 already) – Luis Mendo – 2016-02-07T15:45:48.560

@Adnan I've reduced to 3 bytes changing input order and concatenating both arrays. Maybe that can be applied to your answer as well? – Luis Mendo – 2016-02-07T15:48:50.503

Ahhh, I should really implement a concatenate function :p. Very nice answer! :) – Adnan – 2016-02-07T15:52:23.217

3

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.

Adnan

Posted 2016-02-07T11:33:34.367

Reputation: 41 965

3

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

removed

Posted 2016-02-07T11:33:34.367

Reputation: 2 785

2

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.

tpvasconcelos

Posted 2016-02-07T11:33:34.367

Reputation: 129

2

Mathematica, 17 11 bytes

Tr@#-Tr@#2&

Quite simple.

LegionMammal978

Posted 2016-02-07T11:33:34.367

Reputation: 15 731

2

Common Lisp, 40

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

coredump

Posted 2016-02-07T11:33:34.367

Reputation: 6 292

2

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

Brad Gilbert b2gills

Posted 2016-02-07T11:33:34.367

Reputation: 12 713

1

ES6, 39 bytes

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

Because I wanted to use reduceRight.

Neil

Posted 2016-02-07T11:33:34.367

Reputation: 95 035

1

Python 2, 30 bytes

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

orlp

Posted 2016-02-07T11:33:34.367

Reputation: 37 067

1

Pyth, 6 bytes

-.*sRQ

Explanation

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

Try it here

Blue

Posted 2016-02-07T11:33:34.367

Reputation: 26 661

1

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

JohnE

Posted 2016-02-07T11:33:34.367

Reputation: 4 632

0

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

user65167

Posted 2016-02-07T11:33:34.367

Reputation:

0

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.

PurkkaKoodari

Posted 2016-02-07T11:33:34.367

Reputation: 16 699

0

, 5 chars / 7 bytes

⨭î-⨭í

Try it here (Firefox only).

Wut.

Explanation

sum(input1) - sum(input2)

Mama Fun Roll

Posted 2016-02-07T11:33:34.367

Reputation: 7 234