# puzzlegen.core.utils

Utility functions for puzzle generation and manipulation.

View Source

     1"""
     2Utility functions for puzzle generation and manipulation.
     3"""
     4
     5import random
     6from typing import List, Tuple, Dict, Any
     7
     8def assign_blocks_per_color(nb_blocks: int, colors: List[str]) -> List[Tuple[str, int]]:
     9    """
    10    Randomly assign a color to each block, ensuring at least 3 blocks per color.
    11
    12    Args:
    13        nb_blocks (int): Total number of blocks to assign.
    14        colors (list): List of color names.
    15
    16    Returns:
    17        list: List of tuples (color, number_of_blocks).
    18    """
    19    blocks_per_color = []
    20    nb_blocks_to_assign = nb_blocks
    21
    22    for color in colors:
    23        blocks_per_color.append([color, 3])
    24        nb_blocks_to_assign -= 3
    25
    26    while nb_blocks_to_assign > 0:
    27        random_color = random.choice(colors)
    28        random_blocks = random.randint(1, nb_blocks_to_assign)
    29        for block in blocks_per_color:
    30            if block[0] == random_color:
    31                block[1] += random_blocks
    32                break
    33        nb_blocks_to_assign -= random_blocks
    34
    35    return [(color, count) for color, count in blocks_per_color]
    36
    37def sort_blocks_by_rows(set_blocks: Dict[Any, Any]) -> Dict[Any, Any]:
    38    """
    39    Sort blocks by their row index.
    40
    41    Args:
    42        set_blocks (dict): Dictionary with positions as keys.
    43
    44    Returns:
    45        dict: Sorted dictionary by row.
    46    """
    47    return dict(sorted(set_blocks.items(), key=lambda x: x[0]))
    48
    49def sort_blocks_by_columns(set_blocks: Dict[Any, Any]) -> Dict[Any, Any]:
    50    """
    51    Sort blocks by their column index.
    52
    53    Args:
    54        set_blocks (dict): Dictionary with positions as keys.
    55
    56    Returns:
    57        dict: Sorted dictionary by column.
    58    """
    59    return dict(sorted(set_blocks.items(), key=lambda x: x[0][1]))
    60
    61def print_framed(text_lines):
    62    max_len = max(len(line) for line in text_lines)
    63    border = "╔" + "═" * (max_len + 2) + "╗"
    64    print(border)
    65    for line in text_lines:
    66        print("║ " + line.ljust(max_len) + " ║")
    67    print("╚" + "═" * (max_len + 2) + "╝")

def assign_blocks_per_color(nb_blocks: int, colors: List[str]) -> List[Tuple[str, int]]: View Source

     9def assign_blocks_per_color(nb_blocks: int, colors: List[str]) -> List[Tuple[str, int]]:
    10    """
    11    Randomly assign a color to each block, ensuring at least 3 blocks per color.
    12
    13    Args:
    14        nb_blocks (int): Total number of blocks to assign.
    15        colors (list): List of color names.
    16
    17    Returns:
    18        list: List of tuples (color, number_of_blocks).
    19    """
    20    blocks_per_color = []
    21    nb_blocks_to_assign = nb_blocks
    22
    23    for color in colors:
    24        blocks_per_color.append([color, 3])
    25        nb_blocks_to_assign -= 3
    26
    27    while nb_blocks_to_assign > 0:
    28        random_color = random.choice(colors)
    29        random_blocks = random.randint(1, nb_blocks_to_assign)
    30        for block in blocks_per_color:
    31            if block[0] == random_color:
    32                block[1] += random_blocks
    33                break
    34        nb_blocks_to_assign -= random_blocks
    35
    36    return [(color, count) for color, count in blocks_per_color]

Randomly assign a color to each block, ensuring at least 3 blocks per color.

Args: nb_blocks (int): Total number of blocks to assign. colors (list): List of color names.

Returns: list: List of tuples (color, number_of_blocks).

def sort_blocks_by_rows(set_blocks: Dict[Any, Any]) -> Dict[Any, Any]: View Source

    38def sort_blocks_by_rows(set_blocks: Dict[Any, Any]) -> Dict[Any, Any]:
    39    """
    40    Sort blocks by their row index.
    41
    42    Args:
    43        set_blocks (dict): Dictionary with positions as keys.
    44
    45    Returns:
    46        dict: Sorted dictionary by row.
    47    """
    48    return dict(sorted(set_blocks.items(), key=lambda x: x[0]))

Sort blocks by their row index.

Args: set_blocks (dict): Dictionary with positions as keys.

Returns: dict: Sorted dictionary by row.

def sort_blocks_by_columns(set_blocks: Dict[Any, Any]) -> Dict[Any, Any]: View Source

    50def sort_blocks_by_columns(set_blocks: Dict[Any, Any]) -> Dict[Any, Any]:
    51    """
    52    Sort blocks by their column index.
    53
    54    Args:
    55        set_blocks (dict): Dictionary with positions as keys.
    56
    57    Returns:
    58        dict: Sorted dictionary by column.
    59    """
    60    return dict(sorted(set_blocks.items(), key=lambda x: x[0][1]))

Sort blocks by their column index.

Args: set_blocks (dict): Dictionary with positions as keys.

Returns: dict: Sorted dictionary by column.

def print_framed(text_lines): View Source

    62def print_framed(text_lines):
    63    max_len = max(len(line) for line in text_lines)
    64    border = "╔" + "═" * (max_len + 2) + "╗"
    65    print(border)
    66    for line in text_lines:
    67        print("║ " + line.ljust(max_len) + " ║")
    68    print("╚" + "═" * (max_len + 2) + "╝")