Created
January 29, 2012 21:06
-
-
Save iogakos/1700648 to your computer and use it in GitHub Desktop.
The AC-3 algorithm, the ruby way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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