summaryrefslogtreecommitdiffstats
path: root/19/py/d15.py
blob: b2a9b109e741b69c05b76e749113560fb8c9db37 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import intcode
import heapq as heap
import collections
import time

def neighbours(p):
    return [(p[0]+1, p[1]), (p[0]-1, p[1]), \
            (p[0], p[1]+1), (p[0], p[1]-1)]

def get_path(start, end, board, draw_search=False):
    if start == end:
        return collections.deque()
    visited = set()
    h = []
    heap.heappush(h, (0, start, collections.deque()))
    while True:
        cur = heap.heappop(h)
        for n in neighbours(cur[1]):
            if n == end:
                cur[2].append(n)
                if draw_search:
                    print(draw(board, path=cur[2], visited=visited))
                return cur[2]
            if n in visited or n not in board or board[n] == "#":
                continue
            new_path = collections.deque(cur[2])
            new_path.append(n)
            visited.add(n)
            heap.heappush(h, (cur[0] + 1, n, new_path))
            if draw_search:
                time.sleep(0.005)
                print(draw(board, path=new_path, visited=visited))

def pt1(input):
    cur_x = cur_y = 0
    q = collections.deque()
    q.append((0,0))
    path = collections.deque()
    board = {}
    board[(0,0)] = "."

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

    while not c.SIG_HALT:
        if len(q) == 0:
            break
        c.step()
        if c.SIG_INPUT:
            direction = 0
            prev_x, prev_y = cur_x, cur_y
            if len(path) == 0:
                # find new path
                for n in neighbours((cur_x, cur_y)):
                    if n not in q and n not in board:
                        q.append(n)
                if (cur_x, cur_y) in q:
                    q.remove((cur_x, cur_y))
                next = q.pop()
                path = get_path((cur_x, cur_y), next, board)
            next_step = path.popleft()
            if next_step[1] == cur_y-1:
                direction = 1
                cur_y -= 1
            elif next_step[1] == cur_y+1:
                direction = 2
                cur_y += 1
            elif next_step[0] == cur_x-1:
                direction = 3
                cur_x -= 1
            elif next_step[0] == cur_x+1:
                direction = 4
                cur_x += 1
            else:
                print("invalid path")
                break
            c.input = direction
            c.SIG_INPUT = False
        if c.SIG_OUTPUT:
            if c.output == 0:
                board[(cur_x, cur_y)] = "#"
                cur_x, cur_y = prev_x, prev_y
            elif c.output == 1:
                board[(cur_x, cur_y)] = "."
            elif c.output == 2:
                board[(cur_x, cur_y)] = "S"
                oxygen = (cur_x, cur_y)
            else:
                break
            c.output = None
            c.SIG_OUTPUT = False
    return len(get_path((0,0), oxygen, board))

def pt2(input):
    cur_x = cur_y = 0
    q = collections.deque()
    q.append((0,0))
    path = collections.deque()
    board = {}
    board[(0,0)] = "."
    oxygen = (0,0)

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

    while not c.SIG_HALT:
        if len(q) == 0:
            break
        c.step()
        if c.SIG_INPUT:
            direction = 0
            prev_x, prev_y = cur_x, cur_y
            if len(path) == 0:
                # find new path
                for n in neighbours((cur_x, cur_y)):
                    if n not in q and n not in board:
                        q.append(n)
                if (cur_x, cur_y) in q:
                    q.remove((cur_x, cur_y))
                next = q.pop()
                path = get_path((cur_x, cur_y), next, board)
            next_step = path.popleft()
            if next_step[1] == cur_y-1:
                direction = 1
                cur_y -= 1
            elif next_step[1] == cur_y+1:
                direction = 2
                cur_y += 1
            elif next_step[0] == cur_x-1:
                direction = 3
                cur_x -= 1
            elif next_step[0] == cur_x+1:
                direction = 4
                cur_x += 1
            else:
                print("invalid path")
                break
            c.input = direction
        if c.SIG_OUTPUT:
            if c.output == 0:
                board[(cur_x, cur_y)] = "#"
                cur_x, cur_y = prev_x, prev_y
            elif c.output == 1:
                board[(cur_x, cur_y)] = "."
            elif c.output == 2:
                board[(cur_x, cur_y)] = "S"
                oxygen = (cur_x, cur_y)
            else:
                break
            c.output = None

    steps = 0
    visited = set(oxygen)
    cur_layer = []
    next_layer = [oxygen]
    while True:
        cur_layer = next_layer.copy()
        next_layer = []
        for p in cur_layer:
            for n in neighbours(p):
                if n in board and board[n] != "#" and n not in visited:
                    visited.add(n)
                    next_layer.append(n)
        if len(next_layer) == 0:
            break
        steps += 1
    return steps

def draw(board, droid=None, path=None, visited=None):
    min_x=max_x=min_y=max_y = 0
    for p in board:
        min_x = min(p[0], min_x)
        max_x = max(p[0], max_x)
        min_y = min(p[1], min_y)
        max_y = max(p[1], max_y)
    s = ""
    for y in range(min_y-1, max_y+2):
        s += "\n"
        for x in range(min_x-1, max_x+2):
            point = (x, y)
            if droid is not None and point == droid:
                s += "D" * 2
            elif path is not None and point in path:
                s += "\u2591" * 2
            elif visited is not None and point in visited:
                s += "." * 2
            elif point in board:
                if board[point] == "#":
                    s += "\u2588" * 2
                elif board[point] == "S":
                    s += "\u2591" * 2
                else:
                    s += " " * 2
            else:
                s += "." * 2
    return s

def visualize(input):
    cur_x = cur_y = 0
    q = collections.deque()
    q.append((0,0))
    path = collections.deque()
    board = {}
    board[(0,0)] = "."

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

    while not c.SIG_HALT:
        if len(q) == 0:
            break
        c.step()
        if c.SIG_INPUT:
            direction = 0
            prev_x, prev_y = cur_x, cur_y
            if len(path) == 0:
                # find new path
                for n in neighbours((cur_x, cur_y)):
                    if n not in q and n not in board:
                        q.append(n)
                if (cur_x, cur_y) in q:
                    q.remove((cur_x, cur_y))
                next = q.pop()
                path = get_path((cur_x, cur_y), next, board)
            next_step = path.popleft()
            if next_step[1] == cur_y-1:
                direction = 1
                cur_y -= 1
            elif next_step[1] == cur_y+1:
                direction = 2
                cur_y += 1
            elif next_step[0] == cur_x-1:
                direction = 3
                cur_x -= 1
            elif next_step[0] == cur_x+1:
                direction = 4
                cur_x += 1
            else:
                print("invalid path")
                break
            c.input = direction
        if c.SIG_OUTPUT:
            if c.output == 0:
                board[(cur_x, cur_y)] = "#"
                cur_x, cur_y = prev_x, prev_y
            elif c.output == 1:
                board[(cur_x, cur_y)] = "."
            elif c.output == 2:
                board[(cur_x, cur_y)] = "S"
                oxygen = (cur_x, cur_y)
            else:
                break
            time.sleep(0.005)
            print(draw(board, droid=(cur_x, cur_y)))
            c.output = None

    get_path((0,0), oxygen, board, draw_search=True)

    steps = 0
    visited = set(oxygen)
    cur_layer = []
    next_layer = [oxygen]
    while True:
        cur_layer = next_layer.copy()
        next_layer = []
        for p in cur_layer:
            for n in neighbours(p):
                if n in board and board[n] != "#" and n not in visited:
                    visited.add(n)
                    next_layer.append(n)
        if len(next_layer) == 0:
            break
        steps += 1

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