Where does the pivot belong?

You will be given a positive integer as input.

The integer is the board of a seesaw.

Th integer will not have leading zeroes. You may take this input however you like.

Your task is to output the location of the pivot point of this seesaw, such that the board would balance.

A board balances if the moments on each side are equal in magnitude. A moment is calculated by multiplying the force by the distance from the pivot point. In this case, the force is equal to the digit.

For the integer 100 with a pivot point below the first 0, the moment on the left is 1x1=1. The moment on the right is 0x1=0. If the pivot point was below the second 0, the moment on the left would be 2x1=2 (because the 1 is now 2 away from the pivot).

For example, for the integer 31532 the pivot goes underneath the number 5. This is because the moments on the left are 3x2 + 1x1 = 7 and on the right the moments are 3x1 + 2x2 = 7.

The output for this integer is 3 because the pivot goes underneath the 3rd digit.

If the integer cannot be balanced, your code does not have to do anything - it can hang, error, output nothing - whatever you want.

Note that a pivot cannot go between two numbers. The pivot location must be an integer.

Test cases:

31532 -> 3
101 -> 2
231592 -> 4
8900311672 -> 5


Standard loopholes apply, this is code golf so shortest answer wins.

• Can I not handle the last testcase if it is too large? – Leaky Nun Jul 15 '17 at 2:46
• Can the output be 0-indexed? – Dennis Jul 15 '17 at 4:41
• Can the output be indexed from the front of the number? Your test cases seem to do this. – Wheat Wizard Jul 15 '17 at 4:42
• Related – Peter Taylor Jul 15 '17 at 6:52
• Is a list of digits a valid input format? – Lynn Jul 15 '17 at 9:07

Haskell, 646259574535 31 bytes

d l=sum(zipWith(*)[1..]l)/sum l


Try it online!

