Finish 2015.

This commit is contained in:
2024-05-08 18:29:48 -04:00
parent f0a6214052
commit 4e6a44f67b
5 changed files with 134 additions and 29 deletions

View File

@@ -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)