Finish 2015.
This commit is contained in:
52
2015/d19.py
52
2015/d19.py
@@ -1,41 +1,41 @@
|
||||
import sys
|
||||
from random import shuffle
|
||||
|
||||
data = open(0).read().strip()
|
||||
with open("i19.txt", "r") as f:
|
||||
data = f.read()
|
||||
|
||||
eqs = []
|
||||
text = ""
|
||||
molecule = ""
|
||||
for line in data.splitlines():
|
||||
if "=>" in line:
|
||||
lhs, rhs = line.split(" => ")
|
||||
eqs.append((lhs, rhs))
|
||||
elif line:
|
||||
text = line
|
||||
molecule = line.strip()
|
||||
|
||||
molecules = set()
|
||||
for lhs, rhs in eqs:
|
||||
for i in range(len(text)):
|
||||
f, t = text[:i], text[i:]
|
||||
for i in range(len(molecule)):
|
||||
f, t = molecule[:i], molecule[i:]
|
||||
if t.startswith(lhs):
|
||||
n = f + t.replace(lhs, rhs, 1)
|
||||
molecules.add(n)
|
||||
print(len(molecules))
|
||||
|
||||
# m = float("inf")
|
||||
# for _ in range(100000):
|
||||
# getout = False
|
||||
# c = str(text)
|
||||
# for i in range(10**4):
|
||||
# if i % 1000 == 0:
|
||||
# shuffle(eqs)
|
||||
# for lhs, rhs in eqs:
|
||||
# if rhs in c:
|
||||
# c = c.replace(rhs, lhs, 1)
|
||||
# if c == "e":
|
||||
# if i < m:
|
||||
# m = i
|
||||
# print(i)
|
||||
# getout = True
|
||||
# if getout:
|
||||
# break
|
||||
# print(m)
|
||||
|
||||
# It seems like the problem input is specifically designed so that reversing
|
||||
# the molecule back to 'e' works by simply replacing RHS with LHS continuously.
|
||||
# Intuitively, it seems like this should not work in many cases or at least not
|
||||
# result in the shortest sequence. Probably I am just salty that it took me so
|
||||
# long to find this trivial solution.
|
||||
x = molecule
|
||||
count = 0
|
||||
while len(x) > 1:
|
||||
old_len = len(x)
|
||||
for lhs, rhs in eqs:
|
||||
if rhs in x:
|
||||
x = x.replace(rhs, lhs, 1)
|
||||
count += 1
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
assert x == 'e'
|
||||
print(count)
|
||||
|
||||
40
2015/d23.py
Normal file
40
2015/d23.py
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
insts = []
|
||||
with open("i23.txt", "r") as f:
|
||||
for line in f:
|
||||
args = line.strip().replace(",", "").split()
|
||||
insts.append(args)
|
||||
|
||||
part_2 = True
|
||||
i = 0
|
||||
regs = {"a": 0, "b": 0}
|
||||
if part_2:
|
||||
regs["a"] = 1
|
||||
|
||||
while i < len(insts):
|
||||
inst = insts[i]
|
||||
match inst:
|
||||
case ["inc", reg]:
|
||||
regs[reg] += 1
|
||||
case ["hlf", reg]:
|
||||
regs[reg] //= 2
|
||||
case ["tpl", reg]:
|
||||
regs[reg] *= 3
|
||||
case ["jio", reg, offset]:
|
||||
if regs[reg] == 1:
|
||||
i += int(offset)
|
||||
continue
|
||||
case ["jie", reg, offset]:
|
||||
offset = int(offset)
|
||||
if regs[reg] % 2 == 0:
|
||||
i += int(offset)
|
||||
continue
|
||||
case ["jmp", offset]:
|
||||
i += int(offset)
|
||||
continue
|
||||
case _:
|
||||
print(inst)
|
||||
assert False
|
||||
i += 1
|
||||
|
||||
print(regs["b"])
|
||||
46
2015/d24.py
Normal file
46
2015/d24.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import sys
|
||||
from itertools import combinations
|
||||
|
||||
with open("i24.txt", "r") as f:
|
||||
weights = list(map(int, f.readlines()))
|
||||
|
||||
total_sum = sum(weights)
|
||||
|
||||
indices = list(range(len(weights)))
|
||||
|
||||
lowest = None
|
||||
def find(part_1=True):
|
||||
if part_1:
|
||||
target_weight = total_sum // 3
|
||||
else:
|
||||
target_weight = total_sum // 4
|
||||
for group_1_size in range(1, len(weights) - 1):
|
||||
for group_1_indices in combinations(indices, group_1_size):
|
||||
group_1_weight = sum(weights[i] for i in group_1_indices)
|
||||
if not group_1_weight == target_weight:
|
||||
continue
|
||||
remaining_indices = list(set(indices) - set(group_1_indices))
|
||||
for group_2_size in range(1, len(remaining_indices) - 1):
|
||||
for group_2_indices in combinations(remaining_indices, group_2_size):
|
||||
group_2_weight = sum(weights[i] for i in group_2_indices)
|
||||
if not group_2_weight == target_weight:
|
||||
continue
|
||||
if part_1 :
|
||||
r = 1
|
||||
for i in group_1_indices:
|
||||
r *= weights[i]
|
||||
return r
|
||||
else:
|
||||
remaining_indices = list(set(indices) - set(group_1_indices))
|
||||
for group_3_size in range(1, len(remaining_indices)- 1):
|
||||
for group_3_indices in combinations(remaining_indices, group_3_size):
|
||||
group_3_weight = sum(weights[i] for i in group_3_indices)
|
||||
if not group_3_weight == target_weight:
|
||||
continue
|
||||
r = 1
|
||||
for i in group_1_indices:
|
||||
r *= weights[i]
|
||||
return r
|
||||
|
||||
print(find(True))
|
||||
print(find(False))
|
||||
19
2015/d25.py
Normal file
19
2015/d25.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import sys
|
||||
from lib import str_to_ints
|
||||
|
||||
with open("i25.txt", "r") as f:
|
||||
target_row, target_col = str_to_ints(f.read())
|
||||
|
||||
x = 20151125
|
||||
m = 252533
|
||||
d = 33554393
|
||||
|
||||
for start_row in range(1, 10000):
|
||||
row, col = start_row, 1
|
||||
while row > 0:
|
||||
if row == target_row and col == target_col:
|
||||
print(x)
|
||||
sys.exit(0)
|
||||
x = (x * m) % d
|
||||
row -= 1
|
||||
col += 1
|
||||
Reference in New Issue
Block a user