-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
48 lines (37 loc) · 948 Bytes
/
script.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
inputs = (line.rstrip('\n') for line in open('input'))
cycles = {'addx': 2,
'noop': 1}
cycle = 0
instr_cycles = 0
X = 1
instr = 'noop'
operand = 0
sum_signals = 0
crt = []
for _ in range(0, 6):
crt.append(['.']*40)
def print_crt():
for line in crt:
print(''.join(map(str, line)))
while True:
if instr_cycles == 0:
if instr == 'addx':
X += int(operand)
try:
instr_op = next(inputs)
if instr_op.startswith('addx'):
instr, operand = instr_op.split(' ')
else:
instr = instr_op
except StopIteration:
break
instr_cycles = cycles[instr]
instr_cycles -= 1
cycle += 1
if (cycle - 20) % 40 == 0:
sum_signals += cycle * X
print(cycle, cycle * X, sum_signals)
x = (cycle-1) % 40
y = (cycle-1) // 40
crt[y][x] = '#' if X-1 <= x <= X+1 else '.'
print_crt()