my advent of code 2021 solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

3 years ago
from io import DEFAULT_BUFFER_SIZE
import sys
poly = None
inserters = {}
class polyme:
def __init__(self, initial):
self.Polymere = []
self.letters = {}
self.dubles = {}
for f in initial:
self.Polymere.append(f)
for i in range(len(self.Polymere)):
if i != len(self.Polymere) - 1:
temp = self.Polymere[i] + self.Polymere[i + 1]
g = self.dubles.setdefault(temp, 0)
g += 1
self.dubles.update({temp : g})
temp2 = self.letters.setdefault(self.Polymere[i], 0)
temp2 += 1
self.letters.update({self.Polymere[i] : temp2})
print(self.letters, self.dubles, self.Polymere)
def steppy(self):
tempdict = {}
tempdict.clear()
print(tempdict)
for key, insert in inserters.items():
if key in self.dubles:
count = self.dubles[key]
if insert in self.letters:
adletters = self.letters[insert]
else:
adletters = 0
adletters += count
self.letters.update({insert : adletters})
dub1 = key[0] + insert
dub2 = insert + key[1]
g = tempdict.setdefault(dub1, 0)
g += count
tempdict.update({dub1 : g})
g = tempdict.setdefault(dub2, 0)
g += count
tempdict.update({dub2 : g})
g = tempdict.setdefault(key, 0)
g -= count
tempdict.update({key : g})
for dub, amount in tempdict.items():
g = self.dubles.setdefault(dub, 0)
g += amount
self.dubles.update({dub : g})
print(self.letters, self.dubles, self.Polymere)
def findsolu(self):
temp = self.letters.values()
print(temp)
temp2 = max(temp) - min(temp)
return temp2
with open(sys.argv[1], 'r') as f:
for g in f.readlines():
g = g.strip()
if poly is None:
poly = polyme(g)
continue
if g == '':
continue
else:
temp1, temp2 = g.split(' -> ')
inserters.update({temp1:temp2})
for i in range(40):
poly.steppy()
print(poly.findsolu())