only store unique programs

This commit is contained in:
Josh Holtrop 2014-02-11 09:34:59 -05:00
parent 7bfface1a0
commit 10abe3bf15
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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