• (:[]) is pure. – nimi Jul 15 '17 at 10:04
• I think testing for <1 instead of ==0 should be enough. We get a list of all not right-heavy seesaws, but we are picking the first element anyway. – nimi Jul 15 '17 at 10:19
• @nimi doing (<1) is pretty clever. Thanks for the tips. – Wheat Wizard Jul 15 '17 at 13:34
• @flawr It feels like I should but $ has lower precedence than < preventing me. – Wheat Wizard Jul 15 '17 at 15:37 • Oh you're right! As an alternative I think you could use until instead of the [x|x<-...]!!0 (not tested: until(\x->sum(zipWith(*)[1-x..]l<1)(+1)0) (coding on the phone is a little bit cumbersome :P) – flawr Jul 15 '17 at 15:39 R, 66 bytes sum(1:length(n<-strtoi(strsplit(paste(scan()),"")[[1]]))*n)/sum(n)  Try it online! • You can take input as string by doing scan(,'') instead of using paste. – JAD Jul 15 '17 at 13:46 Jelly, 7 bytes Dµæ.J:S  • You’ve done this before for some reason? 😂 – Tim Jul 15 '17 at 2:11 • @Tim No, it's just a formula used in physics - sum of moments / sum of digits. – Leaky Nun Jul 15 '17 at 2:11 • @Tim He's known for posting answers extremely quickly to questions; sometimes, he posts an answer in the same minute as the question :P – HyperNeutrino Jul 15 '17 at 2:29 J, 29 bytes [:>:@(+/%~]+/@:*i.@$)10#.inv]


Try it online!

• 25 bytes [:(+/<.@%~]+/ .*#\),.&.": using a different method to get the list of digits. – miles Jul 15 '17 at 6:38
• Nice, didn't see this before I posted mine. You beat me by a margin! And @miles made it even better. – Dan Bron Jul 16 '17 at 1:45
• In all honesty I don't think I've seen this many J golfers (5) in one place before :P – Conor O'Brien Jul 16 '17 at 4:00

Brain-Flak, 64 bytes

({({}[((((()()()){}){}){}){}]<>{})<>}<>)({()<({}[({})])>}<>)


Try it online!

60+4 bytes for the -ar flags.

Explanation

 {                                  }     for each digit in input (starting at the end)
{}                                     get digit as ASCII code
[((((()()()){}){}){}){}]             subtract 48 to get digit as number
(                          <>{})<>      add to cumulative sum on second stack
(                                    <>)  push total of all cumulative sums (the sum of moments)

{              }       while sum of moments != 0
({}[({})])         subtract sum of weights
()<          >        count the number of iterations needed
(                <>)    push onto first stack and implicitly output

• Usually you can save bytes by using BrainHack instead of -a, but BrainHack doesn't have -r yet so you wont be able to this time. – Wheat Wizard Jul 15 '17 at 14:21
• Also because of the flexible input it seems you can take input as a list of ints instead of requiring a string. This could save you a bunch of bytes. – Wheat Wizard Jul 16 '17 at 3:23

Python 3, 66 bytes

f=lambda n,a=0,b=0,c=1:n and f(n//10,a+n%10*c,b+n%10,c+1)or c-a//b


Try it online!

• +1, Exactly half the length of my answer.... :D – officialaimm Jul 15 '17 at 3:26

Brachylog, 14 bytes

ẹ⟨{iᶠ×ᵐ+}÷+⟩+₁


Try it online!

GolfScript (24 bytes)

0:i.@{15&.@+@@i):i*+\}//


Online demo

C (gcc), 72 bytes

a,b,c;f(long n){a=b=c=0;for(;n;a+=n%10*++c,n/=10)b+=n%10;return-~c-a/b;}


Try it online!

• Since a,b,c are declared in an enclosing scope, they can be initialised in the initialiser of the for loop, saving a semicolon. – Peter Taylor Jul 15 '17 at 12:55
• funnily also a valid solution in JS (slightly altered) f=n=>{for(a=b=c=0;n;a+=n%10*++c,n=n/10|0)b+=n%10;return-~c-a/b} – Oki Jul 15 '17 at 22:50

MATL, 12 bytes

48-ttn:*sws/


Try it online!

Mathematica, 42 bytes

Range@Length@#.#/Total@#&@IntegerDigits@#&

• Tr saves 4 byte, but breaks Mathics compatibility. – user202729 Jul 15 '17 at 7:34

Python 3, 69 bytes

x=[*map(int,input())]
print(sum(v*k for k,v in enumerate(x))//sum(x))


Try it online!

-7 bytes and fixed thanks to ovs

• But it doesn't even work – Leaky Nun Jul 15 '17 at 2:36
• Adding a closing bracket to the first line fixes the syntax, but then 3 is printed instead of 2. – Leaky Nun Jul 15 '17 at 2:37
• Slightly golfed and fixed – ovs Jul 15 '17 at 6:52
• @LeakyNun It outputs 2 for me, which seems to be right to me if this is 0-indexed. – ovs Jul 15 '17 at 6:53
• @ovs Thanks for the fix+golf – HyperNeutrino Jul 15 '17 at 15:26

Pyth, 14 bytes

h/s.e*bkJjQTsJ


Actually, 10 bytes

♂≡;Σ@;r*\u


Try it online!

Cheddar, 63 bytes

n->(a->a.map((x,y)->x*y).sum/a.sum+1)(("%d"%n).bytes=>((-)&48))


Try it online!

Retina, 43 bytes

$;$
(?=(.*);)
$1 \d$*
^(1+)(\1)*;\1#2


Try it online!

Java (OpenJDK 8), 80 bytes

int f(long n){int a=0,b=0,c=0;for(;n>0;a+=n%10*++c,n/=10)b+=n%10;return-~c-a/b;}


Try it online!

• You could make this shorter with a lambda expression, n->{...} – Pavel Jul 15 '17 at 5:24

05AB1E, 10 bytes

TвÐgL*OsO÷


Try it online!

GolfScript, 33 bytes

{48-}%.[.,,]zip{{*}*}%{+}*\{+}*/)


Try it online!

Python 2, 132 134 126 112 bytes

• Thanks @ovs for 14 bytes.
n,m=map(int,input()),0
z=lambda a,b:sum(n[j]*abs(m-j)for j in range(a,b))
while z(0,m)<z(m,len(n)):m+=1
print-~m


Try it online!

• But it prints 4 instead of 5 – Leaky Nun Jul 15 '17 at 3:30
• Yes, it is 0-indexed. Should I change it? 2 bytes trade though.. – officialaimm Jul 15 '17 at 3:36
• Some bytes off – ovs Jul 15 '17 at 12:15

Retina, 30 bytes


$'¶ 1>¶ .$*
(1+)¶(\1)+
\$#2


Try it online! Link includes test cases. Explanation: The first stage sneakily creates newline-delimited duplicates of all the suffixes of the original string. This means that the each digit appears after the first newline the number of times according to how far away it is from the left, effectively multiplying to give its moment. The second stage deletes all the newlines after the first, completing the calculation of the moments. The third stage converts the digits to unary, summing them, and the fourth stage divides the sums (exact division is assumed to apply here).

J, 29 bytes

(0 i.~]+/ .*[:-/~#\)@(,.&.":)


J executes verbs right-to-left, so let's start at the end and work our way towards the beginning.

Explanation

         ,.&.":      NB. Explode integer into digits
#\          NB. 0 .. N-1, where N=number of digits
-/~         NB. Distance table, one row per digit
] +/ .* …   NB. Forces at each pivot (F=digit * distance)
0 i.~ …     NB. Digit index where F=0


Note that J is an index-origin 0 language, so it would be more natural and idiomatic for the answer to 8900311672 to be 4 rather than 5, for 231592 to be 3 rather than 4, etc, and so that's the way this function is written.

But if the index-origin 1 is required for output, then add 1+ before 0 i.~ and add 2 bytes to the score, bringing it up from 29 to 31.

• This does not work. Replace #.~&10^:_1 with (,.&.":) to fix. – rdm Jul 16 '17 at 3:04
• @rdm Thanks, I originally had (10&#.^:_1) but I wanted to remove the parens, and quick independent testing showed #.~&10^:_1, but of course as you observed the composition doesn't. I'll fix. Thanks for the shortening. – Dan Bron Jul 16 '17 at 3:07

Python 3, 127 bytes

I am a newbie golfer. All suggestions and tips are greatly apreciated :).

lambda x:min([(abs(eval("+".join(list(x[:i+1])))-eval("+".join(list(x[i:])))),i+1)for i in range(len(x))],key=lambda x:x[0])[1]


Try it Online!

C# (.NET Core), 84 bytes

a=>{int p=0,q=0,i=1;for(;i<=a.Length;p+=a[i-1],q+=i*a[i++-1]);return q/(q%p>0?0:p);}


Try it online!

Takes in an array of single digits as the 'integer' input. Uses the image moments approach for finding the centre of mass. Throws a divide by zero if the seesaw is unbalanced.

DeGolfed

a=>{
int p=0, // used to store the image moment of order 0
q=0, // used to store the image moment of order 1
i=1; // array index, offset to 1 as 1 based indexing is required

for (; i <= a.Length; // go through the array
p += a[i-1],      // getting the order 0 image moment value
q += i*a[i++-1]); //     and the order 1 image moment value

// q / p is the centre of mass, but as an exact integer pivot is required,
// if q is not evenly divisible by p,
// then throw a divide by zero error.
// otherwise return q / p
return q / ( q % p > 0? 0 : p);
}