aboutsummaryrefslogtreecommitdiff
path: root/2022/10/solve.py
blob: e9c7b6bf6a1c21f1d11b2c2d1e075b217af2cd94 (plain) (blame)
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
data = [dat.strip().split(" ") for dat in open("input").readlines() if dat != '']

def solve():
    signal_strength = 0

    crt = [["?" for j in range(40)] for i in range(6)]
    col = row = 0

    x = 1
    cycle = timeout = i = 0
    while i < len(data):
        instr = data[i]

        if cycle > 0 and cycle % 40 == 0:
            col = 0
            row += 1

        cycle += 1

        if cycle - 20 >= 0 and (cycle - 20) % 40 == 0:
            signal_strength += cycle * x

        crt[row][col] = "█" if col in [x-1,x,x+1] else " "
        col += 1

        if instr[0] == "noop":
            i += 1
        elif timeout > 0 and instr[0] == "addx":
            timeout -= 1
            if timeout == 0:
                x += int(instr[1])
                i += 1
        elif timeout == 0 and instr[0] == "addx":
            timeout = 1

    print(f"Part 1: {signal_strength}")
    print("\n".join(["".join(row) for row in crt]))

solve()