from dataclasses import dataclass @dataclass class Cycle: pass @dataclass class Change: diff: int def instr_gen(f): for line in f: match line.rstrip().split(): case ['addx', n]: yield Cycle() yield Cycle() yield Change(int(n)) case ['noop']: yield Cycle() with open('10.in') as f: cycle = 0 signal_strength = 0 X = 1 screen = [] for instr in instr_gen(f): match instr: case Cycle(): cycle += 1 if (cycle - 20) % 40 == 0: signal_strength += cycle * X xpos = (cycle - 1) % 40 screen.append('#' if xpos - 1 <= X <= xpos + 1 else ' ') if xpos == 39: screen.append('\n') case Change(diff): X += diff print(signal_strength) print(''.join(screen), end='')