def b(): sizes = [0, 0, 0] def nbrs(p): return (n for n in u.orthogonal(grid, p) if grid[n] < 9) for p in grid: if is_basin(p): _, dists = u.bfs(p, nbrs) heapq.heappushpop(sizes, len(dists)) return math.prod(sizes)
def b(): def neighbors(pos): x, y = pos for dx, dy in u.dirs: npos = (x + dx, y + dy) if is_open(npos): yield npos _, dists = u.bfs( (1, 1), neighbors, is_done=lambda node, p, d: max(d.values()) > 50, ) return len(dists) - 1
def a(): target = (31, 39) def neighbors(pos): x, y = pos for dx, dy in u.dirs: npos = (x + dx, y + dy) if is_open(npos): yield npos _, dists = u.bfs((1, 1), neighbors, is_done=lambda node, pred, dist: node == target) return dists[target]
def a(): terminal = None def is_done(node, preds, dists): nonlocal terminal if node[0] == (3, 3): terminal = node return True else: return False start = ((0, 0), b"") _, dists = u.bfs(start, neighbors, is_done) return terminal[1].decode("ascii")
def reachable(grid, src, c): def neighbors(n): for neighbor in u.orthogonal(grid, n): if grid[neighbor] == '.': yield neighbor _, dists = u.bfs(src, neighbors) def allowed(dst): if src in room_positions: return dst in hallway_positions else: if target_grid[dst] != c: return False return room_occupants(grid, dst).issubset({'.', c}) wt = weightmap[c] return ((make_move(grid, src, p), wt * dist) for p, dist in dists.items() if allowed(p))
def a(): _, dists = u.bfs(target, lambda node: [nbr for _, nbr in contained_by[node]]) s = len(dists) - 1 return s
submit = True from collections import defaultdict, deque import sys from aocd import lines import u edges = [edge.strip().split(")") for edge in lines] orbits = defaultdict(set) for s, t in edges: orbits[s].add(t) preds, dists = u.bfs("COM", lambda node: orbits[node]) def a(): return sum(dists.values()) def b(): spath = {} i = 0 ptr = "SAN" while ptr != "COM": spath[ptr] = i i += 1 ptr = preds[ptr] ptr = "YOU" i = 0 count = None
def b(): _, dists = u.bfs(oxpos, lambda node: graph[node]) return max(dists.values())