adventofcode2022/day10/sol.py

40 lines
826 B
Python

import itertools
import time
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])
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
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)
xs = list(run(open("input")))
print(signal(xs))
img = render(xs)
for line in img:
print(line)
time.sleep(.1) # :)