Advent of Code 2021 Python Solution: Day 13
Tags: adventofcode, python, algorithms
Published: 2021-12-13 06:28:00

Today's challenge was fun to do and it was not that hard as well.

## Solution
```python
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:

```python
[[ ██  █  █ ███  ███  ███   ██  █  █ ████   ]
 [█  █ █  █ █  █ █  █ █  █ █  █ █  █    █   ]
 [█  █ ████ █  █ █  █ █  █ █  █ █  █   █    ]
 [████ █  █ ███  ███  ███  ████ █  █  █     ]
 [█  █ █  █ █    █ █  █    █  █ █  █ █      ]
 [█  █ █  █ █    █  █ █    █  █  ██  ████   ]
 [                                          ]
 [                                          ]]
```

I write blogs about Basic of Machine Learning and related stuffs, you can find it on [q-viper.github.io](https://q-viper.github.io).

## Why not read more?
* [Gesture Based Visually Writing System Using OpenCV and Python](https://q-viper.github.io/2020/08/01/gesture-based-visually-writing-system-using-opencv-and-python/)
* [Gesture Based Visually Writing System: Adding Visual User Interface](https://q-viper.github.io/2020/08/11/gesture-based-visually-writing-system-make-a-visual-user-interface/)
* [Gesture Based Visually Writing System: Adding Virtual Animationn, New Mode and New VUI](https://q-viper.github.io/2020/08/14/gesture-based-visually-writing-system-adding-virtual-animation-new-mode-and-new-vui/)
* [Gesture Based Visually Writing System: Add Slider, More Colors and Optimized OOP code](https://q-viper.github.io/2020/08/21/gesture-based-visually-writing-system-add-slider-more-colors-and-optimized-code/)
* [Gesture Based Visually Writing System: A Web App](https://q-viper.github.io/2020/08/29/gesture-based-visually-writing-system-web-app/)
* [Contour Based Game: Break The Bricks](https://q-viper.github.io/2020/08/16/contour-based-game-break-the-bricks/)
* [Linear Regression from Scratch](https://q-viper.github.io/2020/08/07/writing-a-linear-regression-class-from-scratch-using-python/)
* [Writing Popular ML Optimizers from Scratch](https://q-viper.github.io/2020/06/05/writing-popular-machine-learning-optimizers-from-scratch-on-python/)
* [Feed Forward Neural Network from Scratch](https://q-viper.github.io/2020/05/30/writing-a-deep-neural-network-from-scratch-on-python/)
* [Convolutional Neural Networks from Scratch](https://q-viper.github.io/2020/06/05/convolutional-neural-networks-from-scratch-on-python/)
* [Writing a Simple Image Processing Class from Scratch](https://q-viper.github.io/2020/05/30/image-processing-class-from-scratch-on-python/)
* [Deploying a RASA Chatbot on Android using Unity3d](https://q-viper.github.io/2020/08/04/deploying-a-simple-rasa-chatbot-on-unity3d-project-to-make-a-chatbot-for-android-devices/)
* [Naive Bayes for text classifications: Scratch to Framework](https://q-viper.github.io/2020/03/04/text-classification-using-naive-bayes-scratch-to-the-framework/)
* [Simple OCR for Devanagari Handwritten Text](https://q-viper.github.io/2020/02/25/building-ocr-for-devanagari-handwritten-character/)