Finally solve 2019 day 12 and others

This commit is contained in:
2024-09-08 10:21:20 -04:00
parent fa6ea1dfbe
commit bf88f13791
4 changed files with 194 additions and 12 deletions

57
2020/d10.py Normal file
View File

@@ -0,0 +1,57 @@
from lib import get_data
from collections import defaultdict
from functools import lru_cache
@lru_cache
def count(xs, current, target):
delta_target = target - current
if len(xs) == 0:
if delta_target <= 3:
return 1
else:
return 0
x = xs[0]
xs = xs[1:]
total = 0
delta = x - current
if delta > 3:
return 0
if delta <= 3:
total += count(xs, x, target)
total += count(xs, current, target)
return total
def part_1(data):
xs = list(map(int, data.strip().splitlines()))
out = max(xs) + 3
ds = defaultdict(int)
c = 0
for x in sorted(xs):
d = x - c
ds[d] += 1
c = x
d = out - c
ds[d] += 1
a, b = list(ds.values())
print(a * b)
xs = tuple(sorted(xs))
print(count(xs, 0, out))
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()

89
2020/d11.py Normal file
View File

@@ -0,0 +1,89 @@
from lib import get_data, Grid2D, add2
data = """L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL"""
def part_1(data):
g = Grid2D(data)
seen = set()
while True:
h = g.hash()
if h in seen:
break
seen.add(h)
ng = g.clone_with_val(".")
for r in range(g.n_rows):
for c in range(g.n_cols):
p = (r, c)
s = g[p]
if s == ".":
continue
occupied = sum([1 if g[nb] == "#" else 0 for nb in g.neighbors_adj(p)])
if s == "L" and occupied == 0:
ng[p] = "#"
elif s == "#" and occupied >= 4:
ng[p] = "L"
else:
ng[p] = s
g = ng
print(len(g.find("#")))
def part_2(data):
g = Grid2D(data)
seen = set()
while True:
h = g.hash()
if h in seen:
break
seen.add(h)
ng = g.clone_with_val(".")
for r in range(g.n_rows):
for c in range(g.n_cols):
p = (r, c)
s = g[p]
if s == ".":
continue
occupied = 0
for dir in g.COORDS_ORTH + g.COORDS_DIAG:
np = p
while True:
np = add2(np, dir)
if not g.contains(np):
break
elif g[np] == "#":
occupied += 1
break
elif g[np] == "L":
break
if s == "L" and occupied == 0:
ng[p] = "#"
elif s == "#" and occupied >= 5:
ng[p] = "L"
else:
ng[p] = s
g = ng
print(len(g.find("#")))
def main():
data = get_data(__file__)
part_1(data)
part_2(data)
if __name__ == "__main__":
main()