-
Notifications
You must be signed in to change notification settings - Fork 0
/
bricks.py
149 lines (106 loc) · 4.13 KB
/
bricks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from enum import Enum
import json
from typing import Callable
vector2 = tuple[int, int]
class Position:
pos: vector2
def __init__(self, x: int, y: int) -> None:
self.pos = x, y
def __eq__(self, __value: "Position") -> bool:
return self.pos[0] == __value.pos[0] and self.pos[1] == __value.pos[1]
def __lt__(self, __value: "Position") -> bool:
if self.pos[0] == __value.pos[0]:
return self.pos[1] < __value.pos[1]
return self.pos[0] < __value.pos[0]
def __add__(self, __value: "Position") -> "Position":
return Position(self.pos[0] + __value.pos[0], self.pos[1] + __value.pos[1])
def __getitem__(self, key) -> int:
return self.pos[key]
def __str__(self):
return str(self.pos)
def __repr__(self) -> str:
return str(self)
def __hash__(self) -> int:
return hash(self.pos)
class Blocks:
blocks: list[Position]
def __init__(self, blocks: list[vector2 | Position]) -> None:
self.blocks = [Position(*pos) for pos in blocks]
def __len__(self) -> int:
return len(self.blocks)
def __iter__(self):
for b in sorted(self.blocks):
yield b
def __eq__(self, __value: "Blocks") -> bool:
return all(pos == _pos for pos, _pos in zip(self, __value))
def __str__(self) -> str:
return str(sorted(self.blocks))
def __repr__(self) -> str:
return str(self)
def __hash__(self) -> int:
return hash(str(self))
def display(self):
X = "██"
# ? Group by x-index
_blocks = sorted(self, key=lambda b: b[0])
block_groups: list[list[Position]] = [[]]
cur = block_groups[0]
for i, b in enumerate(_blocks):
if len(cur) == 0:
cur.append(b)
else:
if b[0] == cur[0][0]:
cur.append(b)
else:
cur = [b]
block_groups.append(cur)
# ? Sort each group by y-index
block_groups = [sorted(g, key=lambda b: b[1]) for g in block_groups]
# ? Print the shape line by line
st = min(g[0][1] for g in block_groups)
end = max(g[-1][1] for g in block_groups)
for g in block_groups:
cur = 0
for i in range(st, end + 1):
if cur >= len(g) or i < g[cur][1]:
print(" " * len(X), end="")
else:
print(X, end="")
cur += 1
print()
class transform(Enum):
U = 0
R = 1
D = 2
L = 3
MU = 4
MR = 5
MD = 6
ML = 7
def mirror(pos: Position) -> Position:
return Position(-pos[0], pos[1])
transform_func: dict[transform, Callable[[Position], Position]] = {}
transform_func[transform.U] = lambda pos: pos
transform_func[transform.R] = lambda pos: Position(pos[1], -pos[0])
transform_func[transform.D] = lambda pos: Position(-pos[0], -pos[1])
transform_func[transform.L] = lambda pos: Position(-pos[1], pos[0])
transform_func[transform.MU] = lambda pos: transform_func[transform.U](mirror(pos))
transform_func[transform.MR] = lambda pos: transform_func[transform.R](mirror(pos))
transform_func[transform.MD] = lambda pos: transform_func[transform.D](mirror(pos))
transform_func[transform.ML] = lambda pos: transform_func[transform.L](mirror(pos))
def get_transform(blocks: Blocks, t: transform) -> Blocks:
return Blocks([transform_func[t](pos) for pos in blocks])
def get_all_transforms(blocks: Blocks) -> dict[Blocks, transform]:
return {get_transform(blocks, t): t for t in transform}
class Brick:
blocks: Blocks
id: int
def __init__(self, _blocks: list[vector2 | Position]) -> None:
self.blocks = Blocks(_blocks)
def build_bricks(json_path="bricks.json") -> list[Brick]:
with open(json_path, "r") as f:
_bricks = json.load(f)
bricks = list(map(lambda arr: Brick(arr), _bricks))
for i, b in enumerate(bricks):
b.id = i
return bricks