On Sep 26, 2008, at 9:53 AM, Matthew Moss wrote:

> ## Cookie Monster (#178)

My simple search:

#!/usr/bin/env ruby -wKU

def search(field, current = {:x => 0, :y => 0, :cookies => field[0] 
[0], :path => ""})
   %w[E S].map do |direction|
     new_best = Marshal.load(Marshal.dump(current))
     if direction == "E"
       new_best[:x] += 1
       next new_best if new_best[:x] >= field.first.length
     else
       new_best[:y] += 1
       next new_best if new_best[:y] >= field.length
     end
     next new_best if field[new_best[:y]][new_best[:x]] < 0
     new_best[:cookies] += field[new_best[:y]][new_best[:x]]
     new_best[:path]    << direction
     search(field, new_best)
   end.max { |a, b| a[:cookies] <=> b[:cookies] }
end

puzzle = <<END_PUZZLE
  1  3  0  5 -1  7 -1 -1  0  4  2  1
-1  3  2  1 -1  4 -1  5  3 -1  1  0
  5  4  8 -1  3  2  2 -1  4 -1  0  0
  2  1  0  4  1 -1  8  0  2 -1  2  5
  1  4  0  1 -1  0  3  2  2  4  1  4
  0  1  4  1  1  6  1  4  5  2  1  0
  3  2  5  2  0  7 -1  2  1  0 -1  3
  0 -1  4 -1 -1  3  5  1  4  2  1  2
  5  4  8 -1  3  2  2 -1  4 -1  0  0
  2  1  0  4  1 -1  8  0  2 -1  2  5
  1  3  0  5 -1  7 -1 -1  0  4  2  1
  0  0  3  1  5  2  1  5  4  1  3  3
END_PUZZLE

require "pp"
pp search(puzzle.map { |row| row.split.map { |n| n.to_i } })

__END__

James Edward Gray II