This commit is contained in:
Boyan 2024-12-24 23:28:10 +02:00
parent 19d1cb8e29
commit b3023beacd
3 changed files with 64 additions and 98 deletions

View File

@ -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))

View File

@ -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

62
out.json Normal file
View File

@ -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 }
]