From b3023beacd70ef8e5319fa8d4ebc0e9048c82531 Mon Sep 17 00:00:00 2001 From: Boyan Date: Tue, 24 Dec 2024 23:28:10 +0200 Subject: [PATCH] Change --- iq_mini_4489/src/main.py | 96 -------------------------------------- iq_mini_4489/src/solver.py | 4 +- out.json | 62 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 98 deletions(-) delete mode 100644 iq_mini_4489/src/main.py create mode 100644 out.json diff --git a/iq_mini_4489/src/main.py b/iq_mini_4489/src/main.py deleted file mode 100644 index dff506f..0000000 --- a/iq_mini_4489/src/main.py +++ /dev/null @@ -1,96 +0,0 @@ -from board import Board, generate_orientations -from typing import Tuple, List -import pieces -import numpy as np -import colorama - -def backtrack(board: Board, pieces_list: List[str], piece_index: int, - orientations: dict, solutions: List[np.ndarray]) -> None: - """ - Backtracking to place each piece in the board in all possible ways. - """ - - # if full, check if solution - if piece_index == len(pieces_list): - if board.is_full(): - solutions.append(board.grid.copy()) - return - - piece_name = pieces_list[piece_index] - all_orientations = orientations[piece_name] - - for orientation in all_orientations: - rows, cols = orientation.shape - for row in range(board.rows - rows + 1): - for col in range(board.cols - cols + 1): - if board.placeable(orientation, (row, col)): - board.place(orientation, (row, col), piece_index + 1) - backtrack(board, pieces_list, piece_index + 1, orientations, solutions) - board.remove(orientation, (row, col)) - - -def colorful_solution(solution: np.ndarray) -> str: - """ - Return a colorful representation of the solution. - """ - rows, cols = solution.shape - result = "" - piece_id_to_color = { - -1: colorama.Back.BLACK, - 1: colorama.Back.GREEN, - 2: colorama.Back.BLUE, - 3: colorama.Back.YELLOW, - 4: colorama.Back.MAGENTA, - 5: colorama.Back.CYAN, - 6: colorama.Back.RED, - } - for r in range(rows): - for c in range(cols): - piece_id = solution[r, c] - if piece_id == 0: - result += colorama.Back.LIGHTWHITE_EX + " " - else: - result += piece_id_to_color[piece_id] + " " - - result += colorama.Back.RESET - result += "\n" - return result - -def solve(xPin: Tuple[int, int], yPin: Tuple[int, int], zPin: Tuple[int, int]): - """ - solve the puzzle, avoiding the pins at the specified positions. - - Args: - xPin (Tuple[int, int]): row, col of the 'x' pin - yPin (Tuple[int, int]): row, col of the 'y' pin - zPin (Tuple[int, int]): row, col of the 'z' pin - """ - - board = Board((5, 5)) - - pinned_positions = [xPin, yPin, zPin] - for (pr, pc) in pinned_positions: - board.grid[pr, pc] = -1 - - orientations_map = {} - for piece_name, piece_matrix in pieces.all_pieces.items(): - orientations_map[piece_name] = generate_orientations(piece_matrix) - piece_names = list(pieces.all_pieces.keys()) - solutions = [] - backtrack(board, piece_names, 0, orientations_map, solutions) - if solutions: - print(f"Found {len(solutions)} solution(s).") - for idx, sol in enumerate(solutions, start=1): - print(f"Solution #{idx}:") - print(colorful_solution(sol)) - print("------------------") - print(f"Total: {len(solutions)} solution{"s" if len(solutions) else ""}.") - return - - print("No solution found.") - - - - -if __name__ == "__main__": - solve((0, 2), (2, 0), (3, 3)) \ No newline at end of file diff --git a/iq_mini_4489/src/solver.py b/iq_mini_4489/src/solver.py index 59ade47..0868376 100644 --- a/iq_mini_4489/src/solver.py +++ b/iq_mini_4489/src/solver.py @@ -99,11 +99,11 @@ def try_all_pegs(): all_solutions = [] # Solution entry {x: (row, col), y: (row, col), z: (row, col), solution_count: int} - for xPinA in tqdm(range(5)): + for xPinA in (range(5)): for yPinA in range(5): for xPinB in range(5): for yPinB in range(5): - for xPinC in range(5): + for xPinC in tqdm(range(5)): for yPinC in range(5): if (xPinA, yPinA) == (xPinB, yPinB) or (xPinA, yPinA) == (xPinC, yPinC) or (xPinB, yPinB) == (xPinC, yPinC): continue diff --git a/out.json b/out.json new file mode 100644 index 0000000..6b84a8a --- /dev/null +++ b/out.json @@ -0,0 +1,62 @@ +[ + { "x": [0, 0], "y": [1, 4], "z": [2, 2], "solution_count": 12 }, + { "x": [0, 0], "y": [1, 4], "z": [3, 2], "solution_count": 15 }, + { "x": [0, 0], "y": [1, 4], "z": [4, 2], "solution_count": 12 }, + { "x": [0, 0], "y": [2, 4], "z": [1, 2], "solution_count": 21 }, + { "x": [0, 0], "y": [2, 4], "z": [3, 2], "solution_count": 20 }, + { "x": [0, 0], "y": [2, 4], "z": [4, 2], "solution_count": 28 }, + { "x": [0, 0], "y": [3, 4], "z": [1, 2], "solution_count": 24 }, + { "x": [0, 0], "y": [3, 4], "z": [2, 2], "solution_count": 16 }, + { "x": [0, 0], "y": [3, 4], "z": [4, 2], "solution_count": 26 }, + { "x": [0, 0], "y": [4, 4], "z": [1, 2], "solution_count": 31 }, + { "x": [0, 0], "y": [4, 4], "z": [2, 2], "solution_count": 52 }, + { "x": [0, 0], "y": [4, 4], "z": [3, 2], "solution_count": 31 }, + { "x": [1, 0], "y": [0, 4], "z": [2, 2], "solution_count": 12 }, + { "x": [1, 0], "y": [0, 4], "z": [3, 2], "solution_count": 15 }, + { "x": [1, 0], "y": [0, 4], "z": [4, 2], "solution_count": 12 }, + { "x": [1, 0], "y": [2, 4], "z": [0, 2], "solution_count": 9 }, + { "x": [1, 0], "y": [2, 4], "z": [3, 2], "solution_count": 14 }, + { "x": [1, 0], "y": [2, 4], "z": [4, 2], "solution_count": 5 }, + { "x": [1, 0], "y": [3, 4], "z": [0, 2], "solution_count": 7 }, + { "x": [1, 0], "y": [3, 4], "z": [2, 2], "solution_count": 6 }, + { "x": [1, 0], "y": [3, 4], "z": [4, 2], "solution_count": 7 }, + { "x": [1, 0], "y": [4, 4], "z": [0, 2], "solution_count": 26 }, + { "x": [1, 0], "y": [4, 4], "z": [2, 2], "solution_count": 16 }, + { "x": [1, 0], "y": [4, 4], "z": [3, 2], "solution_count": 24 }, + { "x": [2, 0], "y": [0, 4], "z": [1, 2], "solution_count": 21 }, + { "x": [2, 0], "y": [0, 4], "z": [3, 2], "solution_count": 20 }, + { "x": [2, 0], "y": [0, 4], "z": [4, 2], "solution_count": 28 }, + { "x": [2, 0], "y": [1, 4], "z": [0, 2], "solution_count": 9 }, + { "x": [2, 0], "y": [1, 4], "z": [3, 2], "solution_count": 14 }, + { "x": [2, 0], "y": [1, 4], "z": [4, 2], "solution_count": 5 }, + { "x": [2, 0], "y": [3, 4], "z": [0, 2], "solution_count": 5 }, + { "x": [2, 0], "y": [3, 4], "z": [1, 2], "solution_count": 14 }, + { "x": [2, 0], "y": [3, 4], "z": [4, 2], "solution_count": 9 }, + { "x": [2, 0], "y": [4, 4], "z": [0, 2], "solution_count": 28 }, + { "x": [2, 0], "y": [4, 4], "z": [1, 2], "solution_count": 20 }, + { "x": [2, 0], "y": [4, 4], "z": [3, 2], "solution_count": 21 }, + { "x": [3, 0], "y": [0, 4], "z": [1, 2], "solution_count": 24 }, + { "x": [3, 0], "y": [0, 4], "z": [2, 2], "solution_count": 16 }, + { "x": [3, 0], "y": [0, 4], "z": [4, 2], "solution_count": 26 }, + { "x": [3, 0], "y": [1, 4], "z": [0, 2], "solution_count": 7 }, + { "x": [3, 0], "y": [1, 4], "z": [2, 2], "solution_count": 6 }, + { "x": [3, 0], "y": [1, 4], "z": [4, 2], "solution_count": 7 }, + { "x": [3, 0], "y": [2, 4], "z": [0, 2], "solution_count": 5 }, + { "x": [3, 0], "y": [2, 4], "z": [1, 2], "solution_count": 14 }, + { "x": [3, 0], "y": [2, 4], "z": [4, 2], "solution_count": 9 }, + { "x": [3, 0], "y": [4, 4], "z": [0, 2], "solution_count": 12 }, + { "x": [3, 0], "y": [4, 4], "z": [1, 2], "solution_count": 15 }, + { "x": [3, 0], "y": [4, 4], "z": [2, 2], "solution_count": 12 }, + { "x": [4, 0], "y": [0, 4], "z": [1, 2], "solution_count": 31 }, + { "x": [4, 0], "y": [0, 4], "z": [2, 2], "solution_count": 52 }, + { "x": [4, 0], "y": [0, 4], "z": [3, 2], "solution_count": 31 }, + { "x": [4, 0], "y": [1, 4], "z": [0, 2], "solution_count": 26 }, + { "x": [4, 0], "y": [1, 4], "z": [2, 2], "solution_count": 16 }, + { "x": [4, 0], "y": [1, 4], "z": [3, 2], "solution_count": 24 }, + { "x": [4, 0], "y": [2, 4], "z": [0, 2], "solution_count": 28 }, + { "x": [4, 0], "y": [2, 4], "z": [1, 2], "solution_count": 20 }, + { "x": [4, 0], "y": [2, 4], "z": [3, 2], "solution_count": 21 }, + { "x": [4, 0], "y": [3, 4], "z": [0, 2], "solution_count": 12 }, + { "x": [4, 0], "y": [3, 4], "z": [1, 2], "solution_count": 15 }, + { "x": [4, 0], "y": [3, 4], "z": [2, 2], "solution_count": 12 } +]