1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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)
|