Created
December 14, 2011 08:17
-
-
Save Mic92/1475704 to your computer and use it in GitHub Desktop.
Berechnet alle Möglichkeiten für 7 a)
This file contains 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
#!/usr/bin/ruby | |
class Node | |
attr_accessor :right, :middle, :left | |
attr_accessor :first_val, :second_val | |
def assign(first_val, second_val) | |
@first_val = first_val | |
@second_val= second_val | |
end | |
def neighbors(right, middle, left) | |
@right = right | |
@middle = middle | |
@left = left | |
end | |
def right_neighbors? | |
self != @right and | |
self != @middle and | |
self != @left | |
end | |
def ==(other_node) | |
(@first_val == other_node.first_val) or | |
(@first_val == other_node.second_val) or | |
(@second_val == other_node.first_val) or | |
(@second_val == other_node.second_val) | |
end | |
def to_s() | |
"[#{@first_val.to_s},#{@second_val.to_s}]" | |
end | |
end | |
# our map | |
# a0 | |
# | |
# a1 a2 a3 | |
# a4 a5 | |
# | |
# a6 a7 | |
# a8 a9 | |
# | |
# dependencies | |
# a0: a1, a2, a3 | |
# a1: a0, a4, a8 | |
# a2: a0, a6, a7 | |
# a3: a0, a5, a9 | |
# a4: a1, a5, a7 | |
# a5: a3, a4, a6 | |
# a6: a2, a5, a8 | |
# a7: a2, a4, a9 | |
# a8: a1, a6, a9 | |
# a9: a3, a7, a8 | |
tuples = [ [1, 2], [1, 3], [1, 4], [1, 5], | |
[2, 3], [2, 4], [2, 5], | |
[3, 4], [3, 5], | |
[4, 5]] | |
neighbor_graph = [ | |
[1, 2, 3], | |
[0, 4, 8], | |
[0, 6, 7], | |
[0, 5, 9], | |
[1, 5, 7], | |
[3, 4, 6], | |
[2, 5, 8], | |
[2, 4, 9], | |
[1, 6, 9], | |
[3, 7, 8], | |
] | |
nodes = Array.new(tuples.length) { Node.new } | |
nodes.each_index do |i| | |
r, m, l = neighbor_graph[i] | |
nodes[i].neighbors(nodes[r], nodes[m], nodes[l]) | |
end | |
def dump_nodes(n, pos) | |
print(<<-eos | |
##{pos} solution | |
#{n[0]} | |
#{n[1]} #{n[2]} #{n[3]} | |
#{n[4]} #{n[5]} | |
#{n[6]} #{n[7]} | |
#{n[8]} #{n[9]} | |
eos | |
) | |
end | |
solutions = 0 | |
tuples.permutation do |perm| | |
perm.each_index do |i| | |
nodes[i].assign(*perm[i]) | |
end | |
right_node = true | |
nodes.each do |node| | |
if !node.right_neighbors? then | |
right_node = false | |
break | |
end | |
end | |
if right_node then | |
solutions = solutions + 1 | |
dump_nodes(nodes, solutions) | |
end | |
end | |
print(solutions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment