[WIP] Solution to problem 8 part 1 in Python

This commit is contained in:
David Doblas Jiménez 2022-04-02 20:29:59 +02:00
parent 242e558677
commit 8ba6b57f2c

53
src/Year_2016/P8.py Normal file
View File

@ -0,0 +1,53 @@
import numpy as np
with open("files/test") as f:
instructions = [line for line in f.read().strip().split("\n")]
print(instructions)
# grid = np.zeros((6,50), dtype=int)
init_grid = np.zeros((3, 7), dtype=int)
# initialize grid
print(init_grid)
def rect(instruction, grid):
col, row = int(instruction[5:6]), int(instruction[7:])
# print(col,row)
grid[:row, :col] = 1
rect(instructions[0], init_grid)
print(init_grid)
def rotate_column(instruction, grid):
x, steps = int(instruction[16:17]), int(instruction[21:])
# print(x, steps)
new_col = np.roll(grid[:, x : x + 1], steps, axis=0)
grid[:, x : x + 1] = new_col
rotate_column(instructions[1], init_grid)
print(init_grid)
def rotate_row(instruction, grid):
y, steps = int(instruction[13:14]), int(instruction[18:])
# print(y, steps)
new_row = np.roll(grid[y : y + 1, :], steps, axis=1)
grid[y : y + 1, :] = new_row
rotate_row(instructions[2], init_grid)
print(init_grid)
rotate_column(instructions[3], init_grid)
print(init_grid)
print(np.sum(init_grid))