hlines = [(x/2, y/2) for y, x in grid if grid[y, x] in "_-"] vlines = [(x/2, y/2) for y, x in grid if grid[y, x] == "|"] return Loop(rows, cols, hlines, vlines) class Loop: def __init__(self, rows, cols, hlines, vlines): self.rows = rows self.cols = cols self.hlines = hlines self.vlines = vlines @staticmethod def loads(text): return LoopParser().parse(text.splitlines()) def render(self): grid = Grid(self.cols, self.rows) grid.draw_corners(r=6) grid.draw_grid(stroke_dasharray="4 4", stroke="black", stroke_width=1) for x, y in self.hlines: grid.hline(x, y, 1, stroke="black", stroke_width=4) for x, y in self.vlines: grid.vline(x, y, 1, stroke="black", stroke_width=4) return grid.svg parser.register_puzzle("loop", Loop)
print values return width = 1 + max(len(v) for v in values.values()) def hline(): print '+' + "-" * width * self.size + '-+' print hline() for i in range(self.size): print "|" + "".join(values[i, j].center(width) for j in range(self.size)) + " | " + self.constraints['right'][i] hline() print " " + "".join(str(c).center(width) for c in self.constraints['bottom']) register_puzzle("skyscrapers", SkyScrappers) def main(filename): puzzle = SkyScrappers.load(filename) size = puzzle.size data = puzzle.data solver = Solver(puzzle.size, [data[i][size] for i in range(size)], [data[size][i] for i in range(size)]) solutions = solver.solve(findall=True) print "solutions:" for s in solutions: solver.print_grid(s) def main(*a): g = Generator(6) g.display()
return visited = visited.copy() visited[node] = len(visited) if node == self.end: if len(visited) == len(graph): yield visited return for n in graph[node]: if n not in visited and not self.are_crossing(n, node, visited): for soln in self._solve(graph, visited, n): yield soln parser.register_puzzle("twist", Twist) parser.register_puzzle("twist-solution", TwistSolution) def main(): d = [ "123", "321", "123", ] s = TwistSolver(d) for a in s.solve(): print a print if __name__ == "__main__": #import doctest