diff --git a/genetic.rb b/genetic.rb index c54adfe..6cf60f1 100755 --- a/genetic.rb +++ b/genetic.rb @@ -33,6 +33,7 @@ def main p2.mutate [grade_program(p2), p2] end + programs.uniq! programs.sort! do |a, b| a.first <=> b.first end diff --git a/program.rb b/program.rb index ea1a013..953f58a 100644 --- a/program.rb +++ b/program.rb @@ -1,4 +1,6 @@ class Program + include Comparable + def initialize @instructions = [] (2 + rand(4)).times do @@ -60,6 +62,18 @@ class Program @instructions.size end + def <=>(other) + @instructions <=> other.instance_variable_get(:@instructions) + end + + def eql?(other) + (other.class == self.class) && (other == self) + end + + def hash + @instructions.hash + end + private def random_instruction