This commit is contained in:
Anders Englöf Ytterström 2025-12-09 19:27:43 +01:00
parent f971917640
commit cea6397499
2 changed files with 67 additions and 32 deletions

View file

@ -1,5 +1,6 @@
import re import re
from math import inf from math import inf
from PIL import Image
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT # Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
D = [ D = [
@ -97,8 +98,40 @@ def vdbg(seen, mask=("#", "."), M=None):
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
O = []
for r in range(H): for r in range(H):
print("".join([C if (r + osr, c + osc) in seen else _m(r, c) for c in range(W)])) O.append("".join([C if (r + osr, c + osc) in seen else _m(r, c) for c in range(W)]))
print("\n".join(O))
def svg(seen):
"""Print-debug visited positions of a matrix"""
t = inf
l = inf
b = 0
r = 0
rects = []
for y, x in seen:
t = min(t, y)
r = max(r, x)
b = max(b, y)
l = min(l, x)
H = b - t + 1
W = r - l + 1
im = Image.new(mode="RGB", size=(W,H), color=(255,255,255))
print(H, "*", W)
for y, x in seen:
im.putpixel((x-l, y-t), (0, 0, 0, 255))
im.save("aoc.png")
for y, x in seen:
rects.append(f"<rect x='{x-l}' y='{y-t}' width='1' fill='black' stroke='black' stroke-width='10' height='1' />")
with open("svg.svg", "w") as f:
f.write(f"""
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}">
{"".join(rects)}
</svg>
""".strip())
def vvdbg(seen, h, w): def vvdbg(seen, h, w):

View file

@ -1,6 +1,7 @@
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):
@ -11,7 +12,6 @@ def solve(data):
S = A[0] S = A[0]
V = set() V = set()
B = set() B = set()
while True: while True:
V.add(S) V.add(S)
y, x = S y, x = S
@ -40,35 +40,37 @@ def solve(data):
y, x = Y, X y, x = Y, X
Q = [(y, x)] Q = [(y, x)]
V = V | B V = V | B
while Q: print(len(B))
yx = Q.pop() svg(B)
if yx in V: # while Q:
continue # yx = Q.pop()
V.add(yx) # if yx in V:
y, x = yx # continue
for dy, dx in D: # V.add(yx)
Q.append((dy + y, dx + x)) # y, x = yx
for a, b in combinations(A, r=2): # for dy, dx in D:
y1, x1 = a # Q.append((dy + y, dx + x))
y2, x2 = b # for a, b in combinations(A, r=2):
x = abs(x1 - x2) + 1 # y1, x1 = a
y = abs(y1 - y2) + 1 # y2, x2 = b
p1 = max(p1, x * y) # x = abs(x1 - x2) + 1
if ( # y = abs(y1 - y2) + 1
len( # p1 = max(p1, x * y)
set( # if (
[ # len(
(min(y1, y2), min(x1, x2)), # set(
(min(y1, y2), max(x1, x2)), # [
(max(y1, y2), min(x1, x2)), # (min(y1, y2), min(x1, x2)),
(max(y1, y2), max(x1, x2)), # (min(y1, y2), max(x1, x2)),
] # (max(y1, y2), min(x1, x2)),
) # (max(y1, y2), max(x1, x2)),
- V # ]
) # )
== 0 # - V
): # )
p2 = max(p2, max(p2, x * y)) # == 0
# ):
# p2 = max(p2, max(p2, x * y))
return p1, p2 return p1, p2