False >>> w2 = WordLadderPuzzle("yo", "yo", {"yi", "yo"}) >>> w2.is_solved() True """ return self._from_word == self._to_word if __name__ == "__main__": import doctest doctest.testmod() from puzzle_tools import breadth_first_solve, depth_first_solve from time import time with open("words", "r") as words: word_set = set(words.read().split()) w = WordLadderPuzzle("same", "cost", word_set) start = time() sol = breadth_first_solve(w) end = time() print("Solving word ladder from same->cost") print("...using breadth-first-search") print("Solutions: {} took {} seconds.".format(sol, end - start)) start = time() sol = depth_first_solve(w) end = time() print("Solving word ladder from same->cost") print("...using depth-first-search") print("Solutions: {} took {} seconds.".format(sol, end - start))
""" Return whether MNPuzzle self is solved. @type self: MNPuzzle @rtype: bool """ return self.from_grid == self.to_grid if __name__ == "__main__": import doctest doctest.testmod() target_grid = ( ("1", "2", "3", '4'), ("5", "6", "7", "*"), ) start_grid = ( ("4", "1", "6", "7"), ("3", "*", "2", "5"), ) from puzzle_tools import breadth_first_solve, depth_first_solve from time import time start = time() solution = breadth_first_solve(MNPuzzle(start_grid, target_grid)) end = time() print("BFS solved: \n\n{} \n\nin {} seconds".format(solution, end - start)) start = time() solution = depth_first_solve((MNPuzzle(start_grid, target_grid))) end = time() print("DFS solved: \n\n{} \n\nin {} seconds".format(solution, end - start))
"9", "*", "3", "1", "*", "*", "*", "*", "*", "3", "5", "*", "8", "*", "*", "6", "*", "1", "*", "*", "*", "*", "*", "*", "*", "*", "*", "1", "*", "6", "*", "*", "9", "*", "4", "8", "*", "*", "*", "*", "*", "1", "2", "*", "7", "8", "*", "*", "*", "7", "*", "4", "*", "*", "*", "6", "*", "3", "*", "2", "*", "*", "*"], {"1", "2", "3", "4", "5", "6", "7", "8", "9"}) from time import time print("solving sudoku from July 9 2015 Star... \n\n{}\n\n".format(s)) from puzzle_tools import depth_first_solve start = time() sol = depth_first_solve(s) print(sol) while sol.children: sol = sol.children[0] end = time() print("time to solve 9x9 using depth_first: " "{} seconds\n".format(end - start)) print(sol) s = SudokuPuzzle(9, ["*", "*", "*", "9", "*", "2", "*", "*", "*", "*", "9", "1", "*", "*", "*", "6", "3", "*", "*", "3", "*", "*", "7", "*", "*", "8", "*", "3", "*", "*", "*", "*", "*", "*", "*", "8", "*", "*", "9", "*", "*", "*", "2", "*", "*", "5", "*", "*", "*", "*", "*", "*", "*", "7",
import doctest doctest.testmod() from puzzle_tools import depth_first_solve grid = [["*", "*", "*", "*", "*"], ["*", "*", "*", "*", "*"], ["*", "*", "*", "*", "*"], ["*", "*", ".", "*", "*"], ["*", "*", "*", "*", "*"]] gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"}) import time start = time.time() solution = depth_first_solve(gpsp) end = time.time() print("Solved 5x5 peg solitaire in {} seconds.".format(end - start)) print("Using depth-first: \n{}".format(solution)) ''' grid = [["*", "*", "*", "*", "*"], ["*", "*", "*", "*", "*"], ["*", "*", "*", "*", "*"], ["*", "*", ".", "*", "*"], ["*", "*", "*", "*", "*"]] gridpeg = GridPegSolitairePuzzle(grid,{"*",".","#"}) ext = gridpeg.extensions() #print(ext[1]) for i in range(len(ext)): print(ext[i])
>>> s.is_solved() True """ marker = self._marker amount_of_pegs = 0 for row in marker: for i in row: if i == "*": amount_of_pegs += 1 return amount_of_pegs == 1 if __name__ == "__main__": import doctest doctest.testmod() from puzzle_tools import depth_first_solve grid = [["*", "*", "*", "*", "*"], ["*", "*", "*", "*", "*"], ["*", "*", "*", "*", "*"], ["*", "*", ".", "*", "*"], ["*", "*", "*", "*", "*"]] gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"}) import time start = time.time() solution = depth_first_solve(gpsp) end = time.time() print("Solved 5x5 peg solitaire in {} seconds.".format(end - start)) print("Using depth-first: \n{}".format(solution))
>>> b = WordLadderPuzzle("same", "same", word_set) >>> b.is_solved() True """ # Check that from and to word are the same and that both are legal words return self._from_word == self._to_word and \ (self._from_word and self._from_word) in self._word_set if __name__ == '__main__': import doctest doctest.testmod() from puzzle_tools import breadth_first_solve, depth_first_solve from time import time with open("words", "r") as words: word_set = set(words.read().split()) w = WordLadderPuzzle("same", "cost", word_set) start = time() sol = breadth_first_solve(w) end = time() print("Solving word ladder from same->cost") print("...using breadth-first-search") print("Solutions: {} took {} seconds.".format(sol, end - start)) start = time() sol = depth_first_solve(w) end = time() print("Solving word ladder from same->cost") print("...using depth-first-search") print("Solutions: {} took {} seconds.".format(sol, end - start))
"5", "*", "8", "*", "*", "6", "*", "1", "*", "*", "*", "*", "*", "*", "*", "*", "*", "1", "*", "6", "*", "*", "9", "*", "4", "8", "*", "*", "*", "*", "*", "1", "2", "*", "7", "8", "*", "*", "*", "7", "*", "4", "*", "*", "*", "6", "*", "3", "*", "2", "*", "*", "*" ], {"1", "2", "3", "4", "5", "6", "7", "8", "9"}) #puzzle_mat = ["*"]*(9*9) #s = SudokuPuzzle(9,puzzle_mat,{"1", "2", "3", "4", "5", "6", "7", "8", "9"}) print(s.fail_fast()) from time import time print("solving sudoku from July 9 2015 Star... \n\n{}\n\n".format(s)) from puzzle_tools import depth_first_solve from puzzle_tools import breadth_first_solve #just checking start = time() sol = depth_first_solve(s) print(sol) while sol.children: sol = sol.children[0] end = time() print("time to solve 9x9 using depth_first: " "{} seconds\n".format(end - start)) print(sol) s = SudokuPuzzle(9, [ "*", "*", "*", "9", "*", "2", "*", "*", "*", "*", "9", "1", "*", "*", "*", "6", "3", "*", "*", "3", "*", "*", "7", "*", "*", "8", "*", "3", "*", "*", "*", "*", "*", "*", "*", "8", "*", "*", "9", "*", "*", "*", "2", "*", "*", "5", "*", "*", "*", "*", "*", "*", "*", "7", "*", "7", "*", "*", "8", "*", "*", "4", "*", "*", "4", "5", "*", "*", "*", "8", "1", "*", "*", "*", "*", "3", "*", "6", "*", "*", "*"
# a configuration is solved when from_grid is the same as to_grid # override extensions # legal extensions are configurations that can be reached by swapping one # symbol to the left, right, above, or below "*" with "*" # TODO if __name__ == "__main__": import doctest doctest.testmod() target_grid = (("1", "2", "3"), ("4", "5", "*")) start_grid = (("*", "2", "3"), ("1", "4", "5")) from puzzle_tools import breadth_first_solve, depth_first_solve from time import time start = time() solution = breadth_first_solve(MNPuzzle(start_grid, target_grid)) test = MNPuzzle(start_grid, target_grid) end = time() print("BFS solved: \n\n{} \n\nin {} seconds".format( solution, end - start)) start = time() solution = depth_first_solve((MNPuzzle(start_grid, target_grid))) end = time() print("DFS solved: \n\n{} \n\nin {} seconds".format( solution, end - start))