-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.py
More file actions
63 lines (54 loc) · 1.55 KB
/
Day8.py
File metadata and controls
63 lines (54 loc) · 1.55 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
import time
with open('Day8.in', 'r') as f:
data = f.readlines()
unlit = " "
lit = "█"
mat = [[unlit for i in range(50)] for j in range(6)]
def swap(m, i1, j1, i2, j2):
m[i1][j1], m[i2][j2] = m[i2][j2], m[i1][j1]
def rect(m, x, y, val=1):
for i in range(0, y):
for j in range(0, x):
m[i][j] = val
def shift(m, x=0, column=True, dir=1):
i = x
size = len(m) if column else len(m[i])
if dir == 1:
for j in range(size-1, -1, -1):
if j > 0:
if column:
swap(m, j, i, j-1, i)
else:
swap(m, i, j, i, j-1)
else:
for j in range(0, size):
if j < size-1:
if column:
swap(m, j, i, j-1, i)
else:
swap(m, i, j, i, j+1)
def print_matrix(m):
print("\n" * 100)
for i in range(0, len(m)):
for j in range(0, len(m[i])):
print(mat[i][j], end="")
print()
for line in data:
command = line.split()
if command[0] == "rect":
params = command[1].split("x")
x = int(params[0])
y = int(params[1])
rect(mat, x, y, lit)
elif command[0] == "rotate":
amount = int(command[-1])
column = command[1] == "column"
index = int(command[2].split("=")[1])
for i in range(0, amount):
shift(mat, index, column, 1)
print_matrix(mat)
time.sleep(0.05)
c = [mat[i].count(lit) for i in range(len(mat))]
print_matrix(mat)
print("%d pixels lit up!" % sum(c))