2022-12-10 05:53:36 +00:00
|
|
|
import itertools
|
|
|
|
import time
|
2022-12-10 05:22:36 +00:00
|
|
|
|
2022-12-10 05:53:36 +00:00
|
|
|
def run(input):
|
|
|
|
x = 1
|
|
|
|
for line in input:
|
|
|
|
ins = line.strip()
|
|
|
|
if ins == "noop":
|
|
|
|
yield x
|
|
|
|
else: # addx <num>
|
|
|
|
yield x
|
|
|
|
yield x
|
|
|
|
x += int(line.split()[1])
|
2022-12-10 05:22:36 +00:00
|
|
|
|
2022-12-10 05:53:36 +00:00
|
|
|
def signal(xs):
|
|
|
|
t = 0
|
|
|
|
for cycle, x in enumerate(xs, start=1):
|
|
|
|
if cycle % 40 == 20:
|
|
|
|
signal_strength = cycle * x
|
|
|
|
t += signal_strength
|
|
|
|
return t
|
2022-12-10 05:22:36 +00:00
|
|
|
|
2022-12-10 05:53:36 +00:00
|
|
|
def render(xs):
|
|
|
|
xs = iter(xs)
|
|
|
|
while True:
|
|
|
|
img = []
|
|
|
|
for pos, x in enumerate(itertools.islice(xs, 40)):
|
|
|
|
visible = x-1 <= pos <= x+1
|
|
|
|
img.append(' #'[visible])
|
|
|
|
if not img: return
|
|
|
|
yield ''.join(img)
|
2022-12-10 05:22:36 +00:00
|
|
|
|
2022-12-10 05:53:36 +00:00
|
|
|
xs = list(run(open("input")))
|
|
|
|
print(signal(xs))
|
2022-12-10 05:22:36 +00:00
|
|
|
|
2022-12-10 05:53:36 +00:00
|
|
|
img = render(xs)
|
|
|
|
for line in img:
|
|
|
|
print(line)
|
|
|
|
time.sleep(.1) # :)
|