Advent of Code 2021 Python Solution: Day 14
Viper
Posted on December 14, 2021
They know we could fall into a trap. And again I fell. I went full looping mode and got the result of part 1 but the part 2 could take days.
Part 1
from collections import Counter
data,data1 = get_data(day=14)
wdata=data1.copy()
polymer = wdata[0]
rule = {v[0]:v[1] for v in [d.split(" -> ") for d in wdata[2:]]}
curr_polymer = polymer
i=0
while i< 10:
tpoly = curr_polymer
# print(i, tpoly)
ind = 0
added = 0
for k, c in enumerate(tpoly):
k+=1
ch = curr_polymer[k-1:k+1]
mc = rule.get(ch)
if mc:
tpoly = [c for c in tpoly]
tpoly.insert(k+added, mc)
tpoly = "".join(tpoly)
added+=1
curr_polymer = tpoly
i+=1
res = dict(Counter(curr_polymer))
res = sorted(res.items(), key=lambda x: x[1], reverse=True)
res[0][1]-res[-1][1]
Part 2
Taken hint from here.
tmp_poly = Counter(a+b for a,b in zip(polymer, polymer[1:]))
print(tmp_poly)
chars = Counter(polymer)
for _ in range(40):
tmp = Counter()
for (c1,c2),value in tmp_poly.items():
mc = rule[c1+c2]
tmp[c1+mc] += value
tmp[mc+c2] += value
chars[mc] += value
tmp_poly=tmp
max(chars.values()) - min(chars.values())
Why not read more?
- Gesture Based Visually Writing System Using OpenCV and Python
- Gesture Based Visually Writing System: Adding Visual User Interface
- Gesture Based Visually Writing System: Adding Virtual Animationn, New Mode and New VUI
- Gesture Based Visually Writing System: Add Slider, More Colors and Optimized OOP code
- Gesture Based Visually Writing System: A Web App
- Contour Based Game: Break The Bricks
- Linear Regression from Scratch
- Writing Popular ML Optimizers from Scratch
- Feed Forward Neural Network from Scratch
- Convolutional Neural Networks from Scratch
- Writing a Simple Image Processing Class from Scratch
- Deploying a RASA Chatbot on Android using Unity3d
- Naive Bayes for text classifications: Scratch to Framework
- Simple OCR for Devanagari Handwritten Text
💖 💪 🙅 🚩
Viper
Posted on December 14, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.