Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template = ''
- pairRules = {}
- def getPuzzleData():
- template = ''
- pairRules = {}
- with open('AOC2021D14.txt') as INFILE:
- lines = INFILE.read().split('\n')
- template = lines[0]
- for line in range(2, len(lines)):
- lineElems = lines[line].split(' -> ')
- pairRules[lineElems[0]] = lineElems[1]
- return template, pairRules
- def doStep(aTemplate, aPairRules):
- lTemplate = ''
- lOldPairs = []
- lNewPairs = []
- for pairStartPos in range(len(aTemplate) - 1):
- lOldPairs.append(aTemplate[pairStartPos:pairStartPos+2])
- for lOldPair in lOldPairs:
- if lOldPair in aPairRules:
- lNewPairs.append(lOldPair[0] + aPairRules[lOldPair] + lOldPair[1])
- lTemplate = lNewPairs[0]
- for lNewPairIdx in range(1, len(lNewPairs)):
- if lTemplate[-1] == lNewPairs[lNewPairIdx][0]:
- lTemplate += lNewPairs[lNewPairIdx][1:]
- else:
- lTemplate += lNewPairs[lNewPairIdx]
- return lTemplate
- def getElementQuantities(aTemplate):
- lElementQuantitites = {}
- for element in aTemplate:
- lElementQuantitites.setdefault(element, 0)
- lElementQuantitites[element] += 1
- lElementQuantitites = sorted(lElementQuantitites.items(), key=lambda item: item[1])
- return lElementQuantitites
- #
- # Main program
- #
- STEPS=10
- template, pairRules = getPuzzleData()
- print(f'{template=}')
- print(f'{pairRules=}')
- for step, _ in enumerate(range(STEPS), start=1):
- template = doStep(template, pairRules)
- print(f'After {step=}: {len(template)=}')
- elementQuantities = getElementQuantities(template)
- print(f'{elementQuantities}')
- print(f'{elementQuantities[0][1]}')
- print(f'{elementQuantities[-1][1] - elementQuantities[0][1]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement