summaryrefslogtreecommitdiffstats
path: root/10.py
blob: 27dbde94fbf94ab9d7785ae28e71a05e8860e977 (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
39
40
41
42
from dataclasses import dataclass

@dataclass
class Cycle:
    pass

@dataclass
class Change:
    diff: int

instrs = []
with open('10.in') as f:
    for line in f:
        match line.rstrip().split():
            case ['addx', n]:
                instrs.append(Cycle())
                instrs.append(Cycle())
                instrs.append(Change(int(n)))
            case ['noop']:
                instrs.append(Cycle())

cycle = 0
sigstr = 0
X = 1
screen = [0 for _ in range(240)]
for instr in instrs:
    match instr:
        case Cycle():
            cycle += 1
            if (cycle - 20) % 40 == 0:
                sigstr += cycle * X
            xpos = (cycle - 1) % 40
            if xpos - 1 <= X <= xpos + 1:
                screen[cycle - 1] = 1
        case Change(diff):
            X += diff
print(sigstr)
for y in range(6):
    line = []
    for x in range(40):
        line.append('#' if screen[x + 40 * y] else ' ')
    print(''.join(line))