Skip to content

Instantly share code, notes, and snippets.

@fallengiants
Last active August 29, 2015 14:27
Show Gist options
  • Save fallengiants/e68bdd026f608cd1326c to your computer and use it in GitHub Desktop.
Save fallengiants/e68bdd026f608cd1326c to your computer and use it in GitHub Desktop.
#f '123456789',100
def f(d,t)
a=[]
0.upto(3**(d.size-1)).map do |n|
l,n=[d,n.to_s(3)].map {|t| t.chars.map(&:to_i)}
k,m,z=[l.shift],1,1
l.map {|i| o=n.pop||0;z=(1-(o&1)*2); o&2>0 ? k<<k.pop*10+i*m : k<<i*m=z }
a<<k.join(?+).gsub('+-',?-) if k.reduce(&:+)==t
end
a
end
if __FILE__ == $0
p f('123456789', 100)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment