lunlumo #6383(2008/05/30 12:11 GMT) [ Ruby ] Rating0/0=0.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#! /usr/bin/ruby class Text GOAL = 'METHINKSITISAWEASEL' CHARACTER_SET = ('A'..'Z').to_a attr_accessor :text def create self.text = (1..GOAL.length).map { |_| CHARACTER_SET[rand(CHARACTER_SET.length)] }.to_s self end def update self.text[rand(GOAL.length)] = CHARACTER_SET[rand(CHARACTER_SET.length)] self end def rank self.text.split('').zip(GOAL.split('')).inject(0) { |r,s| s[0] == s[1] ? r + 1 : r } end def complete? self.rank == GOAL.length end def clone copy = super copy.text = self.text.clone copy end end class Processor POOL_SIZE = 300 COPY_SIZE = 3 attr_accessor :text_list def initialize self.text_list = (1..POOL_SIZE).map { |_| Text.new.create }.sort { |a,b| b.rank <=> a.rank } self end def process self.text_list = self.text_list.map { |t| (1..COPY_SIZE).map { |_| t.clone.update } }.flatten.sort { |a,b| b.rank <=> a.rank }.first(POOL_SIZE) self end def complete? self.text_list.first.complete? end end i = 1 processor = Processor.new until processor.complete? do puts "process(#{i}): #{processor.text_list.first.text}(#{processor.text_list.first.rank})" processor.process i = i + 1 end puts "complete(#{i}): #{processor.text_list.first.text}"
Rating0/0=0.00-0+
[ reply ]
lunlumo #6383() [ Ruby ] Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]