summaryrefslogtreecommitdiffstats
path: root/19/py/d18.py
blob: 60cbe465a7f346dbf2126a76ca2843983f7741fc (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
277
278
279
280
281
282
283
284
import collections
import heapq as heap
import sys

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 draw(map, pos=None, keys={}, doors={}, path={}, visited={}, dead=set()) -> str:
    min_x=max_x=min_y=max_y = 0
    for p in map:
        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 pos is not None and point == pos:
                s += "@"
            elif point in keys:
                s += keys[point]
            elif point in doors:
                s += doors[point]
            elif point in path:
                s += "\u2591"
            elif point in visited:
                s += "."
            elif point in dead:
                s += "X"
            elif point in map:
                if map[point] == "#":
                    s += "\u2588"
                else:
                    s += " "
            else:
                s += "."
    return s

def avail(map, start, items, dead, ignore=set()):
    visited = set()
    avail = []
    q = collections.deque()
    q.append((0, start))
    while len(q) > 0:
        cur = q.popleft()
        dist = cur[0]
        pos = cur[1]
        if pos in items and pos != start:
            avail.append((dist, pos, items[pos]))
            continue
        for n in neighbours(pos):
            if (n in map and map[n] == "#") or n in dead or n in visited or n in ignore:
                continue
            visited.add(n)
            q.append((dist+1, n))
    return avail

def avail_keys(map, keys, doors, graph, start, keys_left, unlocked_doors):
    visited = set()
    avail = []
    h = []
    heap.heappush(h, (0, str(start)))
    while len(h) > 0:
        dist, cur_key = heap.heappop(h)
        if cur_key != start and cur_key.islower() and cur_key in keys_left:
            avail.append((dist, cur_key))
            continue
        for n in graph[cur_key]:
            if n[0].isupper() and n[0] not in unlocked_doors:
                continue
            if n[0] in visited:
                continue
            visited.add(n[0])
            heap.heappush(h, (dist + n[1], n[0]))
    return avail

def pt1(input):
    map = {}
    map_keys = {}
    map_doors = {}
    map_keys_doors = {}
    walls = set()
    tiles = set()

    for y in range(len(input)):
        for x in range(len(input[y].strip())):
            c = input[y][x]
            p = (x,y)
            if c == "@":
                start = p
                map[p] = "."
            elif c.isalpha():
                if c.isupper():
                    map_doors[p] = c
                else:
                    map_keys[p] = c
                map[p] = "."
            elif c == "#":
                walls.add(p)
                map[p] = c
            elif c == ".":
                tiles.add(p)
                map[p] = c
    map_keys_doors.update(map_keys)
    map_keys_doors.update(map_doors)

    dead = set()
    for _ in range(10):
        #TODO repeat until no further changes are done
        ends = set()
        intersections = set()
        for tile in tiles:
            num_walls = 0
            for n in neighbours(tile):
                if n in walls or n in dead:
                    num_walls += 1
            if num_walls == 3:
                ends.add(tile)
            elif num_walls < 2:
                intersections.add(tile)

        for end in ends:
            cur = end
            while cur not in intersections and cur not in map_keys and cur not in map_doors:
                dead.add(cur)
                if cur in tiles:
                    tiles.remove(cur)
                for n in neighbours(cur):
                    if n not in walls and n not in dead:
                        cur = n
                        break

    paths = {}  # every path available between a door and a key
    start_paths = {}  # every path available from the starting point

    graph = {}  # {node: [children as tuples with name and distance]}
    for ent, s in map_keys_doors.items():
        for a in avail(map, ent, map_keys_doors, dead):
            paths[(s, a[2])] = a[0]
            graph[s] = graph.get(s, []) + [(a[2], a[0])]
    for a in avail(map, start, map_keys, dead, map_doors):
        start_paths[("@", map_keys[a[1]])] = a[0]

    h = []
    for path, dist in start_paths.items():
        cur = path[1]
        keys_left = set(map_keys.values()).copy()
        keys_left.remove(cur)
        unlocked = set()
        unlocked.add(cur.upper())
        key_path = [cur]
        heap.heappush(h, (dist, cur, keys_left, unlocked, key_path))

    best = len(map_keys)
    seen = set()

    while True:
        dist, key, keys_left, unlocked, key_path = heap.heappop(h)
        if (frozenset(key_path), key) in seen:
            continue
        seen.add((frozenset(key_path), key))
        if len(keys_left) == 0:
            return dist, key_path
        if len(keys_left) < best:
            best = len(keys_left)
        for new_dist, new_key in avail_keys(map, map_keys, map_doors, graph, key, keys_left, unlocked):
            new_keys_left = keys_left.copy()
            new_keys_left.remove(new_key)
            new_unlocked = unlocked.copy()
            new_unlocked.add(new_key.upper())
            new_key_path = key_path.copy()
            new_key_path.append(new_key)
            heap.heappush(h, (dist + new_dist, new_key, new_keys_left, new_unlocked, new_key_path))

def pt2(input):
    map = {}
    map_keys = {}
    map_doors = {}
    map_keys_doors = {}
    walls = set()
    tiles = set()
    starts = [() for _ in range(4)]

    for y in range(len(input)):
        for x in range(len(input[y].strip())):
            c = input[y][x]
            p = (x,y)
            if c.isdigit():
                starts[int(c)-1] = p
                map[p] = "."
                map_keys[p] = c
            elif c.isalpha():
                if c.isupper():
                    map_doors[p] = c
                else:
                    map_keys[p] = c
                map[p] = "."
            elif c == "#":
                walls.add(p)
                map[p] = c
            elif c == ".":
                tiles.add(p)
                map[p] = c
    map_keys_doors.update(map_keys)
    map_keys_doors.update(map_doors)

    dead = set()
    for _ in range(10):
        #TODO repeat until no further changes are done
        ends = set()
        intersections = set()
        for tile in tiles:
            num_walls = 0
            for n in neighbours(tile):
                if n in walls or n in dead:
                    num_walls += 1
            if num_walls == 3 and n:
                ends.add(tile)
            elif num_walls < 2:
                intersections.add(tile)

        for end in ends:
            cur = end
            while cur not in intersections and cur not in map_keys and \
                    cur not in map_doors and cur not in starts:
                dead.add(cur)
                if cur in tiles:
                    tiles.remove(cur)
                for n in neighbours(cur):
                    if n not in walls and n not in dead:
                        cur = n
                        break

    paths = {}  # every path available between any door and any key (without passing another door or key
    start_paths = [{} for _ in range(len(starts))]  # every path available from the starting point (including doors)

    graph = {}  # {node: [children as tuples with name and distance]}
    for ent, s in map_keys_doors.items():
        for a in avail(map, ent, map_keys_doors, dead):
            paths[(s, a[2])] = a[0]
            graph[s] = graph.get(s, []) + [(a[2], a[0])]
    for start in range(len(starts)):
        for a in avail(map, starts[start], map_keys_doors, dead):
            start_paths[start][("@", map_keys_doors[a[1]])] = a[0]

    h = []
    cur = [str(i+1) for i in range(len(starts))]
    keys_left = set(map_keys.values()).copy()
    for i in range(len(starts)):
        keys_left.remove(str(i+1))
    unlocked = set()
    key_path = []
    heap.heappush(h, (0, cur, keys_left, unlocked, key_path))

    best = len(map_keys)
    seen = set()

    while True:
        dist, keys, keys_left, unlocked, key_path = heap.heappop(h)
        if (frozenset(key_path), tuple(keys)) in seen:
            continue
        seen.add((frozenset(key_path), tuple(keys)))
        if len(keys_left) == 0:
            return dist, key_path
        if len(keys_left) < best:
            best = len(keys_left)
        for orig_start in range(len(starts)):
            for new_dist, new_key in avail_keys(map, map_keys, map_doors, graph, keys[orig_start], keys_left, unlocked):
                new_keys = keys.copy()
                new_keys[orig_start] = new_key
                new_keys_left = keys_left.copy()
                new_keys_left.remove(new_key)
                new_unlocked = unlocked.copy()
                new_unlocked.add(new_key.upper())
                new_key_path = key_path.copy()
                new_key_path.append(new_key)
                heap.heappush(h, (dist + new_dist, new_keys, new_keys_left, new_unlocked, new_key_path))

if __name__ == "__main__":
    print(pt1(open("../input/18-1", "r").readlines()))
    print(pt2(open("../input/18-2", "r").readlines()))