Compare commits

...

4 commits

Author SHA1 Message Date
11137463c7 Solve 2025 day 12 pt 1-2
Got this one ruined due to a post not masking
spoilers:

https://www.reddit.com/r/adventofcode/comments/1pkxibs/2025_day_25_part_1_still_pretty_clueless_why_its/

Basically, ignore the test case and just wing it by
asuming the 3x3 can be placed without overlaps. Id
the number of shapes * 3x3 is bigger than the available
canvas, it won't fit.

A naive first check to do before making premature
optimizations.

I would have gone the zip() hell route to prove
it actually works, but after grasping at the puzzle
input i realized it would tka a looong time to
calculate.
2025-12-14 22:41:23 +01:00
01bfcc1351 . 2025-12-11 23:36:38 +01:00
044df24990 Solve 2025 day 11 pt 1-2
For p1 a BFS algorithm is fast enough. For pt 2,
for the third day in a row, it did not work.

I caved in and used recursion and memoisation for
DFS instead, instantly fast as lightning.

I finished by making the pt 2 code compatible with
pt 1.
2025-12-11 21:42:15 +01:00
0723a8223f day 10 2025-12-11 20:45:13 +01:00
4 changed files with 172 additions and 20 deletions

View file

@ -1,6 +1,5 @@
from math import inf
from itertools import combinations
from output import ints
@ -42,43 +41,59 @@ def solve(data):
break
S = E
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):
i += 1
if i % 100 == 0:
print(f"{str(i):>6} / {S}")
y1, x1 = a
y2, x2 = b
x = abs(x1 - x2) + 1
y = abs(y1 - y2) + 1
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))
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__":
with open("./input/09.txt", "r") as f:
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)
print(p1)
print(p2)
assert p1 == 4749838800
assert p2 == 1624057680
assert p1 == 50
assert p2 == 24
# assert p1 == 4749838800
# assert p2 == 1624057680

View file

@ -0,0 +1,78 @@
import re
import functools
from math import inf
from pprint import pprint
from collections import deque, Counter, defaultdict
from heapq import heappop, heappush
from itertools import compress, combinations, chain, permutations
from output import matrix, D, DD, ADJ, ints, sints, mhd, mdbg, vdbg, cw, ccw, bk
def solve(data):
r = r"^\[(.+)\] (.+) \{(.+)\}$"
p1 = 0
for j, line in enumerate(data.splitlines(), start=1):
il, b, j = re.findall(r, line)[0]
B = [set(ints(s)) for s in b.split()]
E = set([i for i, s in enumerate(il) if s == "#"])
J = ints(j)
p = 0
L = set()
while L != E:
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
# 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
p2 = None
return p1, p2
if __name__ == "__main__":
import os
# use dummy data
inp = """
[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}
[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}
[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5} """.strip()
# uncomment to instead use stdin
# import sys; inp = sys.stdin.read().strip()
# uncomment to use AoC provided puzzle input
with open("./input/10.txt", "r") as f:
inp = f.read().strip()
# uncomment to do initial data processing shared by part 1-2
p1, p2 = solve(inp)
print(p1)
os.system(f"echo {p1} | wl-copy")
print(p2)
os.system(f"echo {p2} | wl-copy")
# uncomment and replace 0 with actual output to refactor code
# and ensure nonbreaking changes
# assert p1 == 0
# assert p2 == 0

View file

@ -0,0 +1,33 @@
import functools
from collections import defaultdict
def solve(data):
G = defaultdict(list)
for line in data.splitlines():
f, *t = line.split()
G[f[:-1]] = t
@functools.cache
def _traverse(k, fasttrack=True, fft=False, dac=False):
if k == "out":
return fasttrack or (fft and dac)
return sum(
_traverse(nk, fasttrack, fft or k == "fft", dac or k == "dac")
for nk in G[k]
)
return _traverse("you"), _traverse("svr", fasttrack=False)
if __name__ == "__main__":
with open("./input/11.txt", "r") as f:
inp = f.read().strip()
p1, p2 = solve(inp)
print(p1)
print(p2)
assert p1 == 649
assert p2 == 458948453421420

View file

@ -0,0 +1,26 @@
from output import ints
def solve(data):
p1 = 0
data = data.split("\n\n")
Q = data.pop().splitlines()
for q in Q:
W, H, *C = ints(q)
if W * H >= sum(C) * 9:
p1 += 1
p2 = "God jul!"
return p1, p2
if __name__ == "__main__":
with open("./input/12.txt", "r") as f:
inp = f.read().strip()
p1, p2 = solve(inp)
print(p1)
print(p2)
assert p1 == 440
assert p2 == "God jul!"