aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xsolver.py200
1 files changed, 14 insertions, 186 deletions
diff --git a/solver.py b/solver.py
index cb445dd..35ba5d3 100755
--- a/solver.py
+++ b/solver.py
@@ -41,8 +41,6 @@ class Kana:
kana_void = Kana("void")
-
-
def is_swappable(kana1, kana2):
table_ok = {
"norm": ("norm", "empt", "froz"),
@@ -67,7 +65,6 @@ def test_is_swappable():
assert not is_swappable(Kana("empt"), Kana("empt"))
-
class KanaGrid:
valid_types_for_chain = ("froz", "norm", "rock")
@@ -114,22 +111,6 @@ class KanaGrid:
new_grid.swap_kana(pos, pos_dest)
return new_grid
- def __repr__(self):
- self.update_score()
- return (
- ('KanaGrid (cnt: %d, score:%d): \n ' % (self.action_count, self.score))
- + '\n '.join(repr_grid(self.grid, (self.width, self.height)).splitlines())
- )
-
-
- def __eq__(self, other):
- return (
- self.width == other.width
- and self.height == other.height
- and self.grid == other.grid
- and self.action_count == other.action_count
- )
-
def generate_valid_pos_for_chain(self):
for y in range(self.height):
for x in range(self.width):
@@ -226,7 +207,6 @@ class KanaGrid:
pos_dst = (pos_dst[0] + vect[0], pos_dst[1] + vect[1])
kana_dst = self.get_kana(pos_dst)
-
def load(input_dict):
grid = []
for serialized_kana in input_dict['grid']:
@@ -239,6 +219,20 @@ class KanaGrid:
def dump(self, stream):
raise NotImplemented
+ def __repr__(self):
+ self.update_score()
+ return (
+ ('KanaGrid (cnt: %d, score:%d): \n ' % (self.action_count, self.score))
+ + '\n '.join(repr_grid(self.grid, (self.width, self.height)).splitlines())
+ )
+
+ def __eq__(self, other):
+ return (
+ self.width == other.width
+ and self.height == other.height
+ and self.grid == other.grid
+ and self.action_count == other.action_count
+ )
def test_kana_grid():
@@ -272,11 +266,6 @@ def test_kana_grid():
assert kanagrid_new.grid == expected_grid
-
-
-
-
-
def repr_grid(grid, grid_size):
lines = []
kana_iter = iter(grid)
@@ -306,7 +295,6 @@ def is_kana_compatible(kana1, kana2):
return False
-
def generate_possible_grids(kanagrid):
for y in range(kanagrid.height):
for x in range(kanagrid.width):
@@ -318,67 +306,6 @@ def generate_possible_grids(kanagrid):
yield (x, y), action_type, new_grid # debug test
-
-class Node:
-
- def __init__(self, grid, parent=None, pos=None, action_type=None):
-
- self.grid = grid
- self.parent = parent
- self.action_type = action_type
- self.pos = pos
- self.children = []
-
- def append(self, node):
- self.children.append(node)
-
- def __repr__(self):
-
- return '(%s, %s, %s, %s, [%s])' % (
- id(self.parent),
- self.grid.action_count,
- self.pos,
- self.action_type,
- len(self.children))
-
- #if not self.children:
- # return repr(self.grid)
-
- #return ('Node(%d): ' % self.children[0].grid.action_count) + repr(self.children)
-
-
-def make_actions(node, taboos={}, max_action=100, debug=False):
- for pos, action_type, new_grid in generate_possible_grids(node.grid):
-
- #id_grid = id(new_grid.grid)
- #if id_grid in taboos:
- # best_count = taboos[id_grid]
- # if new_grid.action_count >= best_count:
- # continue
-
- #taboos[id_grid] = new_grid.action_count
-
- #node_test = node
- #while node_test:
- # if new_grid.grid == node_test.grid.grid:
- # continue
- # node_test = node_test.parent
-
- new_node = Node(new_grid, parent=node, pos=pos, action_type=action_type)
- node.append(new_node)
-
- if debug:
- #print()
- #print("pos %s, action: %s" % (pos, action_type))
- print(new_grid)
-
- #if score(new_grid.grid) % 2:
- #print(node, new_grid)
-
- if new_grid.action_count < max_action:
- make_actions(new_node, taboos=taboos, max_action=max_action, debug=debug)
-
-
def generate_all_possible_grids(grid, grids, max_actions):
for pos, action_type, new_grid in generate_possible_grids(grid):
grid_hash = new_grid.get_hash()
@@ -389,14 +316,6 @@ def generate_all_possible_grids(grid, grids, max_actions):
generate_all_possible_grids(new_grid, grids, max_actions)
-def node_repr_with_parents(node):
- items = []
- while node:
- items.append("%s %s" % (str(node), str(node.grid)))
- node = node.parent
- return '\n'.join(reversed(items))
-
-
def repr_grid_with_parents(grid):
items = []
while grid:
@@ -415,95 +334,8 @@ def print_score_over(node, target_score):
print_score_over(child, target_score)
-def fact(n):
- if n == 1:
- return 1
- return n*fact(n-1)
-
-
-def get_leafs(node):
- if not node.children:
- return [node]
- children_extended = []
- for child in node.children:
- children_extended.extend(get_leafs(child))
- return children_extended
-
-
def main():
- #tree = KanaTree(root=grid)
-
- #my_grid = [
- # kana_void , Kana("myst", "su"), kana_void , Kana("myst", "ko"), kana_void ,
- # Kana("froz", "se"), Kana("empt" ), Kana("empt" ), Kana("empt" ), Kana("froz", "so"),
- # Kana("froz", "ku"), Kana("empt" ), Kana("empt" ), Kana("empt" ), Kana("froz", "no"),
- # kana_void , kana_void , Kana("rock", "ka"), kana_void , kana_void ,
- #]
- #my_grid_size = 5, 4
- #chain_target = 7
- #kanagrid = KanaGrid(my_grid_size, my_grid)
-
- #print(kanagrid)
- #root = Node(kanagrid)
-
- # guided
- #node = root
- #make_actions(node, max_action=1)
- #node = node.children[1]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[1]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[4]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[3]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[4]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[5]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[5]
- #print(node.grid)
- #make_actions(node, max_action=1)
- #node = node.children[0]
- #print(node.grid)
- #print_score_over(root, 0)
-
- # action by lvl1
- #make_actions(root, max_action=1)
- #children_lvl1 = root.children
- #print("%d lvl1 calculated" % len(root.children))
- #for index, child in enumerate(children_lvl1):
- # print("calculating child %d" % index)
- # #print(node_repr_with_parents(child))
- # make_actions(child, max_action=8)
- # print_score_over(root, 6)
- # # make space in memory
- # child.children = []
-
- # action by lvl2
- #make_actions(root, max_action=2)
- #children_lvl2 = get_leafs(root)
- #print("%d lvl2 calculated" % len(children_lvl2))
- #for index, child in enumerate(children_lvl2):
- # print("calculating child %d" % index)
- # #print(node_repr_with_parents(child))
- # make_actions(child, max_action=8-2)
- # print_score_over(root, 6)
- # child.children = []
-
- #make_actions(root, max_action=8)
- #print_score_over(root, 7)
-
- #for x, y in ((0,1), (4, 2), (2, 3)):
- # print(grid.get_kana((x, y)))
-
args = docopt.docopt(__doc__)
with open(args['YAML_GRID'], encoding='utf8') as stream:
input_dict = yaml.safe_load(stream)
@@ -530,9 +362,5 @@ def main():
print(grid)
-
-
- # action by lvl in files
-
if __name__ == '__main__':
main()