Compare commits
4 commits
a585a687c9
...
11137463c7
| Author | SHA1 | Date | |
|---|---|---|---|
| 11137463c7 | |||
| 01bfcc1351 | |||
| 044df24990 | |||
| 0723a8223f |
3 changed files with 59 additions and 54 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
from math import inf
|
from math import inf
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
|
|
||||||
from output import ints
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -42,43 +41,59 @@ def solve(data):
|
||||||
break
|
break
|
||||||
S = E
|
S = E
|
||||||
V = V | W
|
V = V | W
|
||||||
|
|
||||||
|
def _within(a, b):
|
||||||
|
y1, x1 = a
|
||||||
|
y2, x2 = b
|
||||||
|
T = min(y1, y2)
|
||||||
|
R = max(x1, x2)
|
||||||
|
B = max(y1, y2)
|
||||||
|
L = min(x1, x2)
|
||||||
|
return all([
|
||||||
|
sum((r, L + 1) in W for r in range(T + 1, B)) % 2 == 0,
|
||||||
|
sum((r, R - 1) in W for r in range(T + 1, B)) % 2 == 0,
|
||||||
|
sum((T + 1, c) in W for c in range(L + 1, R)) % 2 == 0,
|
||||||
|
sum((B - 1, c) in W for c in range(L + 1, R)) % 2 == 0,
|
||||||
|
])
|
||||||
|
|
||||||
|
S = len(list(combinations(A, r=2)))
|
||||||
|
i = 0
|
||||||
for a, b in combinations(A, r=2):
|
for a, b in combinations(A, r=2):
|
||||||
|
i += 1
|
||||||
|
if i % 100 == 0:
|
||||||
|
print(f"{str(i):>6} / {S}")
|
||||||
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 _within(a, b):
|
||||||
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__":
|
||||||
with open("./input/09.txt", "r") as f:
|
with open("./input/09.txt", "r") as f:
|
||||||
inp = f.read().strip()
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
in_p = """
|
||||||
|
7,1
|
||||||
|
11,1
|
||||||
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3 """.strip()
|
||||||
|
|
||||||
p1, p2 = solve(inp)
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
print(p1)
|
print(p1)
|
||||||
print(p2)
|
print(p2)
|
||||||
|
|
||||||
assert p1 == 4749838800
|
assert p1 == 50
|
||||||
assert p2 == 1624057680
|
assert p2 == 24
|
||||||
|
# assert p1 == 4749838800
|
||||||
|
# assert p2 == 1624057680
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import re
|
import re
|
||||||
import functools
|
import functools
|
||||||
from math import gcd, inf
|
from math import inf
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from collections import deque, Counter, defaultdict
|
from collections import deque, Counter, defaultdict
|
||||||
from heapq import heappop, heappush
|
from heapq import heappop, heappush
|
||||||
|
|
@ -12,13 +12,13 @@ from output import matrix, D, DD, ADJ, ints, sints, mhd, mdbg, vdbg, cw, ccw, bk
|
||||||
def solve(data):
|
def solve(data):
|
||||||
r = r"^\[(.+)\] (.+) \{(.+)\}$"
|
r = r"^\[(.+)\] (.+) \{(.+)\}$"
|
||||||
p1 = 0
|
p1 = 0
|
||||||
p2 = 0
|
|
||||||
for line in data.splitlines():
|
for j, line in enumerate(data.splitlines(), start=1):
|
||||||
il, b, j = re.findall(r, line)[0]
|
il, b, j = re.findall(r, line)[0]
|
||||||
B = [set(ints(s)) for s in b.split()]
|
B = [set(ints(s)) for s in b.split()]
|
||||||
E = set([i for i, s in enumerate(il) if s == "#"])
|
E = set([i for i, s in enumerate(il) if s == "#"])
|
||||||
j = tuple(ints(j))
|
J = ints(j)
|
||||||
|
|
||||||
p = 0
|
p = 0
|
||||||
L = set()
|
L = set()
|
||||||
while L != E:
|
while L != E:
|
||||||
|
|
@ -31,30 +31,20 @@ def solve(data):
|
||||||
break
|
break
|
||||||
if L == E:
|
if L == E:
|
||||||
break
|
break
|
||||||
|
# p = 0
|
||||||
|
# L = [0] * len(J)
|
||||||
|
# while L != J:
|
||||||
|
# p += 1
|
||||||
|
# for bp in combinations(B, p):
|
||||||
|
# L = set()
|
||||||
|
# for b in bp:
|
||||||
|
# L = L ^ b
|
||||||
|
# if L == E:
|
||||||
|
# break
|
||||||
|
# if L == E:
|
||||||
|
# break
|
||||||
p1 += p
|
p1 += p
|
||||||
|
p2 = None
|
||||||
@functools.cache
|
|
||||||
def _press(T):
|
|
||||||
if not any(T):
|
|
||||||
return 0
|
|
||||||
L = set(i for i, s in enumerate(T) if s % 2 == 1)
|
|
||||||
R = inf
|
|
||||||
for p in [bb for ii, bb in enumerate(B) if ii in L]:
|
|
||||||
tt = list(T)
|
|
||||||
for b in p:
|
|
||||||
for i in b:
|
|
||||||
tt[i] -= 1
|
|
||||||
if any(j < 0 for j in tt):
|
|
||||||
continue
|
|
||||||
Th = tuple(j // 2 for j in tt)
|
|
||||||
n = _press(Th)
|
|
||||||
if n is None:
|
|
||||||
continue
|
|
||||||
R = min(R, len(p) + 2 * n)
|
|
||||||
return R
|
|
||||||
|
|
||||||
p2 = _press(j)
|
|
||||||
|
|
||||||
return p1, p2
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -72,7 +62,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
# uncomment to use AoC provided puzzle input
|
# uncomment to use AoC provided puzzle input
|
||||||
with open("./input/10.txt", "r") as f:
|
with open("./input/10.txt", "r") as f:
|
||||||
in_p = f.read().strip()
|
inp = f.read().strip()
|
||||||
|
|
||||||
# uncomment to do initial data processing shared by part 1-2
|
# uncomment to do initial data processing shared by part 1-2
|
||||||
p1, p2 = solve(inp)
|
p1, p2 = solve(inp)
|
||||||
|
|
@ -84,5 +74,5 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
# uncomment and replace 0 with actual output to refactor code
|
# uncomment and replace 0 with actual output to refactor code
|
||||||
# and ensure nonbreaking changes
|
# and ensure nonbreaking changes
|
||||||
# assert p1 == 466
|
# assert p1 == 0
|
||||||
# assert p2 == 17214
|
# assert p2 == 0
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
def solve(data):
|
def solve(data):
|
||||||
G = defaultdict(set)
|
G = defaultdict(list)
|
||||||
for line in data.splitlines():
|
for line in data.splitlines():
|
||||||
f, *t = line.split()
|
f, *t = line.split()
|
||||||
G[f[:-1]] = set(t)
|
G[f[:-1]] = t
|
||||||
|
|
||||||
@functools.cache
|
@functools.cache
|
||||||
def _traverse(k, fasttrack=True, fft=False, dac=False):
|
def _traverse(k, fasttrack=True, fft=False, dac=False):
|
||||||
|
|
@ -15,7 +15,7 @@ def solve(data):
|
||||||
return sum(
|
return sum(
|
||||||
_traverse(nk, fasttrack, fft or k == "fft", dac or k == "dac")
|
_traverse(nk, fasttrack, fft or k == "fft", dac or k == "dac")
|
||||||
for nk in G[k]
|
for nk in G[k]
|
||||||
)
|
)
|
||||||
|
|
||||||
return _traverse("you"), _traverse("svr", fasttrack=False)
|
return _traverse("you"), _traverse("svr", fasttrack=False)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue