Compare commits
12 commits
335db26ad4
...
b8409db06c
| Author | SHA1 | Date | |
|---|---|---|---|
| b8409db06c | |||
| ae676bf112 | |||
| 34fb92403e | |||
| 8683885438 | |||
| 68be80a4d4 | |||
| 2d3caced7f | |||
| ac6b97590c | |||
| c3c2dd5759 | |||
| 4a070e827b | |||
| 4927b78e59 | |||
| 56ca9d19c4 | |||
| f0f78fa25c |
2 changed files with 38 additions and 49 deletions
|
|
@ -91,10 +91,10 @@ def vdbg(seen, mask=("#", "."), M=None):
|
|||
r = max(r, x)
|
||||
b = max(b, y)
|
||||
l = min(l, x)
|
||||
H = b - t + 1
|
||||
W = r - l + 1
|
||||
osr = t
|
||||
osc = l
|
||||
H = b + 1
|
||||
W = r + 1
|
||||
osr = 0 #t
|
||||
osc = 0 #l
|
||||
C, Z = mask
|
||||
def _m(r, c):
|
||||
return M[r][c] if M else Z
|
||||
|
|
|
|||
|
|
@ -1,19 +1,28 @@
|
|||
from math import inf
|
||||
from itertools import combinations
|
||||
import PIL
|
||||
|
||||
from output import svg, D, ints, vdbg
|
||||
from output import ints
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
A = [tuple(ints(pos)) for pos in data.splitlines()]
|
||||
|
||||
T = inf
|
||||
R = 0
|
||||
B = 0
|
||||
L = inf
|
||||
for r, c in A:
|
||||
T = min(r, T)
|
||||
R = max(c, R)
|
||||
B = max(r, B)
|
||||
L = min(c, L)
|
||||
S = A[0]
|
||||
V = set()
|
||||
B = set()
|
||||
W = set()
|
||||
while True:
|
||||
V.add(S)
|
||||
W.add(S)
|
||||
y, x = S
|
||||
he = [(r, c) for r, c in A if r == y and (r, c) not in V]
|
||||
ve = [(r, c) for r, c in A if c == x and (r, c) not in V]
|
||||
|
|
@ -28,61 +37,41 @@ def solve(data):
|
|||
y2, x2 = E
|
||||
for r in range(min(y, y2), max(y, y2) + 1):
|
||||
for c in range(min(x, x2), max(x, x2) + 1):
|
||||
B.add((r, c))
|
||||
W.add((r, c))
|
||||
if E == A[0]:
|
||||
break
|
||||
S = E
|
||||
# find out a better start for Flood fill by visualizing
|
||||
Y = 50000
|
||||
X = 25000
|
||||
# or, find a way to floodfill without BFS
|
||||
# end
|
||||
y, x = Y, X
|
||||
Q = [(y, x)]
|
||||
V = V | B
|
||||
while Q:
|
||||
yx = Q.pop()
|
||||
if yx in V:
|
||||
continue
|
||||
V.add(yx)
|
||||
y, x = yx
|
||||
for dy, dx in D:
|
||||
Q.append((dy + y, dx + x))
|
||||
V = V | W
|
||||
for a, b in combinations(A, r=2):
|
||||
y1, x1 = a
|
||||
y2, x2 = b
|
||||
x = abs(x1 - x2) + 1
|
||||
y = abs(y1 - y2) + 1
|
||||
p1 = max(p1, x * y)
|
||||
if (
|
||||
len(
|
||||
set(
|
||||
[
|
||||
(min(y1, y2), min(x1, x2)),
|
||||
(min(y1, y2), max(x1, x2)),
|
||||
(max(y1, y2), min(x1, x2)),
|
||||
(max(y1, y2), max(x1, x2)),
|
||||
]
|
||||
)
|
||||
- V
|
||||
)
|
||||
== 0
|
||||
):
|
||||
if _within(W, a, b, T, R, B, L):
|
||||
p2 = max(p2, max(p2, x * y))
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
inp = """
|
||||
7,1
|
||||
11,1
|
||||
11,7
|
||||
9,7
|
||||
9,5
|
||||
2,5
|
||||
2,3
|
||||
7,3 """.strip()
|
||||
def _within(W, a, b, T, R, B, L):
|
||||
y1, x1 = a
|
||||
y2, x2 = b
|
||||
for r in range(min(y1, y2) + 1, max(y1, y2)):
|
||||
for c in range(min(x1, x2) + 1, max(x1, x2)):
|
||||
if (r, c) in W:
|
||||
continue
|
||||
for s, e in [(T, r), (r, B)]:
|
||||
z = sum((nr, c) in W for nr in range(s, e))
|
||||
if z and z % 2 == 0:
|
||||
return False
|
||||
for s, e in [(L, c), (c, R)]:
|
||||
z = sum((r, nc) in W for nc in range(s, e))
|
||||
if z and z % 2 == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/09.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue