|
| 1 | +import math, os |
| 2 | + |
| 3 | + |
| 4 | +def text_to_grid (text): |
| 5 | + """ |
| 6 | + Converts a text to a set of coordinates |
| 7 | +
|
| 8 | + The text is expected to be separated by newline characters |
| 9 | + Each character will have its coordinates as keys |
| 10 | +
|
| 11 | + :param string text: The text to convert |
| 12 | + :return: The converted grid, its height and width |
| 13 | + """ |
| 14 | + grid = {} |
| 15 | + lines = text.splitlines() |
| 16 | + height = len(lines) |
| 17 | + width = 0 |
| 18 | + for y in range(len(lines)): |
| 19 | + width = max(width, len(lines[y])) |
| 20 | + for x in range(len(lines[y])): |
| 21 | + grid[(x, y)] = lines[y][x] |
| 22 | + |
| 23 | + return grid |
| 24 | + |
| 25 | +def grid_to_text (grid, blank_character = ' '): |
| 26 | + """ |
| 27 | + Converts the grid to a text format |
| 28 | +
|
| 29 | + :param dict grid: The grid to convert, in format (x, y): value |
| 30 | + :param string blank_character: What to use for cells with unknown value |
| 31 | + :return: The grid in text format |
| 32 | + """ |
| 33 | + |
| 34 | + text = '' |
| 35 | + |
| 36 | + grid_x, grid_y = zip(*grid.keys()) |
| 37 | + |
| 38 | + for y in range (min(grid_y), max(grid_y)+1): |
| 39 | + for x in range (min(grid_x), max(grid_x)+1): |
| 40 | + if (x, y) in grid: |
| 41 | + text += grid[(x, y)] |
| 42 | + else: |
| 43 | + text += blank_character |
| 44 | + text += os.linesep |
| 45 | + text = text[:-len(os.linesep)] |
| 46 | + |
| 47 | + return text |
| 48 | + |
| 49 | +def split_in_parts (grid, width, height): |
| 50 | + """ |
| 51 | + Splits a grid in parts of width*height size |
| 52 | +
|
| 53 | + :param dict grid: The grid to convert, in format (x, y): value |
| 54 | + :param integer width: The width of parts to use |
| 55 | + :param integer height: The height of parts to use |
| 56 | + :return: The different parts |
| 57 | + """ |
| 58 | + |
| 59 | + if not isinstance(width, int) or not isinstance(height, int): |
| 60 | + return False |
| 61 | + if width <= 0 or height <= 0: |
| 62 | + return False |
| 63 | + |
| 64 | + grid_x, grid_y = zip(*grid.keys()) |
| 65 | + grid_width = max(grid_x) - min(grid_x) + 1 |
| 66 | + grid_height = max(grid_y) - min(grid_y) + 1 |
| 67 | + |
| 68 | + parts = [] |
| 69 | + |
| 70 | + for part_y in range(math.ceil(grid_height / height)): |
| 71 | + for part_x in range (math.ceil(grid_width / width)): |
| 72 | + parts.append({(x, y):grid[(x, y)] \ |
| 73 | + for x in range(part_x*width, min((part_x + 1)*width, grid_width)) \ |
| 74 | + for y in range(part_y*height, min((part_y + 1)*height, grid_height))}) |
| 75 | + |
| 76 | + return parts |
| 77 | + |
| 78 | +def merge_parts (parts, width, height): |
| 79 | + """ |
| 80 | + Merges different parts in a single grid |
| 81 | +
|
| 82 | + :param dict parts: The parts to merge, in format (x, y): value |
| 83 | + :return: The merged grid |
| 84 | + """ |
| 85 | + |
| 86 | + grid = {} |
| 87 | + |
| 88 | + part_x, part_y = zip(*parts[0].keys()) |
| 89 | + part_width = max(part_x) - min(part_x) + 1 |
| 90 | + part_height = max(part_y) - min(part_y) + 1 |
| 91 | + |
| 92 | + part_nr = 0 |
| 93 | + for part_y in range(height): |
| 94 | + for part_x in range(width): |
| 95 | + grid.update({(x + part_x*part_width, y + part_y*part_height): parts[part_nr][(x, y)] for (x, y) in parts[part_nr]}) |
| 96 | + part_nr += 1 |
| 97 | + |
| 98 | + return grid |
| 99 | + |
| 100 | +def rotate (grid, rotations = (0, 90, 180, 270)): |
| 101 | + """ |
| 102 | + Rotates a grid and returns the result |
| 103 | +
|
| 104 | + :param dict grid: The grid to rotate, in format (x, y): value |
| 105 | + :param tuple rotations: Which angles to use for rotation |
| 106 | + :return: The parts in text format |
| 107 | + """ |
| 108 | + |
| 109 | + rotated_grid = [] |
| 110 | + |
| 111 | + grid_x, grid_y = zip(*grid.keys()) |
| 112 | + width = max(grid_x) - min(grid_x) + 1 |
| 113 | + height = max(grid_y) - min(grid_y) + 1 |
| 114 | + |
| 115 | + for angle in rotations: |
| 116 | + if angle == 0: |
| 117 | + rotated_grid.append(grid) |
| 118 | + elif angle == 90: |
| 119 | + rotated_grid.append({(height-y, x): grid[(x, y)] for (x, y) in grid}) |
| 120 | + elif angle == 180: |
| 121 | + rotated_grid.append({(width-x, height-y): grid[(x, y)] for (x, y) in grid}) |
| 122 | + elif angle == 270: |
| 123 | + rotated_grid.append({(y, width-x): grid[(x, y)] for (x, y) in grid}) |
| 124 | + |
| 125 | + return rotated_grid |
| 126 | + |
| 127 | +def flip (grid, flips = ('V', 'H')): |
| 128 | + """ |
| 129 | + Flips a grid and returns the result |
| 130 | +
|
| 131 | + :param dict grid: The grid to rotate, in format (x, y): value |
| 132 | + :param tuple flips: Which flips (horizontal, vertical) to use for flip |
| 133 | + :return: The parts in text format |
| 134 | + """ |
| 135 | + |
| 136 | + flipped_grid = [] |
| 137 | + |
| 138 | + grid_x, grid_y = zip(*grid.keys()) |
| 139 | + width = max(grid_x) - min(grid_x) + 1 |
| 140 | + height = max(grid_y) - min(grid_y) + 1 |
| 141 | + |
| 142 | + for flip in flips: |
| 143 | + if flip == 'H': |
| 144 | + flipped_grid.append({(x, height-y): grid[(x, y)] for (x, y) in grid}) |
| 145 | + elif flip == 'V': |
| 146 | + flipped_grid.append({(width-x, y): grid[(x, y)] for (x, y) in grid}) |
| 147 | + |
| 148 | + return flipped_grid |
| 149 | + |
0 commit comments