|
Download tasks/program-execution-tracing/contexts/sample-00034.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 383 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00034.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00034.md
-
curl -L -o sample-00034.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00034.md
383 kB
| ### 001 | |
| ```python | |
| def f1(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] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f1([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f2(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f3([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(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} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[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 | |
| ``` | |
| ``` | |
| f4(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(nodes, edges): | |
| nodes = list(nodes) | |
| 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 | |
| rootsource = dict(indegree) | |
| complete = set() | |
| sequence = [] | |
| layers = [] | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| complete.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 rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f5(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f6(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(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} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| 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 | |
| ``` | |
| ``` | |
| f7([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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 | |
| 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] | |
| 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 | |
| ``` | |
| ``` | |
| f8([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f9([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 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)} | |
| 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 = 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 | |
| ``` | |
| ``` | |
| f10([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(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 += 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 | |
| ``` | |
| ``` | |
| f12([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(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(min(count, 64)): | |
| x += dx | |
| 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 = 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 | |
| ``` | |
| ``` | |
| f13([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(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] | |
| increases = 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': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f14([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f15(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f16(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f17([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f18(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| 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] | |
| growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f19([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f20(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f21(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(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]))) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f22([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 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)} | |
| 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 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 | |
| ``` | |
| ``` | |
| f23([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f24(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(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) | |
| 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 | |
| ``` | |
| ``` | |
| f25(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f26(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(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 = 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 = 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 | |
| ``` | |
| ``` | |
| f28([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f29(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(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[: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 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 | |
| ``` | |
| ``` | |
| f30([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(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 | |
| 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 | |
| ``` | |
| ``` | |
| f31(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(nodes, edges): | |
| nodes = list(nodes) | |
| 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 | |
| startdegree = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| 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=any((str(node).isupper() for node in nodes))) | |
| 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]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(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) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) | |
| 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: | |
| handled.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 | |
| ``` | |
| ``` | |
| f33(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(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]))) | |
| 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': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(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 = 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 | |
| ``` | |
| ``` | |
| f35([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right if tidy(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 | |
| ``` | |
| ``` | |
| f36(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(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 | |
| ``` | |
| ``` | |
| f37(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f39([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(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(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 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 | |
| ``` | |
| ``` | |
| f40([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right if tidy(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 | |
| ``` | |
| ``` | |
| f41(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f42(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').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 | |
| 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(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f44(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f45(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper()[:12] | |
| 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(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 = [] | |
| 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 (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 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 | |
| ``` | |
| ``` | |
| f49(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(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(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f50([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f52(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 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)} | |
| 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 % 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 = 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 | |
| ``` | |
| ``` | |
| f53([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(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: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| 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 | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f54(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(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] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f55([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(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 += 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 | |
| ``` | |
| ``` | |
| f56([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(board): | |
| board = list(board) | |
| known = 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 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 (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 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 | |
| ``` | |
| ``` | |
| f57(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(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 += 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 = (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 | |
| ``` | |
| ``` | |
| f58([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| 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 | |
| ``` | |
| ``` | |
| f59([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(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) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f60([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(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[: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 crossings: | |
| crossings.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': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f61([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) < 2: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f62(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| work.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 | |
| ``` | |
| ``` | |
| f63([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(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 | |
| ``` | |
| ``` | |
| f64(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| 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, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f65([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().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 | |
| 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 | |
| ``` | |
| ``` | |
| f66(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(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((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 | |
| ``` | |
| ``` | |
| f67([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').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 | |
| ``` | |
| ``` | |
| f68(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(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) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| work.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[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f69([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(nodes, edges): | |
| nodes = list(nodes) | |
| 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) | |
| handled = set() | |
| listing = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.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 initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f70(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.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, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f71([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f72(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f73([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(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 += 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 | |
| ``` | |
| ``` | |
| f74([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f75(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f76(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(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: | |
| if before == after: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f77(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f78([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| 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, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f79([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 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))] | |
| 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 | |
| startdegree = dict(needs) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[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 children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f80(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(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 = 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 | |
| ``` | |
| ``` | |
| f81([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper()[:12] | |
| 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} - 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 | |
| ``` | |
| ``` | |
| f82(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(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 col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.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 found): | |
| found.add(nxt) | |
| if len(found) >= 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,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(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} - 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 | |
| ``` | |
| ``` | |
| f84(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(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 | |
| 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), 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 rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f85(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(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) | |
| levels = {start: 0} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(nextmap.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[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f86([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(nodes, edges): | |
| nodes = list(nodes) | |
| 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 = [] | |
| groups = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if needs[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]: | |
| 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 | |
| ``` | |
| ``` | |
| f87(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(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 {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f88([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper()[:12] | |
| banned = {str(value).upper() for value in banned} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - banned | |
| b = {canon(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 | |
| ``` | |
| ``` | |
| f89(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f90([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in 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 | |
| ``` | |
| ``` | |
| f91([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(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 | |
| ``` | |
| ``` | |
| f93([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(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]))) | |
| 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 | |
| ``` | |
| ``` | |
| f94([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(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) | |
| if len(found) >= 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 | |
| ``` | |
| ``` | |
| f95(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| 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 | |
| ``` | |
| ``` | |
| f96(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f97(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f98(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(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) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| 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), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| groups.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': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f99(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| banned = {normalize(value) 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 | |
| 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 | |
| ``` | |
| ``` | |
| f100(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| result = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f101([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(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 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| result = [] | |
| 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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f102(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f103(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(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 += 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 | |
| ``` | |
| ``` | |
| f104([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f105([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f106(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| 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 (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| 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, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f107([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(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} | |
| 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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f108([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(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} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| 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 | |
| ``` | |
| ``` | |
| f109([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(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] | |
| increases = sum((1 for pos in range(1, min(65, 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 | |
| ``` | |
| ``` | |
| f110([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {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) | |
| needs[b] += 1 | |
| startdegree = 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)) | |
| 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 nextmap[node]: | |
| needs[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 | |
| ``` | |
| ``` | |
| f111(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(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) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| 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 | |
| ``` | |
| ``` | |
| f112([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} 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(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f114(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f115(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(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} | |
| 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 | |
| initial = dict(indegree) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[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]: | |
| indegree[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 | |
| ``` | |
| ``` | |
| f116(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(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 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 | |
| ``` | |
| ``` | |
| f117(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f118([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| 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) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f119([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(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 = max(-64, min(64, x + dx)) | |
| 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 | |
| ``` | |
| ``` | |
| f120([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(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 | |
| ``` | |
| ``` | |
| f121(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| spaces.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f122([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(board): | |
| board = list(board) | |
| found = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f123(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(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} - excluded | |
| 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 | |
| ``` | |
| ``` | |
| f124(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(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 | |
| ``` | |
| ``` | |
| f125(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(board): | |
| board = list(board) | |
| visited = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f126(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f127(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(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 = 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 = 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 | |
| ``` | |
| ``` | |
| f128([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f129(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| 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 = [] | |
| 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 (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 | |
| ``` | |
| ``` | |
| f130(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(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() | |
| sequence = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| 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]: | |
| 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 | |
| ``` | |
| ``` | |
| f131(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper() | |
| 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 | |
| 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 | |
| ``` | |
| ``` | |
| f132(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(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]))) | |
| matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f133([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f134(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f135([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(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) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.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': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f137([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(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] < 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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f138([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(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] | |
| 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 | |
| ``` | |
| ``` | |
| f139([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(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} 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[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f140([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(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) | |
| 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 | |
| ``` | |
| ``` | |
| f141(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f142(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).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 | |
| 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 | |
| ``` | |
| ``` | |
| f143(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(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() | |
| listing = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = 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 available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[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 | |
| ``` | |
| ``` | |
| f144(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f145(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f146([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(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(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f147([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop or len(word) < 2: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f148(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(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(min(count, 64)): | |
| x += dx | |
| y = max(-64, min(64, 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 | |
| ``` | |
| ``` | |
| f149([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').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 | |
| 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 | |
| ``` | |
| ``` | |
| f150(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(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] | |
| 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 | |
| ``` | |
| ``` | |
| f151([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(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() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| layers.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': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f152(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| ignored = {tidy(value) 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 | |
| ``` | |
| ``` | |
| f153(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(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 += 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 | |
| ``` | |
| ``` | |
| f154([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.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 | |
| 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 | |
| ``` | |
| ``` | |
| f156([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f157(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(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: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| startdegree = dict(indegree) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[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 children[node]: | |
| indegree[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 | |
| ``` | |
| ``` | |
| f158(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', '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(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| ignored = {tidy(value) 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f159(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| 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] | |
| increases = sum((1 for pos in range(1, min(65, 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 | |
| ``` | |
| ``` | |
| f160([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(totals) 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': max(totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f161([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f162([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(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 = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.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': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f163([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(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] | |
| increases = 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': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f165([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(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) if value > limit] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f166([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(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[row])): | |
| 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 | |
| ``` | |
| ``` | |
| f167(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(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 and (limit >= 0 or pos > 0)] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f168([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(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: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = 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)) | |
| 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 nextmap[node]: | |
| incoming[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 | |
| ``` | |
| ``` | |
| f169(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f170(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f171(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(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) | |
| handled = set() | |
| listing = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f172(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(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 | |
| ``` | |
| ``` | |
| f173(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(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 = 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 = 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 | |
| ``` | |
| ``` | |
| f174([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f175(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(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[: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 crossings: | |
| crossings.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': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f176([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f177([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| 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 | |
| ``` | |
| ``` | |
| f178([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.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 | |
| ``` | |
| ``` | |
| f179([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(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[: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 repeats: | |
| repeats.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': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f180([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(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] < min(limit, 3): | |
| for nxt in (node for node in sorted(routes.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 | |
| ``` | |
| ``` | |
| f181([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f182(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').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 | |
| ``` | |
| ``` | |
| f183(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(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 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| 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 | |
| groups.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': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f184(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(board): | |
| board = list(board) | |
| found = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f185(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f186(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(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 | |
| rootsource = dict(indegree) | |
| handled = set() | |
| listing = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(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 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| 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 | |
| groups.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': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f188(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f189([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.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(routes.get(cur, [])): | |
| 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 | |
| ``` | |
| ``` | |
| f190([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(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[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) | |
| 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 | |
| ``` | |
| ``` | |
| f191(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(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 = 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 | |
| ``` | |
| ``` | |
| f192([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(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 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 distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| 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 | |
| ``` | |
| ``` | |
| f193([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(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 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 | |
| ``` | |
| ``` | |
| f194(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(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} - 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 | |
| ``` | |
| ``` | |
| f195(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f196([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f197(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f198(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {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) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| result = [] | |
| 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), 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 nextmap[node]: | |
| needs[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 | |
| ``` | |
| ``` | |
| f199(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(segments))): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f200([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f201(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 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) | |
| 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":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(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) | |
| 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)) | |
| 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]: | |
| 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 | |
| ``` | |
| ``` | |
| f203(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| 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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f205(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(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])) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(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 = max(-64, min(64, 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 | |
| ``` | |
| ``` | |
| f207([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f208(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(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]))) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(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 = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| 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]: | |
| indegree[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 | |
| ``` | |
| ``` | |
| f211(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(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 = (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 | |
| ``` | |
| ``` | |
| f212([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(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])) | |
| positions = [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': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f213([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).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 | |
| ``` | |
| ``` | |
| f214(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(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 | |
| ``` | |
| ``` | |
| f215([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(board): | |
| board = list(board) | |
| found = 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 found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.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 found): | |
| found.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 | |
| ``` | |
| ``` | |
| f216(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(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 = 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 = 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 | |
| ``` | |
| ``` | |
| f217([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(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) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| work.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 | |
| ``` | |
| ``` | |
| f218([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 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 | |
| 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 += 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 | |
| ``` | |
| ``` | |
| f219([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(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 = (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 | |
| ``` | |
| ``` | |
| f220([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap % 5 + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f221([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(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() | |
| listing = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = 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 available: | |
| break | |
| current = available | |
| layers.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': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f222(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(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[:64]) if value >= limit] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f223([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(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]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = 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': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f224([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5 + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f225([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(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 = [] | |
| 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 (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 | |
| ``` | |
| ``` | |
| f226(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f227([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).upper() 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f228(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f229(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| 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) | |
| 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,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(nodes, edges): | |
| nodes = list(nodes) | |
| 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 | |
| startdegree = dict(indegree) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[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]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f231(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f232([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(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 | |
| initial = 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=any((str(node).isupper() for node in nodes))) | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f233(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f234(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f235([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(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 % 100): | |
| 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 | |
| ``` | |
| ``` | |
| f236([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 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 | |
| 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 = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, 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 | |
| ``` | |
| ``` | |
| f237([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(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 | |
| 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 | |
| ``` | |
| ``` | |
| f238(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(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 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| 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 | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f239(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(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 = [] | |
| 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) | |
| 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,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).upper() 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} - 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 | |
| ``` | |
| ``` | |
| f241(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.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(routes.get(cur, [])) if len(str(node)) == 1): | |
| 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 | |
| ``` | |
| ``` | |
| f242([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(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] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.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 | |
| ``` | |
| ``` | |
| f243([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f244([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f245([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(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 += 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 | |
| ``` | |
| ``` | |
| f246([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(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 | |
| initial = 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)) | |
| 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 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 | |
| ``` | |
| ``` | |
| f247(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(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() | |
| 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[: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 children[node]: | |
| incoming[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 | |
| ``` | |
| ``` | |
| f248(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(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 | |
| ``` | |
| ``` | |
| f249([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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] | |
| 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 | |
| ``` | |
| ``` | |
| f250([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(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 if normalize(value) != normalize(value) or normalize(value) != ''} - 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 | |
| ``` | |
| ``` | |
| f251(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(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 | |
| ``` | |
| ``` | |
| f252([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f253(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(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] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f254([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f255(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f256(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(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} - 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f257(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(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, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f258([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').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 | |
| 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 | |
| ``` | |
| ``` | |
| f259(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.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 | |
| ``` | |
| ``` | |
| f260([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f261([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f262([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(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 = max(-64, min(64, 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 | |
| ``` | |
| ``` | |
| f263([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f264([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(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] < 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 | |
| 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 | |
| ``` | |
| ``` | |
| f265([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [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(series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f266([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f267(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + min(gap, 2): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f268([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(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) 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 | |
| ``` | |
| ``` | |
| f269([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 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)} | |
| 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 = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, 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 | |
| ``` | |
| ``` | |
| f270([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(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] < 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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f271([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(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[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 | |
| ``` | |
| ``` | |
| f272(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f273(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(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) if value >= limit] | |
| 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 | |
| ``` | |
| ``` | |
| f274([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(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 += 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 | |
| ``` | |
| ``` | |
| f275([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper()[:12] | |
| 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 | |
| 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 | |
| ``` | |
| ``` | |
| f276(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(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, min(65, 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 | |
| ``` | |
| ``` | |
| f277([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f278(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(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 = max(-64, min(64, 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 = (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 | |
| ``` | |
| ``` | |
| f279([('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-5,14,2],"end":[10,1],"far":16,"revisits":[[0,1],[0,0],[6,-3],[6,-2],[14,1]],"steps":37} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f280([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(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() | |
| sequence = [] | |
| layers = [] | |
| 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 | |
| layers.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': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f282(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(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] | |
| 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': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f283([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(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 | |
| ``` | |
| ``` | |
| f284(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(nodes, edges): | |
| nodes = list(nodes) | |
| 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 | |
| startdegree = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| 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 routes[node]: | |
| needs[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 | |
| ``` | |
| ``` | |
| f285(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap) + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f286([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f287(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(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 | |
| ``` | |
| ``` | |
| f288(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(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} 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) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f289([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(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] | |
| ups = sum((1 for pos in range(1, 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 | |
| ``` | |
| ``` | |
| f291([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| 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 and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(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} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| 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, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f293([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(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 % 100): | |
| 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 | |
| ``` | |
| ``` | |
| f294([('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1), ('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5)]) | |
| ``` | |
| ``` | |
| {"bounds":[0,-3,14,4],"end":[7,3],"far":18,"revisits":[[0,1],[0,0],[6,-1],[6,0],[14,3]],"steps":38} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f295(['delta', 'ivoryx', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTA","EMBER","FROST","GARNET","HELIUM","IVORYX"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTA","FROST","IVORYX"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(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) | |
| complete = set() | |
| result = [] | |
| layers = [] | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[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 | |
| ``` | |
| ``` | |
| f296(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(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() | |
| listing = [] | |
| 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) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[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 | |
| ``` | |
| ``` | |
| f297(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(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] < 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, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f298([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', '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"]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f299([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(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) | |
| handled = set() | |
| listing = [] | |
| rounds = [] | |
| 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 | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f300(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| 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 | |
| ``` | |
| ``` | |
| f301(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| 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, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302([6, 16, 6, 23, 6, 4, 13, 24, 10, 21, 17, 21, 6, 24, 8], 4, 40) | |
| ``` | |
| ``` | |
| {"best":72,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[51,51,39,46,47,51,68,72,69,65,68,59]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(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) | |
| 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 | |
| ``` | |
| ``` | |
| f304(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(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) | |
| if len(found) >= 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 | |
| ``` | |
| ``` | |
| f305(['##..#.', '.....#', '.##...', '.##.#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,1],[0,0],[0,4],[1,5],[3,4],[5,5]],"count":6,"filled":16,"sizes":[10,2,1,1,1,1]} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f306([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f307(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"delta":6,"ember":1,"frost":7,"garnet":3,"helium":3},"initials":{"a":7,"d":6,"e":1,"f":7,"g":3,"h":3},"size":141,"top":["amber","frost","delta","garnet","helium"]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(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) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| 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[:3], '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'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(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] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f309([6, 11, 6, 23, 6, 4, 13, 26, 10, 21, 17, 21, 6, 24, 8], 4, 46) | |
| ``` | |
| ``` | |
| {"best":74,"hits":[0,1,3,4,5,6,7,8,9,10,11],"rises":6,"sums":[46,46,39,46,49,53,70,74,69,65,68,59]} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(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) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| 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 | |
| layers.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': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f310(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f311(['deltax', 'ivory', 'frost', 'Ember', 'GARNET', 'helium', 'cyan '], ['cyan ', 'BLUE', ' Amber', 'GARNET', 'Ember', 'helium', 'cyan '], ['CYAN']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","DELTAX","EMBER","FROST","GARNET","HELIUM","IVORY"],"both":["EMBER","GARNET","HELIUM"],"left":["DELTAX","FROST","IVORY"],"right":["AMBER","BLUE"],"score":17} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(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} if any((start in edge for edge in edges)) else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| 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 | |
| ``` | |
| ``` | |
| f312([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":1,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f313([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[6,2,7,7,5],"longest":11,"segments":[[3,9],[15,17],[19,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f314([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(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 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[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 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 | |
| ``` | |
| ``` | |
| f315(['ui', 'proxy', 'auth', 'cache', 'jobs', 'api', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + max(0, gap) + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f316([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f318(['Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber', 'Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":7,"blue":3,"cyan":1,"delta":6,"garnet":3,"helium":3},"initials":{"a":7,"b":3,"c":1,"d":6,"g":3,"h":3},"size":117,"top":["amber","delta","blue","garnet","helium"]} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap % 5 + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f319([(3, 5), (6, 9), (15, 17), (19, 22), (29, 35), (34, 37), (44, 51), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":39,"holes":[6,7,7,5],"longest":11,"segments":[[3,9],[15,22],[29,37],[44,55],[60,67]]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(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[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) | |
| 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 | |
| ``` | |
| ``` | |
| f320(['##..#.', '.....#', '..#...', '.#..#.', '#.##..', '###..#']) | |
| ``` | |
| ``` | |
| {"anchors":[[4,0],[0,0],[0,4],[1,5],[2,2],[3,1],[3,4],[5,5]],"count":8,"filled":14,"sizes":[6,2,1,1,1,1,1,1]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| lines = list(('Frost, frost. helium frost', 'Amber, amber. ember blue helium', 'Delta, amber. garnet frost', 'Amber, amber. delta frost amber') + ('Delta, garnet. garnet frost', 'Cyan, helium. blue delta delta', 'Delta, blue. amber frost')) | |
| stop = list(('blue',) + ('cyan',)) | |
| normalized = set() | |
| for item in stop: | |
| normalized.add(item.lower()) | |
| stop = normalized | |
| words = [] | |
| for line in lines: | |
| words.extend(re.findall('[a-z]+', line.lower())) | |
| words = [word for word in words if word not in stop] | |
| counts = {} | |
| initials = {} | |
| for word in words: | |
| counts[word] = counts.get(word, 0) + 1 | |
| initials[word[0]] = initials.get(word[0], 0) + 1 | |
| top = sorted(counts, key=lambda word: (-counts[word], word))[:5] | |
| record = {'aa': top, 'bb': counts, 'cc': initials, 'dd': sum((len(word) for word in words))} | |
| memo = record['dd'] | |
| spans = list(((3, 5), (6, 9), (15, 17), (19, 22)) + ((29, 35), (34, 37), (44, 51), (49, 55), (60, 67))) | |
| route = memo | |
| cases = { | |
| 141: 2, | |
| 117: 1, | |
| } | |
| gap = cases[route] | |
| spans.sort(key=lambda pair: (pair[0], pair[1])) | |
| parts = [] | |
| pos = 0 | |
| while pos < len(spans): | |
| left, right = spans[pos] | |
| pos += 1 | |
| while pos < len(spans) and spans[pos][0] <= right + gap: | |
| right = max(right, spans[pos][1]) | |
| pos += 1 | |
| parts.append([left, right]) | |
| holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] | |
| record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} | |
| clue = record['cc'] | |
| left = list(('deltax', 'ivory') + ('frost', 'Ember', 'GARNET', 'helium', 'cyan ')) | |
| right = list(('cyan ', 'BLUE') + (' Amber', 'GARNET', 'Ember', 'helium', 'cyan ')) | |
| blocked = ['CYAN'] | |
| 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))} | |
| stamp = record['bb'][0] | |
| route = stamp | |
| cases = { | |
| 'DELTAX': list(('##..#.', '.....#', '.##...', '.##.#.') + ('#.##..', '###..#')), | |
| 'DELTA': 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)} | |
| ticket = record['cc'] | |
| route = ticket | |
| cases = { | |
| 14: list((('N', 2), ('S', 3), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1)) + (('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 5))), | |
| 16: list((('N', 2), ('S', 5), ('S', 2), ('E', 3), ('E', 3), ('N', 3), ('S', 1)) + (('N', 3), ('E', 3), ('E', 3), ('E', 2), ('N', 2), ('S', 1), ('W', 2), ('W', 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'] | |
| start = 'A' | |
| limit = 3 | |
| route = (clue, badge) | |
| cases = { | |
| (39, 16): list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'H'))), | |
| (39, 18): list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D'))), | |
| (37, 16): list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D'))), | |
| (37, 18): list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'D'))), | |
| } | |
| 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)} | |
| permit = record['bb']['D'] | |
| nodes = list(('ui', 'proxy', 'auth', 'cache', 'jobs') + ('api', 'worker', 'db')) | |
| route = permit | |
| cases = { | |
| 1: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth'))), | |
| 2: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache'))), | |
| } | |
| 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)} | |
| signal = record['aa'][0] | |
| width = 4 | |
| route = signal | |
| cases = { | |
| 'auth': (list((6, 11, 6, 23, 6, 4, 13) + (26, 10, 21, 17, 21, 6, 24, 8)), 46), | |
| 'cache': (list((6, 16, 6, 23, 6, 4, 13) + (24, 10, 21, 17, 21, 6, 24, 8)), 40), | |
| } | |
| values, limit = 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} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |