Advent of Code 2021 Python Solution: Day 13

qviper

Viper

Posted on December 13, 2021

Advent of Code 2021 Python Solution: Day 13

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]}))
Enter fullscreen mode Exit fullscreen mode

My answer was:

[[ ██     ███  ███  ███   ██     ████   ]
 [                           ]
 [   ████                      ]
 [████    ███  ███  ███  ████          ]
 [                             ]
 [                     ██  ████   ]
 [                                          ]
 [                                          ]]
Enter fullscreen mode Exit fullscreen mode

I write blogs about Basic of Machine Learning and related stuffs, you can find it on q-viper.github.io.

Why not read more?

💖 💪 🙅 🚩
qviper
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.

Related

Advent of Code 2021 Python Solution: Day 16
adventofcode Advent of Code 2021 Python Solution: Day 16

December 17, 2021

Advent of Code 2021 Python Solution: Day 15
adventofcode Advent of Code 2021 Python Solution: Day 15

December 15, 2021

Advent of Code 2021 Python Solution: Day 14
adventofcode Advent of Code 2021 Python Solution: Day 14

December 14, 2021

Advent of Code 2021 Python Solution: Day 13
adventofcode Advent of Code 2021 Python Solution: Day 13

December 13, 2021

Advent of Code 2021 Python Solution: Day 12
adventofcode Advent of Code 2021 Python Solution: Day 12

December 12, 2021