-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoSystem.py
133 lines (101 loc) · 2.97 KB
/
VideoSystem.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python3
"""Cathode Ray Tube.py
Author: Bissallah Ekele - bAe
Date: 08/11/2023
Description: Fix/replace the comm's video system.
"""
def compute_signal_strength(count, register_x):
if count + 1 == 20:
return 20 * register_x
if count + 1 > 20 and ((count + 1) - 20) % 40 == 0:
return (count + 1) * register_x
return 0
def get_part_1(input_list):
"""Function description
Keyword argument:
input_list -- parameter description
Return:
returned value
Throws:
if exceptions are thrown
"""
count = 0
register_x = 1
signal_strength = 0
for instruction in input_list:
if instruction[0] == "n":
signal_strength += compute_signal_strength(count, register_x)
count += 1
if instruction[0] == "a":
cycle = 2
while(cycle):
signal_strength += compute_signal_strength(count, register_x)
count += 1
cycle -= 1
v = int(instruction.split(" ")[-1])
register_x += v
return signal_strength
def get_screen():
pixel_state = {}
for pixel in range(0, 240):
pixel_state[pixel] = "."
return pixel_state
def get_column_index(count):
while count > 39:
count -= 40
return count
def is_match(count, register_x):
sprite = [register_x - 1, register_x, register_x + 1]
if count in sprite:
return True
return False
def update_screen(count, register_x, screen):
column_index = get_column_index(count)
if is_match(column_index, register_x):
screen[count] = "#"
def display_row(screen, start, end):
row = []
for pixel in range(start, end):
row.append(screen[pixel])
display = []
display.append("".join(row))
print(display)
def display_screen(screen):
row_count = 6
start = 0
end = 40
while(row_count):
display_row(screen, start, end)
start += 40
end += 40
row_count -= 1
def get_part_2(input_list):
screen = get_screen()
count = 0
register_x = 1
for instruction in input_list:
if instruction[0] == "n":
update_screen(count, register_x, screen)
count += 1
if instruction[0] == "a":
cycle = 2
while(cycle):
update_screen(count, register_x, screen)
count += 1
cycle -= 1
v = int(instruction.split(" ")[-1])
register_x += v
display_screen(screen)
def main():
test_input = "test-input"
puzzle_input = "puzzle-input"
file_name = puzzle_input #test_input
with open(file_name) as f:
input_list = f.read().splitlines()
print("Part_1: "
+ str(get_part_1(input_list))
+ "\nPart_2: ")
get_part_2(input_list)
if __name__ == "__main__":
main()
# FZBPBFZF