-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
41 lines (33 loc) · 1.08 KB
/
day8.py
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
import numpy as np
import re
def pixels(in_file):
screen = np.zeros((6, 50), dtype=bool)
def rect(a,b):
screen[:b,:a] = True
def rot_row(a, b):
row = screen[a, :].copy()
screen[a, b:] = row[:-b]
screen[a, :b] = row[-b:]
def rot_col(a, b):
col = screen[:, a].copy()
screen[b:, a] = col[:-b]
screen[:b, a] = col[-b:]
# read file
f = open(in_file, 'r')
for line in f:
line = line.replace('\n', '')
m = re.search('rect (\d+)x(\d+)', line)
if m is not None:
rect(int(m.group(1)), int(m.group(2)))
m = re.search('rotate row y=(\d+) by (\d+)', line)
if m is not None:
rot_row(int(m.group(1)), int(m.group(2)))
m = re.search('rotate column x=(\d+) by (\d+)', line)
if m is not None:
rot_col(int(m.group(1)), int(m.group(2)))
print("There are {} pixels lit.".format(sum(sum(screen))))
screen = np.int_(screen)
print(screen[:,:24], '\n')
print(screen[:,24:])
if __name__ == "__main__":
pixels('day8_input.txt')