Exemple #1
2
def fetch_puzzle(size, difficulty, id, solution=False):
    """Fetches a new puzzle of given size, difficulty (1 - 6), puzzle id (1 to ???)
    from http://www.futoshiki.org/.

    If solution is True, displays the solution to the puzzle.
    """

    input_text = urllib2.urlopen(
        'http://www.futoshiki.org/get.cgi?size=%d&difficulty=%d&id=%d' % (size, difficulty, id)).read()
    input_text = input_text[28:-8]
    input_text = input_text[len(input_text) / 2:] if solution else input_text[:len(input_text) / 2]
    return Futoshiki.convert(input_text)
Exemple #2
0
def fetch_puzzle(size, difficulty, id, solution=False):
    """Fetches a new puzzle of given size, difficulty (1 - 6), puzzle id (1 to ???)
    from http://www.futoshiki.org/.

    If solution is True, displays the solution to the puzzle.
    """

    input_text = urlopen("http://www.futoshiki.org/get.cgi?size=%d&difficulty=%d&id=%d" % (size, difficulty, id)).read()
    input_text = parseString(input_text).getElementsByTagName("game")[0].firstChild.nodeValue
    input_text = input_text[int(len(input_text) / 2) :] if solution else input_text[: int(len(input_text) / 2)]
    return Futoshiki.convert(input_text)
def fetch_puzzle(size, difficulty, id, solution=False):
    """Fetches a new puzzle of given size, difficulty (1 - 6), puzzle id (1 to ???)
    from http://www.futoshiki.org/.

    If solution is True, displays the solution to the puzzle.
    """

    input_text = urlopen(
        'http://www.futoshiki.org/get.cgi?size=%d&difficulty=%d&id=%d' % (size, difficulty, id)).read()
    input_text = parseString(input_text).getElementsByTagName('game')[0].firstChild.nodeValue
    input_text = input_text[int(len(input_text) / 2):] if solution else input_text[:int(len(input_text) / 2)]
    return Futoshiki.convert(input_text)
def solve_puzzle(puzzle, solve_method):
    """Solves the puzzle using the given solution method and returns the approximate time taken in seconds."""
    start_time = time()
    result = puzzle.solve_with(solve_method)
    if result:
        print(puzzle)
    else:
        print("Failed to find the solution.")
    return time() - start_time


if __name__ == '__main__':

    python_file = '../solutions/p6_solver.py' if len(
        sys.argv) < 2 else sys.argv[1]
    method_name = 'backtracking_search' if len(sys.argv) < 3 else sys.argv[2]

    sys.path.append(os.path.abspath(os.path.dirname(python_file)))
    module = __import__(os.path.splitext(os.path.basename(python_file))[0])
    solve_method = getattr(module, method_name)

    puzzle = Futoshiki(sys.stdin.read().strip())

    start_time = time()
    result = puzzle.solve_with(solve_method)
    if result:
        print(puzzle)
    else:
        print("Failed to find the solution.")
    print("Took %s seconds." % (time() - start_time))
Exemple #5
0

def solve_puzzle(puzzle, solve_method):
    """Solves the puzzle using the given solution method and returns the approximate time taken in seconds."""
    start_time = time()
    result = puzzle.solve_with(solve_method)
    if result:
        print(puzzle)
    else:
        print("Failed to find the solution.")
    return time() - start_time


if __name__ == '__main__':

    python_file = '../solutions/p6_solver.py' if len(sys.argv) < 2 else sys.argv[1]
    method_name = 'backtracking_search' if len(sys.argv) < 3 else sys.argv[2]

    sys.path.append(os.path.abspath(os.path.dirname(python_file)))
    module = __import__(os.path.splitext(os.path.basename(python_file))[0])
    solve_method = getattr(module, method_name)

    puzzle = Futoshiki(sys.stdin.read().strip())

    start_time = time()
    result = puzzle.solve_with(solve_method)
    if result:
        print(puzzle)
    else:
        print("Failed to find the solution.")
    print("Took %s seconds." % (time() - start_time))