Do day 3 and 4 for 2025

This commit is contained in:
2025-12-26 20:18:44 -05:00
parent 183f909508
commit e5421854a9
3 changed files with 71 additions and 1 deletions

35
2025/d03.py Normal file
View File

@@ -0,0 +1,35 @@
from lib import get_data
data = get_data(__file__)
def max_joltage(xs: str, target_len: int) -> int:
def dynamic(xs: str) -> dict[int, str]:
if len(xs) == 0:
return {0: ""}
rs = dynamic(xs[1:])
for i in range(target_len, 0, -1):
if not (i - 1) in rs:
continue
nv = int(xs[0] + rs[i - 1])
if not i in rs:
rs[i] = str(nv)
elif nv > int(rs[i]):
rs[i] = str(nv)
return rs
rs = dynamic(xs)
return int(rs[target_len])
r1 = 0
r2 = 0
for line in data.splitlines():
r1 += max_joltage(line, 2)
r2 += max_joltage(line, 12)
print(r1)
print(r2)

32
2025/d04.py Normal file
View File

@@ -0,0 +1,32 @@
from lib import get_data, Grid2D
data = get_data(__file__)
g = Grid2D(data)
r = 0
adjs = []
for pos in g.all_coords():
if g[pos] != "@":
continue
nr_of_rolls_adj = len([nb for nb in g.neighbors_adj(pos) if g[nb] == "@"])
if nr_of_rolls_adj < 4:
r += 1
adjs.append(pos)
print(r)
r = 0
while True:
adjs = []
for pos in g.all_coords():
if g[pos] != "@":
continue
nr_of_rolls_adj = len([nb for nb in g.neighbors_adj(pos) if g[nb] == "@"])
if nr_of_rolls_adj < 4:
adjs.append(pos)
if adjs:
r += len(adjs)
for a in adjs:
g[a] = "."
else:
break
print(r)