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

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

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)