From: Eugene Kalenkovich Date: 2007-07-31T11:29:58+09:00 Subject: Re: [QUIZ] Crossword Solver (#132) "Ruby Quiz" wrote in message > > Write a Ruby crossword solver. Randomly fill a crossword template with > words > from a dictionary. Use any dictionary you want (/usr/share/dict/words, > text of > pickaxe, etc.). > My solution, slightly optimized for performance version of the code that was used originally to generate examples for this quiz: class CrossTempl def initialize(templ) @tmpl=templ.collect { |line| line.split(//) } @words=[] @tmpl.each_index do |i| @tmpl[i].each_with_index do |char, j| if char!='#' if (j==0||@tmpl[i][j-1]=='#') && !@tmpl[i][j+1].nil? && @tmpl[i][j+1]!='#' @words << [i,j,0] if self.template(i,j,0).include?('_') end if (i==0||@tmpl[i-1][j]=='#') && !@tmpl[i+1].nil? && @tmpl[i+1][j]!='#' @words << [i,j,1] if self.template(i,j,1).include?('_') end end end end @words.sort! {|a,b| a[0]+a[1]<=>b[0]+b[1]} end def template(i,j,dir) str='' chr=@tmpl[i][j] while !chr.nil? && chr!='#' str<0 && rand(2)==1 # fighting incorrect word sorting # by un-ballancing search end $ct[i]=pattern return false end if findWord(0) puts $ct else puts "O-ops" end