Compare commits
2 commits
8fbd267d99
...
f971917640
| Author | SHA1 | Date | |
|---|---|---|---|
| f971917640 | |||
| 267ecaebc1 |
2 changed files with 114 additions and 4 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import re
|
||||
from math import inf
|
||||
|
||||
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
|
||||
D = [
|
||||
|
|
@ -78,12 +79,26 @@ def mdbg(m):
|
|||
print("".join(r))
|
||||
|
||||
|
||||
def vdbg(seen, h, w, C="#", M=None):
|
||||
def vdbg(seen, mask=("#", "."), M=None):
|
||||
"""Print-debug visited positions of a matrix"""
|
||||
t = inf
|
||||
l = inf
|
||||
b = 0
|
||||
r = 0
|
||||
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
|
||||
osr = t
|
||||
osc = l
|
||||
C, Z = mask
|
||||
def _m(r, c):
|
||||
return M[r][c] if M else "."
|
||||
for r in range(h):
|
||||
print("".join([C if (r, c) in seen else _m(r, c) for c in range(w)]))
|
||||
return M[r][c] if M else Z
|
||||
for r in range(H):
|
||||
print("".join([C if (r + osr, c + osc) in seen else _m(r, c) for c in range(W)]))
|
||||
|
||||
|
||||
def vvdbg(seen, h, w):
|
||||
|
|
|
|||
95
2025-python/output/day_09.py
Normal file
95
2025-python/output/day_09.py
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
from itertools import combinations
|
||||
|
||||
from output import ints
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
A = [tuple(ints(pos)) for pos in data.splitlines()]
|
||||
|
||||
S = A[0]
|
||||
V = set()
|
||||
B = set()
|
||||
|
||||
while True:
|
||||
V.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]
|
||||
if not he and not ve:
|
||||
E = A[0]
|
||||
elif he and ve:
|
||||
E = min(min(he), min(ve))
|
||||
elif he and not ve:
|
||||
E = min(he)
|
||||
else:
|
||||
E = min(ve)
|
||||
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))
|
||||
if E == A[0]:
|
||||
break
|
||||
S = E
|
||||
# find out a better start for Flood fill by visualizing
|
||||
Y = 1841
|
||||
X = 53478
|
||||
# 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):
|
||||
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
|
||||
):
|
||||
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()
|
||||
|
||||
with open("./input/09.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 4749838800
|
||||
assert p2 == 1624057680
|
||||
Loading…
Add table
Reference in a new issue