Solve 2015 days 11-18.
This commit is contained in:
41
2015/d19.py
Normal file
41
2015/d19.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import sys
|
||||
from random import shuffle
|
||||
|
||||
data = open(0).read().strip()
|
||||
|
||||
eqs = []
|
||||
text = ""
|
||||
for line in data.splitlines():
|
||||
if "=>" in line:
|
||||
lhs, rhs = line.split(" => ")
|
||||
eqs.append((lhs, rhs))
|
||||
elif line:
|
||||
text = line
|
||||
|
||||
molecules = set()
|
||||
for lhs, rhs in eqs:
|
||||
for i in range(len(text)):
|
||||
f, t = text[:i], text[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)
|
||||
Reference in New Issue
Block a user