summaryrefslogtreecommitdiffstats
path: root/10.py
blob: e983650278db4a8a5b306c6268825695f9b846ec (plain)
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
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='')