Solve 2020 day 24 and 25 and 2018 day 23 part 2

This commit is contained in:
felixm 2024-10-08 19:36:45 -04:00
parent ea28a17ab9
commit 7d1dc3f95e
4 changed files with 131 additions and 27 deletions

View File

@ -1,32 +1,45 @@
from lib import get_data, str_to_ints
def dist(a, b):
return sum(abs(x - y) for x, y in zip(a[:3], b[:3]))
from lib import get_data, ints
data = get_data(__file__)
data = """pos=<10,12,12>, r=2
pos=<12,14,12>, r=2
pos=<16,12,12>, r=4
pos=<14,14,14>, r=6
pos=<50,50,50>, r=200
pos=<10,10,10>, r=5
"""
max_bot = None
bots = []
for line in data.splitlines():
xs = str_to_ints(line)
if max_bot is None:
max_bot = xs
elif xs[3] > max_bot[3]:
max_bot = xs
bots.append(xs)
X, Y, Z, RADIUS = 0, 1, 2, 3
assert max_bot is not None
t = 0
def dist(a, b):
return sum(abs(x - y) for x, y in zip(a[:RADIUS], b[:RADIUS]))
bots = [ints(line) for line in data.splitlines()]
max_bot = max(bots, key=lambda b: b[RADIUS])
r = max_bot[3]
for bot in bots:
if dist(bot, max_bot) <= r:
t += 1
t = sum(1 for bot in bots if dist(bot, max_bot) <= r)
print(t)
from z3 import Int, If, Abs, Sum, IntVal, Optimize, set_param
x = Int("x")
y = Int("y")
z = Int("z")
z3zero = IntVal(0)
z3one = IntVal(1)
n_out_of_range = Int("n_out_of_range")
dist_origin = Int("dist_origin")
bots_out_of_range = [
If(Abs(b[X] - x) + Abs(b[Y] - y) + Abs(b[Z] - z) > b[RADIUS], z3one, z3zero)
for b in bots
]
set_param("parallel.enable", True)
opt = Optimize()
opt.add(n_out_of_range == Sum(bots_out_of_range))
opt.add(dist_origin == Abs(x) + Abs(y) + Abs(z))
opt.minimize(n_out_of_range)
opt.minimize(dist_origin)
res = opt.check()
m = opt.model()
print(m[dist_origin])

59
2020/d24.py Normal file
View File

@ -0,0 +1,59 @@
from lib import get_data, add2
from collections import defaultdict
DIRS = {
"e": (0, 2),
"w": (0, -2),
"se": (1, 1),
"sw": (1, -1),
"nw": (-1, -1),
"ne": (-1, 1),
}
data = get_data(__file__)
tiles_black = set()
for row in data.splitlines():
row = row.strip()
i = 0
pos = (0, 0)
while i < len(row):
d = None
if row[i] in DIRS:
d = DIRS[row[i]]
i += 1
elif "".join(row[i : i + 2]) in DIRS:
d = DIRS["".join(row[i : i + 2])]
i += 2
else:
assert False
assert d is not None
pos = add2(pos, d)
if pos in tiles_black:
tiles_black.remove(pos)
else:
tiles_black.add(pos)
print(len(tiles_black))
for _ in range(100):
nbs = defaultdict(int)
for bt in tiles_black:
for d in DIRS.values():
nb = add2(bt, d)
nbs[nb] += 1
new_tiles_black = set()
for pos, count in nbs.items():
if pos in tiles_black:
if count == 0 or count > 2:
pass
else:
new_tiles_black.add(pos)
else:
if count == 2:
new_tiles_black.add(pos)
tiles_black = new_tiles_black
print(len(tiles_black))

32
2020/d25.py Normal file
View File

@ -0,0 +1,32 @@
from lib import get_data, ints
data = get_data(__file__)
a, b = ints(data)
v = 1
subject_number = 7
al, bl = None, None
for loop in range(100_000_000):
v *= subject_number
v %= 20201227
if al is None and v == a:
al = loop + 1
if bl is None and v == b:
bl = loop + 1
if al and bl:
break
assert al is not None
assert bl is not None
# print(al, bl)
v = 1
subject_number = b
for _ in range(al):
v *= subject_number
v %= 20201227
print(v)

View File

@ -162,8 +162,8 @@ Solutions and utility script for Advent of Code challenges in Python.
- Day 21: 23:02 (Simply to slow)
- Day 22: 45:49 (Simple and too slow)
- Day 23: 105:00 (Sad)
- Day 24:
- Day 25:
- Day 24: 15:38 (Close to leaderboard)
- Day 25: 14:40 (Way too slow)
## AoC 2022