-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
56 lines (52 loc) · 1.68 KB
/
day10.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
import numpy as np
class day10:
def __init__(self) -> None:
pass
def star1(self, input):
register = self.signal_strengths(input)
sum_of_signal_strength = 0
# print(register)
nb_of_values = (len(register)-20)//40+1
# print(nb_of_values)
for i in range(nb_of_values):
sum_of_signal_strength += register[20+i*40]*(20+i*40)
# print(register[20+i*40]*(20+i*40))
print(f"Sum of signal strengths are: {sum_of_signal_strength}")
return sum_of_signal_strength
def signal_strengths(self, input):
instructions = input.split("\n")
cycle = 1
x = 1
register = {}
for instruction in instructions:
if instruction[0] == "n":
register.update({cycle:x})
cycle += 1
else:
v = int(instruction.split(" ")[1])
for i in range(2):
register.update({cycle:x})
cycle += 1
x += v
return register
def star2(self, input):
register = self.signal_strengths(input)
# instructions = input.split("\n")
x = 1
CRT_Screen = ""
# print(register)
for i in range(len(register)):
x = register[i+1]
sprite = [x-1, x, x+1]
pixel = "#" if ((i%40) in sprite) else "."
CRT_Screen += pixel
if(i%40 == 39):
CRT_Screen += "\n"
print(CRT_Screen)
return CRT_Screen
if __name__ == '__main__':
with open("input/day10.star1") as f:
input = f.read()
d = day10()
d.star1(input)
d.star2(input)