My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update day24.py

+9 -13
+9 -13
2016/day24.py
··· 18 18 19 19 20 20 def bfs(grid, start, goal): 21 - horizon = deque() 22 - horizon.append((start, 0)) 21 + horizon = deque([(start, 0)]) 23 22 seen = set(start) 24 23 25 24 while horizon: 26 25 (x, y), dst = horizon.pop() 26 + 27 27 if (x, y) == goal: 28 28 return dst 29 29 30 - for dx, dy, in DIRS: 30 + for dx, dy in DIRS: 31 31 nx, ny = x + dx, y + dy 32 32 if (nx, ny) not in seen and valid(grid, nx, ny): 33 33 horizon.appendleft(((nx, ny), dst + 1)) ··· 47 47 48 48 if __name__ == '__main__': 49 49 grid = [] 50 - num_locs = {} 50 + locations = {} 51 51 52 52 for y, line in enumerate(fileinput.input()): 53 - row = [] 54 53 for x, tile in enumerate(line): 55 - row.append(tile) 56 54 if tile.isdigit(): 57 - num_locs[int(tile)] = (x, y) 55 + locations[int(tile)] = (x, y) 58 56 59 - grid.append(row) 57 + grid.append(list(line)) 60 58 61 59 distances = {} 62 - max_num = max(num_locs) 63 60 64 - for a, b in combinations(range(max(num_locs) + 1), 2): 65 - dst = bfs(grid, num_locs[a], num_locs[b]) 66 - distances[a, b] = distances[b, a] = dst 61 + for a, b in combinations(range(max(locations) + 1), 2): 62 + distances[a, b] = distances[b, a] = bfs(grid, locations[a], locations[b]) 67 63 68 - paths = list(permutations(range(1, max_num + 1))) 64 + paths = list(permutations(range(1, max(locations) + 1))) 69 65 print "Fewest steps to visit all numbers:", min(navigate(distances, path) for path in paths) 70 66 print "Fewest steps to also return to 0:", min(navigate(distances, path, complete=True) for path in paths)