Skip to content

Instantly share code, notes, and snippets.

@iogakos
Created January 29, 2012 21:06
Show Gist options
  • Save iogakos/1700648 to your computer and use it in GitHub Desktop.
Save iogakos/1700648 to your computer and use it in GitHub Desktop.
The AC-3 algorithm, the ruby way
def calculate_worklist(bishops)
worklist = Array.new
bishops.each { |b|
worklist.unshift("#{b[0]}") && next if b[1] != [nil, nil]
worklist << b[0]
}
worklist.combination(2).to_a
end
def pick_position(positions)
# Assign position to bishop
positions.delete_at(0)
end
def init_ac3
bishops = Hash.new
bishops["a"] = bishops["b"] = bishops["c"] = [nil,nil]
positions = Array.new
3.times do |i|
3.times do |j|
positions << [i,j]
end
end
bishops["a"] = pick_position(positions)
worklist = calculate_worklist(bishops)
[bishops, worklist]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment