Advent of Code 2021 Python Solution: Day 13
Viper
Posted on December 13, 2021
Today's challenge was fun to do and it was not that hard as well.
Solution
import numpy as np
data1, data = get_data(day=13)
dots = [list(map(int, f.split(","))) for f in data[:data.index("")]]
folds = data[data.index("")+1:]
folds = [f.split("along ")[1].split("=") for f in folds]
folds = [(f[0], int(f[1])) for f in folds]
dots = np.array(dots)
window = np.zeros(dots.max(axis=0)+3)
for c in dots:
window[c[0], c[1]] = 1
window = window.T
tw = window.copy()
# print(tw)
for f in folds:
print(f)
axis,value=f
cr,cc = tw.shape
print(tw)
if axis=="y":
#fold y axis
chunk = tw[value+1:-2]
# print(value-crs,chunk.shape, chunk)
crs,ccs = chunk.shape
tw[np.abs(value-crs):value] += chunk[::-1]
tw = tw[:value]
tw = np.append(tw, np.zeros((2, tw.shape[1])), axis=0)
#break
else:
# fold x axis
chunk = tw[:, value+1:-2]
crs,ccs = chunk.shape
print(value-crs,chunk.shape, chunk)
tw[:, abs(value-ccs):value] += chunk[:,::-1]
tw = tw[:, :value]
tw = np.append(tw, np.zeros((tw.shape[0], 2)), axis=1)
print(f"Dots: {np.sum(tw>0)}")
print(np.array2string(tw>0, separator='',
formatter = {'bool':lambda x: ' █'[x]}))
My answer was:
[[ ██ █ █ ███ ███ ███ ██ █ █ ████ ]
[█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ]
[█ █ ████ █ █ █ █ █ █ █ █ █ █ █ ]
[████ █ █ ███ ███ ███ ████ █ █ █ ]
[█ █ █ █ █ █ █ █ █ █ █ █ █ ]
[█ █ █ █ █ █ █ █ █ █ ██ ████ ]
[ ]
[ ]]
I write blogs about Basic of Machine Learning and related stuffs, you can find it on q-viper.github.io.
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 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.