7
\$\begingroup\$

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.

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

24 Answers 24

5
\$\begingroup\$

Haskell, 64 62 59 57 45 35 31 bytes

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

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ (:[]) is pure. \$\endgroup\$
    – nimi
    Jul 15, 2017 at 10:04
  • \$\begingroup\$ 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. \$\endgroup\$
    – nimi
    Jul 15, 2017 at 10:19
  • \$\begingroup\$ @nimi doing (<1) is pretty clever. Thanks for the tips. \$\endgroup\$
    – Wheat Wizard
    Jul 15, 2017 at 13:34
  • \$\begingroup\$ @flawr It feels like I should but $ has lower precedence than < preventing me. \$\endgroup\$
    – Wheat Wizard
    Jul 15, 2017 at 15:37
  • \$\begingroup\$ 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) \$\endgroup\$
    – flawr
    Jul 15, 2017 at 15:39
4
\$\begingroup\$

R, 66 bytes

sum(1:length(n<-strtoi(strsplit(paste(scan()),"")[[1]]))*n)/sum(n)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can take input as string by doing scan(,'') instead of using paste. \$\endgroup\$
    – JAD
    Jul 15, 2017 at 13:46
3
\$\begingroup\$

Jelly, 7 bytes

Dµæ.J:S

Try it online! or verify all inputs at once.

\$\endgroup\$
3
  • \$\begingroup\$ You’ve done this before for some reason? 😂 \$\endgroup\$
    – Tim
    Jul 15, 2017 at 2:11
  • \$\begingroup\$ @Tim No, it's just a formula used in physics - sum of moments / sum of digits. \$\endgroup\$
    – Leaky Nun
    Jul 15, 2017 at 2:11
  • 3
    \$\begingroup\$ @Tim He's known for posting answers extremely quickly to questions; sometimes, he posts an answer in the same minute as the question :P \$\endgroup\$
    – hyper-neutrino
    Jul 15, 2017 at 2:29
3
\$\begingroup\$

J, 29 bytes

[:>:@(+/%~]+/@:*i.@$)10#.inv]

Try it online!

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

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
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Jul 15, 2017 at 14:21
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Jul 16, 2017 at 3:23
2
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ +1, Exactly half the length of my answer.... :D \$\endgroup\$
    – 0xffcourse
    Jul 15, 2017 at 3:26
2
\$\begingroup\$

Brachylog, 14 bytes

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

Try it online!

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

GolfScript (24 bytes)

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

Online demo

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

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!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Since a,b,c are declared in an enclosing scope, they can be initialised in the initialiser of the for loop, saving a semicolon. \$\endgroup\$ Jul 15, 2017 at 12:55
  • \$\begingroup\$ 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} \$\endgroup\$
    – Oki
    Jul 15, 2017 at 22:50
1
\$\begingroup\$

MATL, 12 bytes

48-ttn:*sws/

Try it online!

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

Mathematica, 42 bytes

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

Try it online! (Mathics)

\$\endgroup\$
1
  • \$\begingroup\$ Tr saves 4 byte, but breaks Mathics compatibility. \$\endgroup\$
    – DELETE_ME
    Jul 15, 2017 at 7:34
1
\$\begingroup\$

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

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

Pyth, 14 bytes

h/s.e*bkJjQTsJ

Test suite.

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

Actually, 10 bytes

♂≡;Σ@;r*\u

Try it online!

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

Cheddar, 63 bytes

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

Try it online!

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

Retina, 43 bytes

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

Try it online!

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

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!

\$\endgroup\$
1
  • \$\begingroup\$ You could make this shorter with a lambda expression, n->{...} \$\endgroup\$
    – Pavel
    Jul 15, 2017 at 5:24
0
\$\begingroup\$

05AB1E, 10 bytes

TвÐgL*OsO÷

Try it online!

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

GolfScript, 33 bytes

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

Try it online!

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

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!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ But it prints 4 instead of 5 \$\endgroup\$
    – Leaky Nun
    Jul 15, 2017 at 3:30
  • \$\begingroup\$ Yes, it is 0-indexed. Should I change it? 2 bytes trade though.. \$\endgroup\$
    – 0xffcourse
    Jul 15, 2017 at 3:36
  • 1
    \$\begingroup\$ Some bytes off \$\endgroup\$
    – ovs
    Jul 15, 2017 at 12:15
0
\$\begingroup\$

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

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ This does not work. Replace #.~&10^:_1 with (,.&.":) to fix. \$\endgroup\$
    – rdm
    Jul 16, 2017 at 3:04
  • \$\begingroup\$ @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. \$\endgroup\$
    – Dan Bron
    Jul 16, 2017 at 3:07
0
\$\begingroup\$

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!

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

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);
}
\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.