Python Solution for 2021 Advent of Code - Day 13 - Transparent Origami

preview_player
Показать описание
Solution to both parts of 2021's Advent of Code's Day 13's problem. You might have different data to deal with. However, hopefully the code still works the same for you as it does for me.

Bradley Sward is currently an Associate Professor at the College of DuPage in suburban Chicago, Illinois. He has earned a Masters degree in Software Engineering from DePaul University, a Masters degree in Computer Science from the University of Illinois at Springfield, and two Bachelors degrees in Computer Science and Molecular Biology from Benedictine University. Prior to teaching, Bradley worked for five years in the field of casino gaming on a variety of video slot machine and poker games. The Village People have been permanently etched into his brain.
Рекомендации по теме
Комментарии
Автор

I used a numpy array for this problem. The advantage is that numpy has a fold method called flip (on either axis).

The array is a collection of 0s and 1s.

The algorithm is
- create a folded array on the axis specified.
- take the logical 'or' values of the original and folded arrays. (returns a Boolean which you can convert back to integer)
- trim the array at the fold point via a copy
- repeat with the next fold instruction

def fold_matrix (matrix, halfway, ax, ):

matrix_flipped = (np.flip(matrix, axis=ax))
matrix_ord = (np.logical_or(matrix, matrix_flipped))
new_matrix = matrix_ord.astype(int)
if not ax:
new_matrix = new_matrix [:halfway, :]
else:
new_matrix = new_matrix [:, :halfway]
return new_matrix, np.count_nonzero(new_matrix)

This takes advantage of features of the very rich numpy library.

billholst