From: Ken Bloom Date: 2007-04-09T05:10:04+09:00 Subject: Re: [QUIZ] Getting to 100 (#119) On Sun, 08 Apr 2007 23:25:14 +0900, Kyle Schmitt wrote: > My submission uses extreme (in)elegance ;) in accordance with this weeks > quiz saying "don't think, let the computer think for you" > > I'm submitting several versions, one full version that includes the > extra credit, and several "golf" versions > > Enjoy! > > --Kyle > > First we have this version > #########begin########### 42 lines long elegance = nil > > ######################### > > to100 = Hash.new() > > @setlength=9#set, starting from 1 to use > > @maxops=@setlength-1#the maximum number of operators (including space) > in any equation > > @operator = ["", "+", "-"] > > @oplength = @operator.length > > keylength = @oplength.power!(@maxops) > > > > def bogoGen() > > little=Array.new(@setlength+@maxops) do > > |i| > > if i.modulo(2)==0 then > > i=i/2 > > i+=1 > > else > > i=@operator[rand(@oplength)] > > end > > end > > return(little.join) > > end > > > > writingHamlet = Time.now() > > > > while to100.keys.length > elegance = bogoGen() > > to100.store(elegance,eval(elegance)) > > #puts "Found #{to100.keys.length} formulas" if > to100.keys.length%100==0 > > end > > > > millionMonkeys = Time.now() > > > > to100.sort.each do > > |answer| > > fortytwo=answer[1]==100?'*':' ' > > #display the answer! > > puts "#{fortytwo} #{answer[0]}=#{answer[1]} #{fortytwo}" > > end > > > > willFinish = Time.now() > > #puts "Total calculation time: #{millionMonkeys - writingHamlet} > seconds" > > #puts "Total run time: #{willFinish - writingHamlet} seconds" > #####end ###### > > > > > #compact v1 > t={} > > while t.length<(6561) > > e = Array.new(17){|i| i=i%2==0?i/2+1:["","+","-"][rand(3)]}.join > > t.store(e,eval(e)) > > end > > t.sort.each {|a| f=a[1]==100?'*':' '; puts "#{f} #{a[0]}=#{a[1]} #{f}"} > > #compact v2 > t={} > > while t.length<(6561) > > t.store(Array.new(17){|i| > i=i%2==0?i/2+1:["","+","-"][rand(3)]}.join,'') > > end > > t.sort.each {|a| f=eval(a[0])==100?'*':' '; puts "#{f} > #{a[0]}=#{eval(a[0])} #{f}"} > > #compact v3 > t=Array.new(6561){|i|i} > > t.each_index{|a| while t[a]=Array.new(17){|i| > i=i%2==0?i/2+1:["","+","-"][rand(3)]}.join and not > t.length==t.uniq.length;end} > > t.sort.each {|a| f=eval(a)==100?'*':' '; puts "#{f} #{a}=#{eval(a)} > #{f}"} For those reading along, the magic number 6560=='22222222'.to_i(3) His goal is to just generate 6560 different combinations of the operators using a random generator. One should note that this technically violates the rules of code golf which require the solution to terminate within a short period of time (I believe 4 seconds), so that its correctness can be verified automatically without having to worry about DOS attacks. -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/