summaryrefslogtreecommitdiffstats
path: root/19/py/d21.py
blob: 84f01ff0b7cedde185c5f832c3f7049237753211 (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
import intcode

def ascii_draw(a):
    s = ""
    for c in a:
        if c == 10:
            s += "\n"
        elif c < 128:
            s += chr(c)
        else:
            print("[INVALID ASCII]", c)
    return s

def do(c, jumpscript):
    output = []
    for line in jumpscript:
        if line[:2] != "//":
            c.queue_ascii(line.strip().upper())
    while not c.SIG_HALT:
        c.step()
        if c.SIG_OUTPUT:
            output.append(c.output)
            c.output = None
    return ascii_draw(output)

def pt1(input):
    c = intcode.Computer([int(x) for x in input[0].split(",")], ascii=True)
    return do(c, open("21-1.js", "r").readlines())

def pt2(input):
    c = intcode.Computer([int(x) for x in input[0].split(",")], ascii=True)
    return do(c, open("21-2.js", "r").readlines())

if __name__ == "__main__":
    input = open("../input/21", "r").readlines()
    print(pt1(input))
    print(pt2(input))