Compare commits
14 commits
b8409db06c
...
335db26ad4
| Author | SHA1 | Date | |
|---|---|---|---|
| 335db26ad4 | |||
| cea6397499 | |||
| f971917640 | |||
| 267ecaebc1 | |||
| 8fbd267d99 | |||
| ee8d11f84d | |||
| 05c7b5bfb9 | |||
| 11168d283e | |||
| 6600f416d2 | |||
| d1a1ce775e | |||
| 8a1910e174 | |||
| 4cbd10fe4d | |||
| 04bb4c93ac | |||
| 19c4a2626f |
2 changed files with 49 additions and 38 deletions
|
|
@ -91,10 +91,10 @@ def vdbg(seen, mask=("#", "."), M=None):
|
||||||
r = max(r, x)
|
r = max(r, x)
|
||||||
b = max(b, y)
|
b = max(b, y)
|
||||||
l = min(l, x)
|
l = min(l, x)
|
||||||
H = b + 1
|
H = b - t + 1
|
||||||
W = r + 1
|
W = r - l + 1
|
||||||
osr = 0 #t
|
osr = t
|
||||||
osc = 0 #l
|
osc = l
|
||||||
C, Z = mask
|
C, Z = mask
|
||||||
def _m(r, c):
|
def _m(r, c):
|
||||||
return M[r][c] if M else Z
|
return M[r][c] if M else Z
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,19 @@
|
||||||
from math import inf
|
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
|
import PIL
|
||||||
|
|
||||||
from output import ints
|
from output import svg, D, ints, vdbg
|
||||||
|
|
||||||
|
|
||||||
def solve(data):
|
def solve(data):
|
||||||
p1 = 0
|
p1 = 0
|
||||||
p2 = 0
|
p2 = 0
|
||||||
A = [tuple(ints(pos)) for pos in data.splitlines()]
|
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]
|
S = A[0]
|
||||||
V = set()
|
V = set()
|
||||||
W = set()
|
B = set()
|
||||||
while True:
|
while True:
|
||||||
V.add(S)
|
V.add(S)
|
||||||
W.add(S)
|
|
||||||
y, x = S
|
y, x = S
|
||||||
he = [(r, c) for r, c in A if r == y and (r, c) not in V]
|
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]
|
ve = [(r, c) for r, c in A if c == x and (r, c) not in V]
|
||||||
|
|
@ -37,41 +28,61 @@ def solve(data):
|
||||||
y2, x2 = E
|
y2, x2 = E
|
||||||
for r in range(min(y, y2), max(y, y2) + 1):
|
for r in range(min(y, y2), max(y, y2) + 1):
|
||||||
for c in range(min(x, x2), max(x, x2) + 1):
|
for c in range(min(x, x2), max(x, x2) + 1):
|
||||||
W.add((r, c))
|
B.add((r, c))
|
||||||
if E == A[0]:
|
if E == A[0]:
|
||||||
break
|
break
|
||||||
S = E
|
S = E
|
||||||
V = V | W
|
# 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))
|
||||||
for a, b in combinations(A, r=2):
|
for a, b in combinations(A, r=2):
|
||||||
y1, x1 = a
|
y1, x1 = a
|
||||||
y2, x2 = b
|
y2, x2 = b
|
||||||
x = abs(x1 - x2) + 1
|
x = abs(x1 - x2) + 1
|
||||||
y = abs(y1 - y2) + 1
|
y = abs(y1 - y2) + 1
|
||||||
p1 = max(p1, x * y)
|
p1 = max(p1, x * y)
|
||||||
if _within(W, a, b, T, R, B, L):
|
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
|
||||||
|
):
|
||||||
p2 = max(p2, max(p2, x * y))
|
p2 = max(p2, max(p2, x * y))
|
||||||
return p1, p2
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
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__":
|
if __name__ == "__main__":
|
||||||
|
inp = """
|
||||||
|
7,1
|
||||||
|
11,1
|
||||||
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3 """.strip()
|
||||||
|
|
||||||
with open("./input/09.txt", "r") as f:
|
with open("./input/09.txt", "r") as f:
|
||||||
inp = f.read().strip()
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue