summaryrefslogtreecommitdiffstats
path: root/solutions/py/d13.py
blob: 88b0fa25b5db12bf7a0b0adb295df9503f421881 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import intcode
import queue
import time

def pt1(input):
    c = intcode.Computer([int(x) for x in input[0].split(",")])
    buffer_out = []
    screen = {}
    while c.memory[c.pointer] != 99:
        c.step()
        if c.output is not None:
            buffer_out.append(c.output)
            c.output = None
        if len(buffer_out) == 3:
            screen[(buffer_out[0], buffer_out[1])] = buffer_out[2]
            buffer_out = []
    blocks = 0
    for p in screen:
        if screen[p] == 2:
            blocks += 1
    return blocks


def pt2(input):
    def draw(screen, points):
        ball_x = 0
        paddle_x = 0
        min_x = max_x = min_y = max_y = 0
        for p in screen:
            if p == (-1,0):
                points = screen[p]
            elif screen[p] == 3:
                paddle_x = p[0]
            elif screen[p] == 4:
                ball_x = p[0]
        return points, ball_x, paddle_x

    c = intcode.Computer([int(x) for x in input[0].split(",")])
    c.memory[0] = 2

    screen = {}
    buffer_out = []
    points = 0
    ball_x = paddle_x = 0

    while c.memory[c.pointer] != 99:
        if c.wants_input:
            points, ball_x, paddle_x = draw(screen, points)
            if paddle_x < ball_x:
                c.input = 1
            elif paddle_x > ball_x:
                c.input = -1
            else:
                c.input = 0
        c.step()
        if c.output is not None:
            buffer_out.append(c.output)
            c.output = None
        if len(buffer_out) == 3:
            screen[(buffer_out[0], buffer_out[1])] = buffer_out[2]
            buffer_out = []
    return c.memory[386]

def visualize(input):
    def draw(screen, points):
        ball_x = 0
        paddle_x = 0
        min_x = max_x = min_y = max_y = 0
        for p in screen:
            min_x = min(min_x, p[0])
            max_x = max(max_x, p[0])
            min_y = min(min_y, p[1])
            max_y = max(max_y, p[1])
        s = ""
        for y in range(min_y-1, max_y+1):
            for x in range(min_x-1, max_x+1):
                if (x,y) in screen:
                    if (x,y) == (-1,0):
                        points = screen[(x,y)]
                    elif screen[(x,y)] == 0:
                        s += " "
                    elif screen[(x,y)] == 1:
                        s += "\u2588"
                    elif screen[(x,y)] == 2:
                        s += "#"
                    elif screen[(x,y)] == 3:
                        s += "-"
                        paddle_x = x
                    elif screen[(x,y)] == 4:
                        s += "O"
                        ball_x = x
                else:  # (x,y) not in screen
                    s += " "
            s += "\n"
        return s, points, ball_x, paddle_x

    c = intcode.Computer([int(x) for x in input[0].split(",")])
    c.memory[0] = 2

    screen = {}
    buffer_out = []
    frame_n = 0
    points = 0
    ball_x = paddle_x = 0

    while c.memory[c.pointer] != 99:
        if c.wants_input:
            time.sleep(0.01)
            frame_n += 1
            frame, points, ball_x, paddle_x = draw(screen, points)
            print(frame)
            if paddle_x < ball_x:
                c.input = 1
            elif paddle_x > ball_x:
                c.input = -1
            else:
                c.input = 0
        c.step()
        if c.output is not None:
            buffer_out.append(c.output)
            c.output = None
        if len(buffer_out) == 3:
            screen[(buffer_out[0], buffer_out[1])] = buffer_out[2]
            buffer_out = []

if __name__ == "__main__":
    import cProfile

    _input = open("../input/13", "r").readlines()
    print(pt1(_input))
    print(pt2(_input))
    #visualize(_input)