On Sep 12, 2007, at 5:36 PM, Morton Goldberg wrote:

> <code>
> n = 3
> n.times do
>    grid.each_with_remaining do |cell, other|
>       cn, on = neighbors(cell), neighbors(other)
>       before_score = score(cell.cell_type, cn) +
>                      score(other.cell_type, on)
>       after_score = score(other.cell_type, cn) +
>                     score(cell.cell_type, on)
>       if after_score <= before_score
>          cell.cell_type, other.cell_type = other.cell_type,  
> cell.cell_type
>       end
>    end
>    grid.print_field_values
>    puts
> end
> </code>

I realized I was doing something silly in the above code -- I was  
scoring two cells even if they were of the same type. It runs  
noticeably faster if I don't do that.

<code>
n = 3
n.times do
    grid.each_with_remaining do |cell, other|
       next if cell.cell_type == other.cell_type # <-- added this line
       cn, on = neighbors(cell), neighbors(other)
       before_score = score(cell.cell_type, cn) + score 
(other.cell_type, on)
       after_score = score(other.cell_type, cn) + score 
(cell.cell_type, on)
       if after_score <= before_score
          cell.cell_type, other.cell_type = other.cell_type,  
cell.cell_type
       end
    end
    grid.print_field_values
    puts
end
</code>

Regards, Morton