Advent of Code 2021 Python Solution: Day 2
Viper
Posted on December 4, 2021
Again, I will be using my helper function created on day 1 and the input data will be in same format.
Part 1
hs = 0
vs = 0
for d in data:
k,v = d.split()
if k =="forward":
hs+=int(v)
elif k == "down":
vs+=int(v)
elif k=="up":
vs-=int(v)
print(hs*vs)
Answer of test data will be 150 and of real input will be 1947824, we just have to give data1 instead of data in loop.
Part 2
hs = 0
vs = 0
aim = 0
for d in data1:
k,v = d.split()
v = int(v)
if k =="forward":
hs+=v
vs+=aim*v
elif k == "down":
aim+=v
# vs+=v
elif k=="up":
aim-=v
# vs-=v
print(hs*vs)
The output of above code will be 1813062561.
All of my codes are available in GitHub as Jupyter Notebook.
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 4, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.