Advent-of-Code/2021/14/main2.py

46 lines
1.0 KiB
Python
Raw Permalink Normal View History

2022-12-01 09:14:56 -05:00
str = ''
data = {}
letters = []
with open('data', 'r') as fp:
for line in fp:
if(' -> ' in line):
pair = line.strip().split(' -> ')
data[pair[0]] = pair[1]
elif(len(line) > 1):
str = line.strip()
for ch in str:
if(not ch in letters):
letters.append(ch)
letters.sort()
# # DEBUG(dev):
# print(data)
# print(str)
# print(letters)
for step in range(0, 40):
new_str = ''
for i in range(0, len(str) - 1):
lookup = str[i:i+2]
if(lookup in data.keys()):
new_str += str[i] + data[lookup]
else:
new_str += str[i]
new_str += str[-1]
str = new_str
print('Step ', step+1, ', length', len(str))
# NOTE(dev): un-indent this (take it out of the for loop) for just a final answer
counts = {}
for ch in str:
if(ch in counts):
counts[ch] += 1
else:
counts[ch] = 1
counts_list = list(counts.values())
counts_list.sort()
print(counts_list, counts_list[-1] - counts_list[0])