-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm0130.py
295 lines (248 loc) · 8.14 KB
/
m0130.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions
surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
Example:
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
Explanation:
Surrounded regions shouldn’t be on the border, which means that any 'O' on the
border of the board are not flipped to 'X'. Any 'O' that is not on the border
and it is not connected to an 'O' on the border will be flipped to 'X'. Two
cells are connected if they are adjacent cells connected horizontally or
vertically.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import IntEnum, unique
from functools import reduce
from typing import Optional, Tuple
import numpy as np
@dataclass(frozen=True)
class Segment:
"""line segment (i.e. adjacent dots)
"""
lower: int
upper: int
def distance(self, seg: Segment) -> int:
new_upper = min(self.upper, seg.upper)
new_lower = max(self.lower, seg.lower)
if new_upper >= new_lower:
return 0
return new_lower - new_upper
def adjacent(self, seg: Segment) -> bool:
if self.distance(seg) == 0:
return True
return False
def union(self, seg: Segment) -> Optional[Segment]:
if self.distance(seg) >= 2:
return None
new_upper = max(self.upper, seg.upper)
new_lower = min(self.lower, seg.lower)
return (type(self))(new_lower, new_upper)
@dataclass(frozen=True)
class SegmentGroup:
position: int
segments: Tuple[Segment, ...]
y: bool = False
@property
def empty(self) -> bool:
return not self.segments
def adjacent(self, seg: Segment) -> bool:
return reduce(lambda x, y: x or y,
tuple(seg.adjacent(s) for s in self.segments))
def move(self, sg: SegmentGroup) -> Optional[SegmentGroup]:
if abs(self.position - sg.position) >= 2:
return None
bools = tuple(self.adjacent(s) for s in sg.segments)
return SegmentGroup(
sg.position,
tuple(s for b, s in zip(bools, sg.segments) if b),
self.y,
)
@property
def points(self):
if not self.y:
return tuple((self.position, j) for seg in self.segments
for j in range(seg.lower, seg.upper + 1))
return tuple((j, self.position) for seg in self.segments
for j in range(seg.lower, seg.upper + 1))
@unique
class State(IntEnum):
DOING = 0
MERGED_NOT_COPIED = 1
COPIED_NOT_FINISHED = 2
DONE = 3
@dataclass(frozen=True)
class FSM:
"""
the first element of the `ordered_groups` should be on border
"""
ordered_groups: Tuple[Optional[SegmentGroup], ...]
acc: Tuple[SegmentGroup, ...]
@property
def length(self) -> int:
return len(self.ordered_groups)
@property
def state(self) -> State:
if self.length <= 1:
return State.DONE
sg = self.ordered_groups[0]
if not sg or sg.empty:
return State.DONE
return State.DOING
@property
def points(self) -> Tuple[Tuple[int, int], ...]:
return tuple(p for sg in self.acc for p in sg.points)
def transform(self) -> FSM:
assert self.state == State.DOING
sg1 = self.ordered_groups[0]
sg2 = self.ordered_groups[1]
new_sg = sg1.move(sg2)
new_groups = (new_sg, *self.ordered_groups[2:])
if not new_sg or new_sg.empty:
acc = self.acc
else:
acc = (*self.acc, new_sg)
return (type(self))(new_groups, acc)
class SurroundedRegion:
__slots__ = ('array', )
def __init__(self, array: np.array):
self.array = array
@staticmethod
def merge_segments(segments: Tuple[Segment, ...]) -> Tuple[Segment, ...]:
"""
Examples:
>>> merge_segments((Segment(1, 2), Segment(2, 3), Segment(5, 7)))
>>> (Segment(1, 3), Segment(5, 7))
"""
def helper(
segments_: Tuple[Segment, ...],
acc: Tuple[Segment, ...],
) -> Tuple[Segment, ...]:
if not segments_:
return acc
seg1 = segments_[0]
if len(segments_) == 1:
return (*acc, seg1)
seg2 = segments_[1]
new_seg = seg1.union(seg2)
if not new_seg:
return helper(segments_[1:], (*acc, seg1))
return helper((new_seg, *segments_[2:]), acc)
return helper(segments, ())
@classmethod
def to_segment_group(
cls,
position: int,
array: np.array,
y: bool = False,
) -> SegmentGroup:
indexes = (array == 0).nonzero()[0]
segments = tuple(Segment(i, i) for i in indexes)
merged_segments = cls.merge_segments(segments)
return SegmentGroup(position, merged_segments, y)
@property
def shape(self) -> Tuple[int, int]:
return self.array.shape
@property
def x_length(self) -> int:
return self.shape[0]
@property
def y_length(self) -> int:
return self.shape[1]
def segment_group(
self,
y: bool = False,
desc: bool = False,
) -> Tuple[SegmentGroup, ...]:
if not y:
sgs = tuple(
self.to_segment_group(i, self.array[i, :], y)
for i in range(self.x_length))
else:
sgs = tuple(
self.to_segment_group(i, self.array[:, i], y)
for i in range(self.y_length))
if desc:
return sgs[::-1]
return sgs
@staticmethod
def looper(fsm: FSM) -> Tuple[Tuple[int, int], ...]:
fsm_ = fsm
while True:
if fsm_.state == State.DONE:
return fsm_.points
fsm_ = fsm_.transform()
def zeros(self):
def helper(
y: bool = False,
desc: bool = False,
) -> Tuple[Tuple[int, int], ...]:
sgs = self.segment_group(y, desc)
fsm = FSM(sgs, (sgs[0], ))
return self.looper(fsm)
bools = ((False, False), (False, True), (True, False), (True, True))
seq_points = tuple(helper(y, d) for y, d in bools)
return tuple(sorted({p for tp in seq_points for p in tp}))
def solution(self):
zero_points = self.zeros()
mat = np.ones(self.shape, np.int8)
for p in zero_points:
mat[p[0], p[1]] = 0
return mat
if __name__ == '__main__':
ipt_1 = np.array([
[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 1, 0, 1],
[1, 0, 1, 1],
])
exp_1 = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 0, 1, 1],
])
ipt_2 = np.array([
[1, 1, 1, 1, 1, 0, 1],
[0, 0, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 0, 0, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
])
exp_2 = np.array([
[1, 1, 1, 1, 1, 0, 1],
[0, 0, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
])
# segment = Segment(3, 7)
# assert segment.union(Segment(4, 6)) == Segment(3, 7)
# assert segment.union(Segment(1, 1)) is None
# assert segment.union(Segment(1, 2)) == Segment(1, 7)
# assert segment.union(Segment(1, 3)) == Segment(1, 7)
# assert segment.union(Segment(7, 8)) == Segment(3, 8)
# segment_group = SegmentGroup(1, (Segment(0, 3), Segment(6, 8)))
# assert segment_group.adjacent(Segment(4, 5)) is False
# assert segment_group.adjacent(Segment(9, 10)) is False
# assert segment_group.adjacent(Segment(2, 5)) is True
# assert segment_group.adjacent(Segment(1, 9)) is True
assert ((SurroundedRegion(ipt_1).solution() - exp_1) == 0).all()
assert ((SurroundedRegion(ipt_2).solution() - exp_2) == 0).all()