From: James Edward Gray II Date: 2007-04-09T11:11:34+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1-208995236 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Begin forwarded message: > From: Dennis Frommknecht > Date: April 8, 2007 12:26:42 PM CDT > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Hi, > > here is a solution for quiz number 119. > As I'm not subscribed to the mailing list, i hope you can forward it. > > Thanks, > > Dennis Frommknecht > > --Apple-Mail-1-208995236 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0755; name=puzzlers.rb Content-Disposition: attachment; filename=puzzlers.rb #!/usr/bin/env ruby -W # This solution uses 3 nested loops to divide the # numbers into 4 groups (using regular expressions). # Then the 3 allowed combinations of plus and minus # are inserted between the groups. # Finally the result is calculated using eval NUMBERS = "123456789" CORRECT_RES = 100 OPS = [['+', '-', '-'], ['-', '+', '-'], ['-', '-', '+']] num_of_ops = OPS[0].length equ_counter = 0 1.upto(NUMBERS.length - num_of_ops) do |i| 1.upto(NUMBERS.length - num_of_ops - i + 1) do |j| 1.upto(NUMBERS.length - num_of_ops - i + 1 - j + 1) do |k| if NUMBERS.match(/(\d{#{i}})(\d{#{j}})(\d{#{k}})(\d+)/) then OPS.each do |o| command = "#{$1} #{o[0]} #{$2} #{o[1]} #{$3} #{o[2]} #{$4}" res = eval command equ_counter += 1 puts "*" * 15 if res == CORRECT_RES puts "#{command} = #{res}" puts "*" * 15 if res == CORRECT_RES end end end end end puts "#{equ_counter} possible equations tested" --Apple-Mail-1-208995236 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1-208995236--