-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.py
84 lines (68 loc) · 2.16 KB
/
Day6.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# from PIL import Image
import numpy
with open("Day6input.txt") as f:
data = f.readlines()
arr = numpy.zeros((1000, 1000))
def switch(op, corner1, corner2):
x1, y1 = corner1
x2, y2 = corner2
for x in range(x1, x2+1):
for y in range(y1, y2+1):
if op == 'on':
arr[x, y] = 1
elif op == 'off':
arr[x, y] = 0
else:
arr[x, y] = 1 - arr[x, y]
for s in data:
words = s.split()
if words[0] == "toggle":
x1y1 = words[1].split(',')
x2y2 = words[3].split(',')
switch('t', (int(x1y1[0]), int(x1y1[1])), (int(x2y2[0]), int(x2y2[1])))
elif words[1] == "on":
x1y1 = words[2].split(',')
x2y2 = words[4].split(',')
switch('on', (int(x1y1[0]), int(x1y1[1])), (int(x2y2[0]), int(x2y2[1])))
elif words[1] == "off":
x1y1 = words[2].split(',')
x2y2 = words[4].split(',')
switch('off', (int(x1y1[0]), int(x1y1[1])), (int(x2y2[0]), int(x2y2[1])))
else:
print("illegal command!")
print(arr.sum())
# img = Image.fromarray(arr*255)
# img.show()
# part 2
arr = numpy.zeros((1000, 1000))
def switch2(op, corner1, corner2):
x1, y1 = corner1
x2, y2 = corner2
for x in range(x1, x2+1):
for y in range(y1, y2+1):
if op == 'on':
arr[x, y] += 1
elif op == 'off':
if arr[x, y] > 0:
arr[x, y] -= 1
else:
arr[x, y] += 2
for s in data:
words = s.split()
if words[0] == "toggle":
x1y1 = words[1].split(',')
x2y2 = words[3].split(',')
switch2('t', (int(x1y1[0]), int(x1y1[1])), (int(x2y2[0]), int(x2y2[1])))
elif words[1] == "on":
x1y1 = words[2].split(',')
x2y2 = words[4].split(',')
switch2('on', (int(x1y1[0]), int(x1y1[1])), (int(x2y2[0]), int(x2y2[1])))
elif words[1] == "off":
x1y1 = words[2].split(',')
x2y2 = words[4].split(',')
switch2('off', (int(x1y1[0]), int(x1y1[1])), (int(x2y2[0]), int(x2y2[1])))
else:
print("illegal command!")
print(arr.sum())
# img = Image.fromarray(arr)
# img.show()