On 4/8/07, Ryan Leavengood <leavengood / gmail.com> wrote:
>
> I started working on a generalized solution using this algorithm, but
> it gets tricky. It isn't too hard to generalize the number sequence,
> but generalizing the operators really makes it messy.

OK, I figured out the generalized solution based on Christian's submission:

seq = ARGV[0]
ops = ARGV[1]
res = ARGV[2].to_i
uops = ops.split('').uniq
print (0..(uops.length.to_s * (seq.length-1)).to_i(uops.length+1)).
map { |x| x.to_s(uops.length+1).rjust((seq.length-1), '0').
tr('012345', uops.join.ljust(6, ' ')) }.
find_all { |x| uops.inject(true){|b, op| b and (x.count(op) ==
ops.count(op))} }.
map { |x|
t = seq[0,1] + x.split('').zip(seq[1..-1].split('')).join.delete(' ')
[eval(t), t]
}.each { |s, x|
puts "*****************" if s == res
puts "#{x}: #{s}"
puts "*****************" if s == res
}.size
puts " possible equations tested"

This supports any arbitrary sequence of numbers, up to 5 different
operators in whatever combination, and whatever result. It gets slower