import numpy as np
import re
def parse_line(line: str):
'''
rect/rotate = 0/1
row/col = 0/1
dims = int/int
'''
splitline = line.strip().split()
if splitline[0] == 'rect':
col, row = map(int, splitline[-1].split('x'))
return (0, -1, col, row)
else:
dim, offset = map(int, re.findall(r'\d+', line))
if splitline[1] == 'row':
return (1, 0, dim, offset)
else:
return (1, 1, dim, offset)
with open('day8.txt') as data:
insts = [parse_line(line) for line in data]
def part1(instructions):
grid = np.zeros((6, 50), dtype=bool)
for inst in instructions:
match inst:
case 0, _, col, row:
grid[0:row, 0:col] = True
case 1, axis, dim, offset:
if axis == 0:
grid[dim] = np.roll(grid[dim], offset)
else:
grid[:, dim] = np.roll(grid[:, dim], offset)
return grid
def part2(instructions):
grid = part1(instructions)
printable = np.zeros_like(grid, dtype=str)
printable[grid] = "*"
printable[~grid] = " "
for line in printable:
print("".join(char for char in line))
print(np.count_nonzero(part1(insts)))
part2(insts)