Skip to main content
8 of 8
Shaved 2 bytes
Dendrobium
  • 2.5k
  • 13
  • 26

Javascript ES6, 62 Bytes

s=>Math.sign([...s].reduce((p,c,i)=>p+~~c*(i-s.indexOf`^`),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input p+~~c*(i-s.indexOf`^`),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs (assigning anonymous function to f):

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @Vɪʜᴀɴ)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
  • -9 bytes: removed pivot check (apparently, ~~'^' evaluates to 0...)
  • -2 bytes: made function anonymous (thanks @cᴏɴᴏʀ-obʀɪᴇɴ)
Dendrobium
  • 2.5k
  • 13
  • 26