-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-10.py
72 lines (65 loc) · 2.12 KB
/
day-10.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
from common import parse
def process_signal(filename):
inputs = parse(filename)
x = 1
result = 0
cycles = 1
next_cycle = 20
crt_lines = []
crt = ""
for i in range(len(inputs)):
input = inputs[i]
if input == "noop":
crt += "#" if x <= cycles % 40 <= x+2 else "." # cycle-1
print(
f"Pixel is at {x}. {''.join([('#' if x-1 <= j <= x+1 else '.') for j in range(40)])}\nDrawing {cycles-1} => {crt[-1]}")
print(crt)
if len(crt) == 40:
crt_lines.append(crt)
crt = ""
cycles += 1
if (cycles >= next_cycle):
result += x * next_cycle
next_cycle += 40
continue
# Else => begin adding X
_, value = input.split(" ")
# During cycle 1
crt += "#" if x <= cycles % 40 <= x+2 else "." # cycle-1
print(
f"Pixel is at {x}. {''.join([('#' if x-1 <= j <= x+1 else '.') for j in range(40)])}\nDrawing {cycles-1} => {crt[-1]}")
print(crt)
if len(crt) == 40:
crt_lines.append(crt)
# print(crt)
crt = ""
cycles += 1
if (cycles >= next_cycle):
result += x * next_cycle
next_cycle += 40
# During cycle 2
crt += "#" if x <= cycles % 40 <= x+2 else "." # cycle-1
print(
f"Pixel is at {x}. {''.join([('#' if x-1 <= j <= x+1 else '.') for j in range(40)])}\nDrawing {cycles-1} => {crt[-1]}")
print(crt)
if len(crt) == 40:
crt_lines.append(crt)
# print(crt)
crt = ""
cycles += 1
# Add X
x += int(value)
if (cycles >= next_cycle):
result += x * next_cycle
next_cycle += 40
print(''.join(['-' for i in range(40)]))
for crt in crt_lines:
print(crt)
print(f"Final X={x}, result={result}")
return x, result
# x, result = process_signal("10-1")
# assert(x == -1)
# assert(result == 0)
x, result = process_signal("10-2")
assert(result == 13140)
x, result = process_signal("10-3")