|
Download tasks/program-execution-tracing/contexts/sample-00032.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 377 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00032.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00032.md
-
curl -L -o sample-00032.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00032.md
377 kB
| ### 001 | |
| ```python | |
| def f1(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f1([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f2([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f3([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| complete = set() | |
| listing = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f4(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f5([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f6([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f7([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f8([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f9([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f10([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(ratemap[group]) | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f12([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f13(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f14([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f15([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f16([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f17(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f18([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f19([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f20([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(series[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f21([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| result = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f22([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f23([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f24([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f25([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f26([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f28([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f29(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f30(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f31(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f33([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f35([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f36([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f37(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f39([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| matches.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f40([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f41(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f42(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right if canon(value) != ''} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f43(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f44([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f45([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f49(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f50([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f52([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f53([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f54(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| result = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f55([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f56(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f57([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| result = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f58([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(ratemap[group]) | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| grand += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f59([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f60([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f61(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f62(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f63(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f64([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f65(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f66([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f67([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f68([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f69([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f70([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f71([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f72([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f73([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| complete = set() | |
| result = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f74(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f75(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f76(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f77(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f78(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f79(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f80(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f81([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f82([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f83(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f84(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f85(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f86(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right if normalize(value) != ''} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f87(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| grand += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f88([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f89([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f90(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f91([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(prices[group]) | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f93([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f94(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f95([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f96([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f97([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f98([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f99([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(ratemap[group]) | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f100([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f101([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f102(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f103([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f104(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f105([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f106([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f107([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f108(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f109(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f110(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f111([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f112(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f114([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f115([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f116([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f117([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f118([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * prices[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f119([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f120([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f121([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f122(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f123([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f124([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': edge[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f125([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f126([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f127([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f128([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f129(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f130(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f131(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f132(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f133([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f134([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f135([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f137([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * prices[group] | |
| kept.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': buckets, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f138([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f139(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f140([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f141(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f142(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f143([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f144([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f145([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f146([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(ratemap[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f147([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f148(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f149(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f150(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f151(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f152(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f153(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f154([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| result = list(levels) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': edge[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f156([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f157(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f158(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f159([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f160(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f161([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * prices[group] | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f162([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f163([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f165(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f166(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f167([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f168(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f169(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f170(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f171([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': edge[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f172([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f173([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f174([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f175([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f176([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f177([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f178([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f179([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f180([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f181([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f182([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f183([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f184([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f185([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| groups = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f186(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper() | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - ignored | |
| b = {canon(value) for value in right if canon(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f188([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f189(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f190([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f191([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f192([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f193([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| listing = [] | |
| groups = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f194(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f195([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': edge, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f196([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f197(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f198(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f199([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f200(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f201([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f202(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f203([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f205([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f207([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f208(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f211([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f212([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f213(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f214([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f215([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * factors[group] | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f216([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f217(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f218(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f219([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f220([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f221([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f222([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f223([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| listing = list(levels) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': edge, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f224([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f225(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f226(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f227(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded | |
| b = {normalize(value) for value in right if normalize(value) != ''} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f228(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f229(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f230(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f231([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f232([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f233([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f234(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f235(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f236(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f237([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(totals[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f238([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| complete = set() | |
| listing = [] | |
| rounds = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f239(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f240(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f241(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f242([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f243(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f244([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f245(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: | |
| charge = value * ratemap[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f246([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * prices[group] | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f247([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f248(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| startdegree = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f249(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f250([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| banned = {canon(value) for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f251(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f252([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| complete = set() | |
| result = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f253(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f254(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f255(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f256([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f257(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f258([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f259([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f260([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f261([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f262([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f263([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f264([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| score = value * ratemap[group] | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f265([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f266([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f267(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f268(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f269([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f270([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f271([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f272(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f273(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f274(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| handled = set() | |
| listing = [] | |
| groups = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f275(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f276([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2), ('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,12],"end":[-5,10],"far":17,"revisits":[[2,0],[1,0],[-5,11],[-5,10]],"steps":35} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f277(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals) if value > limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f278([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f279([('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2), ('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["EV",39],["FP",33],["DL",23]],"missing":["AN","BR","CX","HR"],"total":186} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f280([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value > limit] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f282([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| score = value * abs(factors[group]) | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f283([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * prices[group] | |
| matches.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| grand += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f284([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f285([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f286([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f287(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right if normalize(value) != ''} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f288(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f289([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f291([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right if normalize(value) != ''} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f293(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f294([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f295(['...#..', '.....#', '##..#.', '##.#..', '.#.#..', '##...#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[0,3],[1,5],[2,4],[5,5]],"count":6,"filled":13,"sizes":[7,2,1,1,1,1]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f296(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f297([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(ratemap[group]) | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f298([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f299(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f300([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f301([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f304([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":1,"groups":{"edge":18},"top":["DL02"],"total":18} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f305(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f306([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f307(['ivory', 'Ember', 'frostx', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["IVORY"],"left":["BLUE","CYAN","FROSTX","HELIUM"],"right":["AMBER","DELTA","FROST","GARNET"],"score":5} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f308([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f309(['#..#..', '.....#', '##..#.', '##.#..', '.#.#..', '##..##']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,0],[3,3],[5,4],[0,0],[0,3],[1,5],[2,4]],"count":7,"filled":15,"sizes":[7,2,2,1,1,1,1]} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges[:16]: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f310([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f311(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f312([('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2), ('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-5,3,13],"end":[-5,11],"far":18,"revisits":[[-5,12],[-5,11]],"steps":36} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f313([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f314(['ivory', 'Emberx', 'frost', 'helium', 'BLUE', 'cyan ', 'Ember'], ['delta', 'GARNET', 'frost', 'ivory', ' Amber', 'Ember', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","IVORY"],"left":["BLUE","CYAN","EMBERX","HELIUM"],"right":["AMBER","DELTA","GARNET"],"score":10} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f315([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 4, 54) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[1,2,8,9],"rises":4,"sums":[43,59,54,50,50,36,51,53,61,59,44,33]} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f316([('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2), ('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11)], [('DL', 10), ('EV', 3), ('FP', 3), ('GQ', 8)], 3) | |
| ``` | |
| ``` | |
| {"count":4,"items":[["GQ",91],["FP",48],["EV",39],["DL",23]],"missing":["AN","BR","CX","HR"],"total":201} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317(['proxy', 'api', 'ui', 'worker', 'db', 'auth', 'cache', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f318([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f319([('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15), ('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":6,"groups":{"core":77,"edge":60,"south":305},"top":["DL35","DL61","AN91"],"total":442} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value > limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f320([4, 10, 22, 7, 20, 5, 18, 7, 6, 20, 20, 15, 4, 5, 9], 6, 54) | |
| ``` | |
| ``` | |
| {"best":86,"hits":[0,1,2,3,4,5,6,7,8,9],"rises":4,"sums":[68,82,79,63,76,76,86,72,70,73]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| rows = list((('AN54', 'edge', 'void', 8), ('AN91', 'core', 'ready', 7), ('CX28', 'core', 'void', 8), ('BR65', 'core', 'void', 22), ('DL02', 'edge', 'checked', 6), ('AN39', 'south', 'void', 12), ('BR76', 'south', 'void', 15)) + (('CX13', 'edge', 'ready', 16), ('DL50', 'edge', 'ready', 4), ('BR87', 'south', 'void', 7), ('CX24', 'core', 'hold', 12), ('DL61', 'south', 'ready', 22), ('AN98', 'south', 'ready', 15), ('DL35', 'south', 'ready', 24))) | |
| keep = ['ready'] | |
| rates = dict((('north', 7), ('south', 5)) + (('core', 11), ('edge', 3))) | |
| chosen = [(code, group, value) for code, group, state, value in rows if state in keep] | |
| priced = [(code, group, value * rates[group]) for code, group, value in chosen] | |
| groups = {} | |
| for code, group, price in priced: | |
| groups[group] = groups.get(group, 0) + price | |
| ranked = sorted(priced, key=lambda row: (-row[2], row[0])) | |
| record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups} | |
| signal = record['bb'] | |
| right = list(('delta', 'GARNET') + ('frost', 'ivory', ' Amber', 'Ember', 'Ember')) | |
| blocked = ['EMBER'] | |
| route = signal | |
| cases = { | |
| 442: list(('ivory', 'Emberx') + ('frost', 'helium', 'BLUE', 'cyan ', 'Ember')), | |
| 18: list(('ivory', 'Ember') + ('frostx', 'helium', 'BLUE', 'cyan ', 'Ember')), | |
| } | |
| left = cases[route] | |
| a = set() | |
| b = set() | |
| ban = set() | |
| for value in blocked: | |
| ban.add(value.strip().upper()) | |
| for value in left: | |
| value = value.strip().upper() | |
| if value not in ban: | |
| a.add(value) | |
| for value in right: | |
| value = value.strip().upper() | |
| if value not in ban: | |
| b.add(value) | |
| both = a.intersection(b) | |
| record = {'aa': sorted(both), 'bb': sorted(a.difference(b)), 'cc': sorted(b.difference(a)), 'dd': sorted(a.union(b)), 'ee': sum(map(len, both))} | |
| token = record['ee'] | |
| route = token | |
| cases = { | |
| 5: list(('#..#..', '.....#', '##..#.', '##.#..') + ('.#.#..', '##..##')), | |
| 10: list(('...#..', '.....#', '##..#.', '##.#..') + ('.#.#..', '##...#')), | |
| } | |
| board = cases[route] | |
| cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#'] | |
| parent = {cell: cell for cell in cells} | |
| def root(cell): | |
| while parent[cell] != cell: | |
| parent[cell] = parent[parent[cell]] | |
| cell = parent[cell] | |
| return cell | |
| def merge(a, b): | |
| a = root(a) | |
| b = root(b) | |
| if a != b: | |
| parent[b] = a | |
| for row, col in cells: | |
| for other in ((row - 1, col), (row, col - 1)): | |
| if other in parent: | |
| merge((row, col), other) | |
| groups = {} | |
| for cell in cells: | |
| groups.setdefault(root(cell), []).append(cell) | |
| parts = [(len(group), min(group)) for group in groups.values()] | |
| parts.sort(key=lambda item: (-item[0], item[1])) | |
| record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)} | |
| memo = record['cc'] | |
| right = list((('DL', 10), ('EV', 3)) + (('FP', 3), ('GQ', 8))) | |
| fee = 3 | |
| route = memo | |
| cases = { | |
| 13: list((('AN', 10), ('BR', 3), ('CX', 11), ('DL', 2)) + (('EV', 12), ('FP', 10), ('GQ', 11), ('HR', 11))), | |
| 15: list((('AN', 10), ('BR', 3), ('CX', 9), ('DL', 2)) + (('EV', 12), ('FP', 15), ('GQ', 11), ('HR', 11))), | |
| } | |
| left = cases[route] | |
| joined = [] | |
| for key, count in left: | |
| matches = [rate for name, rate in right if name == key] | |
| if matches: | |
| joined.append([key, count * matches[0] + fee]) | |
| joined.sort(key=lambda item: (-item[1], item[0])) | |
| rightkeys = {key for key, rate in right} | |
| record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)} | |
| clue = record['bb'] | |
| start = 'A' | |
| limit = 3 | |
| route = token | |
| cases = { | |
| 5: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('A', 'F'))), | |
| 10: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'H'))), | |
| } | |
| edges = cases[route] | |
| links = {} | |
| for edge in edges: | |
| links.setdefault(edge[0], []).append(edge[1]) | |
| distance = {start: 0} | |
| queue = deque([start]) | |
| while queue: | |
| node = queue.popleft() | |
| if distance[node] == limit: | |
| continue | |
| for child in sorted(links.get(node, [])): | |
| if child in distance: | |
| continue | |
| distance[child] = distance[node] + 1 | |
| queue.append(child) | |
| furthest = max(distance.values()) | |
| record = {'aa': list(distance), 'bb': distance, 'cc': sorted((node for node in distance if distance[node] == furthest)), 'dd': len(distance)} | |
| stamp = record['aa'][3] | |
| values = list((4, 10, 22, 7, 20, 5, 18, 7) + (6, 20, 20, 15, 4, 5, 9)) | |
| limit = 54 | |
| route = (clue, stamp) | |
| cases = { | |
| (186, 'D'): 6, | |
| (186, 'F'): 4, | |
| (201, 'D'): 4, | |
| (201, 'F'): 4, | |
| } | |
| width = cases[route] | |
| current = sum(values[:width]) | |
| sums = [current] | |
| for pos in range(width, len(values)): | |
| current += values[pos] - values[pos - width] | |
| sums.append(current) | |
| hits = [] | |
| rises = 0 | |
| for pos, value in enumerate(sums): | |
| if value >= limit: | |
| hits.append(pos) | |
| if pos and value > sums[pos - 1]: | |
| rises += 1 | |
| record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} | |
| ticket = record['bb'] | |
| route = ticket | |
| cases = { | |
| 61: list((('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 6), ('W', 2)) + (('N', 3), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2))), | |
| 86: list((('E', 2), ('S', 2), ('S', 3), ('E', 1), ('N', 2), ('N', 3), ('W', 2)) + (('N', 5), ('W', 2), ('W', 3), ('W', 1), ('N', 3), ('N', 1), ('N', 3), ('S', 2))), | |
| } | |
| moves = cases[route] | |
| delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| points = [[0, 0]] | |
| for direction, count in moves: | |
| dx, dy = delta[direction] | |
| for turn in range(count): | |
| points.append([points[-1][0] + dx, points[-1][1] + dy]) | |
| seen = set() | |
| revisits = [] | |
| for point in points: | |
| key = tuple(point) | |
| if key in seen and point not in revisits: | |
| revisits.append(point) | |
| seen.add(key) | |
| record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} | |
| badge = record['bb'] | |
| nodes = list(('proxy', 'api', 'ui') + ('worker', 'db', 'auth', 'cache', 'jobs')) | |
| route = badge | |
| cases = { | |
| 17: list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth'))), | |
| 18: list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db'))), | |
| } | |
| edges = cases[route] | |
| remaining = set(nodes) | |
| done = set() | |
| order = [] | |
| batches = [] | |
| while remaining: | |
| ready = sorted((node for node in remaining if all((before in done for before, after in edges if after == node)))) | |
| if not ready: | |
| break | |
| batches.append(ready) | |
| order.extend(ready) | |
| done.update(ready) | |
| remaining.difference_update(ready) | |
| roots = sorted((node for node in nodes if all((after != node for before, after in edges)))) | |
| record = {'aa': order, 'bb': batches, 'cc': roots, 'dd': len(order)} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |