diff options
Diffstat (limited to 'day8.py')
-rw-r--r-- | day8.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/day8.py b/day8.py new file mode 100644 index 0000000..868aeb1 --- /dev/null +++ b/day8.py @@ -0,0 +1,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) \ No newline at end of file |