### 001 ```python def f1(lines, stop): lines = list(lines) frequency = {} 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[: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 ``` ``` f1(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 002 ```python def f2(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) 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 ``` ``` f2([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 003 ```python def f3(rows, keep, ratemap): rows = list(rows) kept = [] totals = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * abs(ratemap[group]) kept.append((code, score)) totals[group] = totals.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f3([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 004 ```python def f4(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if str(state).lower() in {str(item).lower() for item in keep}: score = value * ratemap[group] kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f4([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 005 ```python def f5(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, 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 ``` ``` f5([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 006 ```python def f6(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 initial = dict(indegree) finished = set() listing = [] layers = [] 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 layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f6(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 007 ```python def f7(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors and rate0 != 0: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f7([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 008 ```python def f8(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) 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 ``` ``` f8([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 009 ```python def f9(rows, keep, prices): rows = list(rows) matches = [] totals = {} sumvalue = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value != 0: score = value * prices[group] matches.append((code, score)) totals[group] = totals.get(group, 0) + score sumvalue += score matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in matches)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f9([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 010 ```python def f10(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: 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])) 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 ``` ``` f10(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 011 ```python def f11(rows, keep, prices): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * abs(prices[group]) kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f11([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 012 ```python def f12(spans, gap): spans = list(spans) segments = [] for left, right in sorted(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 ``` ``` f12([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 013 ```python def f13(lines, stop): lines = list(lines) frequency = {} 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 or len(word) > 12: continue frequency[word] = frequency.get(word, 0) + 1 ranking = 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 ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f13(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 014 ```python def f14(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([0] + windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f14([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 015 ```python def f15(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] growth = 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': growth} output = dict() output.update(returnvalue) return output ``` ``` f15([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 016 ```python def f16(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 (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): 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, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f16([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 017 ```python def f17(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f17([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 018 ```python def f18(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() result = [] groups = [] 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 groups.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': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f18(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 019 ```python def f19(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap + 1: 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((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 ``` ``` f19([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 020 ```python def f20(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * abs(ratemap[group]) kept.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge amount += charge kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f20([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 021 ```python def f21(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: 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 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 ``` ``` f21(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 022 ```python def f22(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])) 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 ``` ``` f22(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 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 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f23([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 024 ```python def f24(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 and (limit >= 0 or pos > 0)] increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f24([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 025 ```python def f25(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 combined = [] for key, count in left: if key in prices: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f25([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 026 ```python def f26(edges, start, limit): edges = list(edges) children = {} for a, b in edges: 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) 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 ``` ``` f26([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 027 ```python def f27(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) 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) 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 ``` ``` f27([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 028 ```python def f28(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left: if key in prices and count > 0: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f28([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 029 ```python def f29(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) steps = {start: 0} pending = [start] while pending: cur = pending.pop(0) if steps[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 pending.append(nxt) sequence = list(steps) lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f29([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 030 ```python def f30(rows, keep, prices): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * prices[group] matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f30([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 031 ```python def f31(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` ``` f31([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 032 ```python def f32(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 = max(-64, min(64, 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 = (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 ``` ``` f32([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 033 ```python def f33(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: if word in stop: 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 ``` ``` f33(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 034 ```python def f34(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 rootsource = dict(incoming) finished = set() listing = [] 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) listing.append(node) for node in current: for nxt in routes[node]: incoming[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 ``` ``` f34(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 035 ```python def f35(rows, keep, prices): rows = list(rows) kept = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: score = value * prices[group] kept.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score sumvalue += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f35([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 036 ```python def f36(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] + gap % 5: 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 ``` ``` f36([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 037 ```python def f37(edges, start, limit): edges = list(edges) children = {} for a, b in edges[:16]: children.setdefault(a, []).append(b) levels = {start: 0} if any((start in edge for edge in edges)) else {} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in levels and (not nxt.startswith('Z')): levels[nxt] = levels[cur] + 1 pending.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f37([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 038 ```python def f38(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 ``` ``` f38([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 039 ```python def f39(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() result = [] 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) result.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f39(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 040 ```python def f40(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])) 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 ``` ``` f40(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 041 ```python def f41(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(totals) if value >= limit and (limit >= 0 or pos > 0)] ups = sum((1 for pos in range(1, len(totals)) if totals[pos] >= totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f41([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 042 ```python def f42(rows, keep, factors): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: charge = value * factors[group] kept.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge grand += charge kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': buckets, 'top': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f42([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 043 ```python def f43(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: charge = value * prices[group] selected.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge sumvalue += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f43([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 044 ```python def f44(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, 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 ``` ``` f44([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 045 ```python def f45(edges, start, limit): edges = list(edges) children = {} for a, b in edges[:16]: children.setdefault(a, []).append(b) distance = {start: 0} if limit > 0 or limit < 0 else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: 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 ``` ``` f45([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 046 ```python def f46(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f46([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 047 ```python def f47(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: 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 ``` ``` f47(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 048 ```python def f48(rows, keep, prices): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * prices[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f48([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 049 ```python def f49(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) if not available: break current = available layers.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f49(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 050 ```python def f50(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[:64]: 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])) 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 ``` ``` f50(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 051 ```python def f51(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 += 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 ``` ``` f51([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 052 ```python def f52(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 += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (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 ``` ``` f52([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 053 ```python def f53(rows, keep, prices): rows = list(rows) selected = [] buckets = {} grand = 0 for code, group, state, value in rows: if state in keep: charge = value * prices[group] selected.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge grand += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f53([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 054 ```python def f54(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap and count > 0: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f54([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 055 ```python def f55(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': max(series), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f55([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 056 ```python def f56(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 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 ``` ``` f56(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 057 ```python def f57(rows, keep, prices): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * prices[group] selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f57([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 058 ```python def f58(rows, keep, prices): rows = list(rows) kept = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * prices[group] kept.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f58([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 059 ```python def f59(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 ``` ``` f59([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 060 ```python def f60(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: 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])) 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 ``` ``` f60(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 061 ```python def f61(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[: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) < 2: 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 ``` ``` f61(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 062 ```python def f62(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not combined or left > combined[-1][1] + gap + 1: 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 ``` ``` f62([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 063 ```python def f63(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f63([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 064 ```python def f64(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f64(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 065 ```python def f65(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f65([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 066 ```python def f66(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left[:20]: if key in factors: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f66([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 067 ```python def f67(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f67([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 068 ```python def f68(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(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (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 ``` ``` f68([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 069 ```python def f69(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): 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 ``` ``` f69([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 070 ```python def f70(lines, stop): lines = list(lines) frequency = {} 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 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 ``` ``` f70(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 071 ```python def f71(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) levels = {start: 0} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.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, 'count': len(levels)} 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', 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"]} ``` ### 072 ```python def f72(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): 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 ``` ``` f72([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 073 ```python def f73(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x = max(-64, min(64, x + dx)) y += 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 ``` ``` f73([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 074 ```python def f74(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 items = [] for key, count in left: if key in prices and count > 0: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f74([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 075 ```python def f75(rows, keep, factors): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * abs(factors[group]) selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f75([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 076 ```python def f76(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' or (char == "'" and word): 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])) 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': min(256, sum((len(word) * count for word, count in wordmap.items())))} output = dict() output.update(returnvalue) return output ``` ``` f76(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 077 ```python def f77(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] 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 ``` ``` f77([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 078 ```python def f78(rows, keep, prices): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: score = value * abs(prices[group]) kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f78([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 079 ```python def f79(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) incoming[b] += 1 rootsource = dict(incoming) complete = set() listing = [] groups = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch groups.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f79(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 080 ```python def f80(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] + 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(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 ``` ``` f80([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 081 ```python def f81(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() result = [] rounds = [] 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 rounds.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f81(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 082 ```python def f82(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap and rate0 != 0: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in ratemap}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f82([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 083 ```python def f83(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 items = [] for key, count in left[:20]: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f83([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 084 ```python def f84(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 = min(64, 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 ``` ``` f84([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 085 ```python def f85(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: continue tallies[word] = tallies.get(word, 0) + 1 sorteditems = 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 sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f85(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 086 ```python def f86(lines, stop): lines = list(lines) frequency = {} 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: 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 ``` ``` f86(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 087 ```python def f87(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: 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 ``` ``` f87(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 088 ```python def f88(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f88([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 089 ```python def f89(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value >= 0: charge = value * prices[group] selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f89([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 090 ```python def f90(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left[:20]: if key in prices: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f90([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 091 ```python def f91(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap and count > 0: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f91([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 092 ```python def f92(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(0, 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': min(100, max((right - left for left, right in segments)))} output = dict() output.update(returnvalue) return output ``` ``` f92([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 093 ```python def f93(rows, keep, ratemap): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * abs(ratemap[group]) matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f93([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 094 ```python def f94(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 startdegree = dict(needs) finished = set() listing = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch 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 startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f94(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 095 ```python def f95(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.setdefault(a, []).append(b) steps = {start: 0} pending = [start] while pending: cur = pending.pop(0) if steps[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 pending.append(nxt) listing = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f95([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 096 ```python def f96(edges, start, limit): edges = list(edges) children = {} for a, b in edges[:16]: children.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) result = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f96([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 097 ```python def f97(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + gap % 5: result.append([left, right]) else: result[-1][1] = max(0, 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 ``` ``` f97([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 098 ```python def f98(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) needs[b] += 1 initial = dict(needs) complete = set() listing = [] layers = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in routes[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 ``` ``` f98(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 099 ```python def f99(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] growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f99([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 100 ```python def f100(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) handled = set() result = [] layers = [] 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), 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) result.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f100(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 101 ```python def f101(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors and rate0 != 0: factors[name0] = rate0 items = [] for key, count in left[:20]: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f101([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 102 ```python def f102(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} terms = [] for line in lines[:32]: 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 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 ``` ``` f102(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 103 ```python def f103(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} grand = 0 for code, group, state, value in rows: if state in keep: cost = value * abs(prices[group]) selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost grand += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f103([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 104 ```python def f104(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors or rate0 <= rate0 + 0: factors[name0] = rate0 items = [] for key, count in left[:20]: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f104([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 105 ```python def f105(edges, start, limit): edges = list(edges) children = {} for a, b in edges: 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 (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) listing = list(levels) edge = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': edge[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f105([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 106 ```python def f106(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) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < min(limit, 3): for nxt in sorted(routes.get(cur, [])): 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, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f106([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 107 ```python def f107(rows, keep, factors): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * abs(factors[group]) selected.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score amount += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f107([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 108 ```python def f108(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f108([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 109 ```python def f109(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(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 ``` ``` f109([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 110 ```python def f110(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + min(gap, 2): result.append([left, right]) else: result[-1][1] = right gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(min(100, 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 ``` ``` f110([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 111 ```python def f111(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[: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 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 ``` ``` f111(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 112 ```python def f112(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) finished = set() sequence = [] 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[:1] if any((str(node).isupper() for node in nodes)) else nextbatch layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in 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 ``` ``` f112(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 113 ```python def f113(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() listing = [] layers = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled)) 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) listing.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f113(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 114 ```python def f114(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, 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 ``` ``` f114([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 115 ```python def f115(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])) 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 ``` ``` f115(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 116 ```python def f116(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])) positions = [pos for pos, value in enumerate(totals) if value >= limit] growth = sum((1 for pos in range(1, len(totals)) if totals[pos] >= totals[pos - 1])) returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f116([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 117 ```python def f117(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() result = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available rounds.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f117(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 118 ```python def f118(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) levels = {start: 0} work = [start] while work: cur = work.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 work.append(nxt) listing = list(levels) lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f118([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 119 ```python def f119(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] = 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 ``` ``` f119([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 120 ```python def f120(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f120([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 121 ```python def f121(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in found and list(point) not in repeats: repeats.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (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 ``` ``` f121([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 122 ```python def f122(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap and rate0 != 0: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap and count > 0: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f122([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 123 ```python def f123(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) spaces = [] for pos in range(1, 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': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f123([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 124 ```python def f124(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 = 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, 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 ``` ``` f124([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 125 ```python def f125(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, 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 ``` ``` f125([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 126 ```python def f126(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 or len(word) > 12: 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 ``` ``` f126(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 127 ```python def f127(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] 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 ``` ``` f127([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 128 ```python def f128(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])) matches = [pos for pos, value in enumerate(series) if value >= limit] growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f128([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 129 ```python def f129(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(0, 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((min(100, 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 ``` ``` f129([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 130 ```python def f130(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): 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, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f130([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 131 ```python def f131(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) 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': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f131([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 132 ```python def f132(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) finished = set() result = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in 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 ``` ``` f132(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 133 ```python def f133(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[: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 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 ``` ``` f133(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 134 ```python def f134(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f134([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 135 ```python def f135(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' or (char == "'" and word): word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop or len(word) > 12: 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 ``` ``` f135(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 136 ```python def f136(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) 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 children[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 ``` ``` f136(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 137 ```python def f137(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[: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 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 ``` ``` f137(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 138 ```python def f138(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 = 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, 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 ``` ``` f138([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 139 ```python def f139(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap and rate0 != 0: ratemap[name0] = rate0 items = [] for key, count in left: if key in ratemap: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in ratemap}) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f139([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 140 ```python def f140(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f140([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 141 ```python def f141(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(min(count, 3 + (count - 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 ``` ``` f141([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 142 ```python def f142(rows, keep, prices): rows = list(rows) selected = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: cost = value * prices[group] selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f142([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 143 ```python def f143(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f143([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 144 ```python def f144(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 = [] 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[:1] if any((str(node).isupper() for node in nodes)) else available layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f144(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 145 ```python def f145(edges, start, limit): edges = list(edges) children = {} for a, b in edges: 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 {} 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 ``` ``` f145([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 146 ```python def f146(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(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f146([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 147 ```python def f147(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(series[:64]) if value >= limit and (limit >= 0 or pos > 0)] ups = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f147([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 148 ```python def f148(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(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 ``` ``` f148([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 149 ```python def f149(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] 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 ``` ``` f149([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 150 ```python def f150(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f150([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 151 ```python def f151(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} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < min(limit, 3): for nxt in sorted(nextmap.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) 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 ``` ``` f151([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 152 ```python def f152(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y = max(-64, min(64, y + dy)) used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, 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 ``` ``` f152([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 153 ```python def f153(rows, keep, factors): rows = list(rows) matches = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * factors[group] matches.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f153([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 154 ```python def f154(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 startdegree = dict(indegree) finished = set() sequence = [] layers = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if indegree[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]: 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 ``` ``` f154(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 155 ```python def f155(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left[:20]: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f155([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 156 ```python def f156(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 = [] groups = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available groups.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f156(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 157 ```python def f157(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch layers.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f157(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 158 ```python def f158(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(min(count, 64)): 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 = (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 ``` ``` f158([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 159 ```python def f159(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) 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 ``` ``` f159([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 160 ```python def f160(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in levels and (not nxt.startswith('Z')): 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[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f160([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 161 ```python def f161(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: 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 ``` ``` f161(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 162 ```python def f162(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f162([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 163 ```python def f163(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() 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=len(nodes) > 20) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f163(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 164 ```python def f164(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(series) if value >= limit] ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f164([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 165 ```python def f165(rows, keep, prices): rows = list(rows) kept = [] totals = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * prices[group] kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost grand += cost kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f165([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 166 ```python def f166(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f166([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 167 ```python def f167(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] < min(limit, 3): 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 ``` ``` f167([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 168 ```python def f168(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 items = [] for key, count in left: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f168([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 169 ```python def f169(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} 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) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f169([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 170 ```python def f170(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(combined[-1][1], right) gaps = [] for pos in range(1, min(50, 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 ``` ``` f170([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 171 ```python def f171(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, 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 ``` ``` f171([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 172 ```python def f172(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 + 1: 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 ``` ``` f172([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 173 ```python def f173(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap and rate0 != 0: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f173([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 174 ```python def f174(rows, keep, ratemap): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * abs(ratemap[group]) selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f174([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 175 ```python def f175(rows, keep, factors): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * factors[group] kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f175([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 176 ```python def f176(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(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 ``` ``` f176([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 177 ```python def f177(rows, keep, prices): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: cost = value * prices[group] matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f177([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 178 ```python def f178(lines, stop): lines = list(lines) tallies = {} 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 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 ``` ``` f178(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 179 ```python def f179(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f179([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 180 ```python def f180(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f180([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 181 ```python def f181(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[: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 ``` ``` f181(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 182 ```python def f182(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f182([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 183 ```python def f183(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] + gap % 5 + 1: 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 ``` ``` f183([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 184 ```python def f184(lines, stop): lines = list(lines) tallies = {} stop = {str(word) 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 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 ``` ``` f184(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 185 ```python def f185(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() listing = [] groups = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) 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: complete.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 ``` ``` f185(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 186 ```python def f186(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[:64]) 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 ``` ``` f186([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 187 ```python def f187(values, width, limit): values = list(values) size = width 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] 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': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f187([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 188 ```python def f188(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) 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 ``` ``` f188([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 189 ```python def f189(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 items = [] for key, count in left: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f189([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 190 ```python def f190(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 ``` ``` f190([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 191 ```python def f191(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' 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])) 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 ``` ``` f191(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 192 ```python def f192(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) handled = set() listing = [] layers = [] while len(handled) < len(nodes): 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 ``` ``` f192(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 193 ```python def f193(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(totals) if value >= limit] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f193([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 194 ```python def f194(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f194([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 195 ```python def f195(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap and rate0 != 0: ratemap[name0] = rate0 items = [] for key, count in left[:20]: if key in ratemap and count > 0: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f195([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 196 ```python def f196(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.append(list(point)) known.add(point) far = 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 ``` ``` f196([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 197 ```python def f197(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} if any((start in edge for edge in edges)) else {} work = [start] while work: cur = work.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 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 ``` ``` f197([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 198 ```python def f198(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 startdegree = 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=len(nodes) > 20) 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 children[node]: needs[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 ``` ``` f198(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 199 ```python def f199(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 ``` ``` f199([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 200 ```python def f200(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5: 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([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 201 ```python def f201(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(min(count, 3 + (count - 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 ``` ``` f201([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 202 ```python def f202(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f202([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 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: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = 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 layers.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f203(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 204 ```python def f204(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] >= windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f204([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 205 ```python def f205(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 % 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, 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 ``` ``` f205([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 206 ```python def f206(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] ups = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f206([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 207 ```python def f207(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 sorted(children.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.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 ``` ``` f207([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 208 ```python def f208(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: 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])) 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 ``` ``` f208(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 209 ```python def f209(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 rootsource = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) 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]: 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 ``` ``` f209(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 210 ```python def f210(spans, gap): spans = list(spans) segments = [] for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): 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, len(segments)): left = segments[pos][0] spaces.append(min(100, 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 ``` ``` f210([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 211 ```python def f211(rows, keep, ratemap): rows = list(rows) matches = [] groupmap = {} grand = 0 for code, group, state, value in rows: if state in keep: charge = value * abs(ratemap[group]) matches.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge grand += charge matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': grand, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, charge in matches)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f211([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 212 ```python def f212(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < min(limit, 3): for nxt in (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) 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 ``` ``` f212([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 213 ```python def f213(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * prices[group] selected.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score sumvalue += score selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f213([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 214 ```python def f214(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): if not combined or left > combined[-1][1] + 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': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f214([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 215 ```python def f215(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])) 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 sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f215(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 216 ```python def f216(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 = [] rounds = [] 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 rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f216(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 217 ```python def f217(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f217([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 218 ```python def f218(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - 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 ``` ``` f218([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 219 ```python def f219(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, 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 ``` ``` f219([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 220 ```python def f220(lines, stop): lines = list(lines) frequency = {} 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 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 ``` ``` f220(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 221 ```python def f221(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (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) 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 ``` ``` f221([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 222 ```python def f222(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[:64]: 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 ``` ``` f222(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 223 ```python def f223(spans, gap): spans = list(spans) segments = [] for left, right in sorted(set(spans)): if not segments or left > segments[-1][1] + 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 ``` ``` f223([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 224 ```python def f224(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(min(count, 3 + (count - 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, 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 ``` ``` f224([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 225 ```python def f225(rows, keep, ratemap): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * ratemap[group] selected.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f225([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 226 ```python def f226(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f226([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 227 ```python def f227(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.setdefault(a, []).append(b) distance = {start: 0} if limit > 0 or limit < 0 else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in sorted(routes.get(cur, [])): 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 ``` ``` f227([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 228 ```python def f228(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(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) 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 ``` ``` f228([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 229 ```python def f229(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(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': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f229([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 230 ```python def f230(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 ``` ``` f230([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 231 ```python def f231(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[:64]) if value >= limit] growth = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f231([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 232 ```python def f232(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] 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 ``` ``` f232([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 233 ```python def f233(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f233([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 234 ```python def f234(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f234([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 235 ```python def f235(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) 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 ``` ``` f235([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 236 ```python def f236(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` ``` f236([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 237 ```python def f237(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) levels = {start: 0} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) result = list(levels) edge = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': result, 'depth': levels, 'frontier': edge, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f237([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 238 ```python def f238(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 rootsource = dict(incoming) finished = set() listing = [] 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) 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 rootsource[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f238(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 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 initial = dict(needs) finished = set() listing = [] layers = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) 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 routes[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 ``` ``` f239(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 240 ```python def f240(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) indegree[b] += 1 initial = 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[: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 routes[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 ``` ``` f240(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 241 ```python def f241(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(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((min(100, 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 ``` ``` f241([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 242 ```python def f242(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): nextmap.setdefault(a, []).append(b) steps = {start: 0} pending = [start] while pending: cur = pending.pop(0) if steps[cur] < limit: for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in steps: steps[nxt] = steps[cur] + 1 pending.append(nxt) sequence = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f242([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors or rate0 <= rate0 + 0: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f243([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 244 ```python def f244(lines, stop): lines = list(lines) frequency = {} stop = {str(word) 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: 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 ``` ``` f244(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 245 ```python def f245(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left >= combined[-1][1] + gap % 5: 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 ``` ``` f245([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 246 ```python def f246(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) finished = set() listing = [] groups = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not available: break current = available groups.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in nextmap[node]: needs[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f246(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 247 ```python def f247(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} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < min(limit, 3): for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) 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 ``` ``` f247([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 248 ```python def f248(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value != 0: charge = value * ratemap[group] selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge grand += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f248([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 249 ```python def f249(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in ratemap}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f249([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 250 ```python def f250(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} if any((start in edge for edge in edges)) else {} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(routes.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f250([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 251 ```python def f251(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 ``` ``` f251([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 252 ```python def f252(rows, keep, ratemap): rows = list(rows) kept = [] totals = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: charge = value * abs(ratemap[group]) kept.append((code, charge)) totals[group] = totals.get(group, 0) + charge grand += charge kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f252([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 253 ```python def f253(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() result = [] rounds = [] 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 rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f253(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 254 ```python def f254(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges: nextmap.setdefault(a, []).append(b) distance = {start: 0} if limit > 0 or limit < 0 else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(nextmap.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.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 ``` ``` f254([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 255 ```python def f255(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 rootsource = dict(indegree) finished = set() sequence = [] rounds = [] 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 rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f255(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 256 ```python def f256(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() listing = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=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: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f256(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 257 ```python def f257(values, width, limit): values = list(values) size = width 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] ups = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f257([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 258 ```python def f258(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f258([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 259 ```python def f259(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 startdegree = dict(needs) complete = set() listing = [] groups = [] while len(complete) < len(nodes): 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 groups.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f259(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 260 ```python def f260(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 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': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f260(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 261 ```python def f261(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} 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 ``` ``` f261([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### 262 ```python def f262(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) incoming[b] += 1 rootsource = dict(incoming) handled = set() listing = [] rounds = [] 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)) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available rounds.append(current) for node in current: handled.add(node) listing.append(node) for node in current: for nxt in routes[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 ``` ``` f262(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 263 ```python def f263(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(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 ``` ``` f263([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 264 ```python def f264(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, 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 ``` ``` f264([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 265 ```python def f265(rows, keep, factors): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * factors[group] kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f265([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 266 ```python def f266(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 ``` ``` f266([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 267 ```python def f267(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 ``` ``` f267([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 268 ```python def f268(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 combined = [] for key, count in left[:20]: if key in ratemap: combined.append((key, count * ratemap[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in ratemap}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f268([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 269 ```python def f269(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f269([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 270 ```python def f270(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, []))[:20]: 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 ``` ``` f270([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 271 ```python def f271(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) 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 ``` ``` f271([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 272 ```python def f272(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} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < limit: for nxt in sorted(routes.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) result = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': result, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f272([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 273 ```python def f273(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - 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 = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f273([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 274 ```python def f274(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.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] < limit: 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 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[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f274([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 275 ```python def f275(spans, gap): spans = list(spans) segments = [] for left, right in sorted(set(spans)): if not segments or left > segments[-1][1] + min(gap, 2): segments.append([left, right]) else: 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 ``` ``` f275([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 276 ```python def f276(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * abs(ratemap[group]) kept.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge amount += charge kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f276([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 277 ```python def f277(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 = 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 ``` ``` f277([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 278 ```python def f278(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) steps = {start: 0} if limit > 0 or limit < 0 else {} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < limit: for nxt in sorted(nextmap.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) sequence = list(steps) 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 ``` ``` f278([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 279 ```python def f279(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f279([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 280 ```python def f280(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in steps and (not nxt.startswith('Z')): steps[nxt] = steps[cur] + 1 queue.append(nxt) listing = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f280([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 281 ```python def f281(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[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 sorteditems = 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 sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f281(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 282 ```python def f282(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f282([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 283 ```python def f283(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) 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 ``` ``` f283([(2, 5), (5, 7), (14, 21), (18, 23), (28, 30), (33, 39), (43, 49), (48, 53), (59, 65)], 1) ``` ``` {"cover":38,"holes":[7,5,3,4,6],"longest":10,"segments":[[2,7],[14,23],[28,30],[33,39],[43,53],[59,65]]} ``` ### 284 ```python def f284(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: a, b = (before, after) routes[a].append(b) indegree[b] += 1 initial = dict(indegree) complete = set() sequence = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: nextbatch = 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 nextbatch: break current = nextbatch layers.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in routes[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 ``` ``` f284(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["auth","cache","db","jobs","api","proxy","worker","ui"],"roots":["auth","cache","db","jobs"]} ``` ### 285 ```python def f285(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) 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 ``` ``` f285([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 286 ```python def f286(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f286([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["BR",43],["DL",43],["FP",43],["GQ",33]],"missing":["HR"],"total":335} ``` ### 287 ```python def f287(rows, keep, prices): rows = list(rows) kept = [] groupmap = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * abs(prices[group]) kept.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost grand += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f287([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 288 ```python def f288(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: 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 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 ``` ``` f288(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 289 ```python def f289(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' or (char == "'" and word): word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop or len(word) > 12: 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': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` ``` f289(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 290 ```python def f290(rows, keep, prices): rows = list(rows) matches = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: cost = value * prices[group] matches.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f290([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 291 ```python def f291(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])) 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 ``` ``` f291(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 292 ```python def f292(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 {} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f292([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 293 ```python def f293(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 startdegree = dict(indegree) finished = set() listing = [] rounds = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) 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 nextmap[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f293(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 294 ```python def f294(rows, keep, prices): rows = list(rows) matches = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: score = value * prices[group] matches.append((code, score)) totals[group] = totals.get(group, 0) + score sumvalue += score matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f294([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 295 ```python def f295(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 and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 pending.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 ``` ``` f295([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 296 ```python def f296(lines, stop): lines = list(lines) wordmap = {} 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: 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': min(256, sum((len(word) * count for word, count in wordmap.items())))} output = dict() output.update(returnvalue) return output ``` ``` f296(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['helium', 'amber']) ``` ``` {"counts":{"blue":10,"cyan":6,"ember":3,"frost":3,"garnet":2},"initials":{"b":10,"c":6,"e":3,"f":3,"g":2},"size":106,"top":["blue","cyan","ember","frost","garnet"]} ``` ### 297 ```python def f297(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 = max(-64, min(64, x + dx)) y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f297([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 298 ```python def f298(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] growth = sum((1 for pos in range(1, 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 ``` ``` f298([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 299 ```python def f299(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] ups = 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': ups} output = dict() output.update(returnvalue) return output ``` ``` f299([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 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: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) 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: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f300(['ui', 'proxy', 'auth', 'cache', 'worker', 'db', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy')]) ``` ``` {"batches":[["auth","cache","db","worker"],["api","jobs","proxy"],["ui"]],"count":8,"order":["auth","cache","db","worker","api","jobs","proxy","ui"],"roots":["auth","cache","db","worker"]} ``` ### 301 ```python def f301(rows, keep, prices): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: score = value * prices[group] kept.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f301([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 302 ```python def f302(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(series) if value >= limit] growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f302([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 303 ```python def f303(rows, keep, ratemap): rows = list(rows) selected = [] groupmap = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * abs(ratemap[group]) selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost grand += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f303([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 304 ```python def f304(rows, keep, ratemap): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * ratemap[group] kept.append((code, score)) totals[group] = totals.get(group, 0) + score sumvalue += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f304([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 305 ```python def f305(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)] growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f305([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 306 ```python def f306(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 += 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 ``` ``` f306([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 307 ```python def f307(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} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) 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 ``` ``` f307([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 308 ```python def f308(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(0, 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 ``` ``` f308([(2, 5), (5, 7), (14, 18), (18, 23), (28, 30), (33, 39), (43, 49), (48, 55), (59, 65)], 1) ``` ``` {"cover":40,"holes":[7,5,3,4,4],"longest":12,"segments":[[2,7],[14,23],[28,30],[33,39],[43,55],[59,65]]} ``` ### 309 ```python def f309(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[: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 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 ``` ``` f309(['Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue', 'Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan'], ['garnet', 'helium']) ``` ``` {"counts":{"amber":3,"blue":10,"cyan":6,"ember":3,"frost":3},"initials":{"a":3,"b":10,"c":6,"e":3,"f":3},"size":109,"top":["blue","cyan","amber","ember","frost"]} ``` ### 310 ```python def f310(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, 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 ``` ``` f310([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 311 ```python def f311(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) 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 ``` ``` f311([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], '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"]} ``` ### 312 ```python def f312(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] 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 ``` ``` f312([5, 23, 12, 4, 12, 20, 9, 16, 10, 21, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":78,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":6,"sums":[44,51,48,45,57,55,56,63,64,78,71,65]} ``` ### 313 ```python def f313(rows, keep, factors): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * factors[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f313([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":102,"north":168,"south":15},"top":["AN29","AN77","AN03"],"total":593} ``` ### 314 ```python def f314(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(totals) if value >= limit and (limit >= 0 or pos > 0)] 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': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f314([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 315 ```python def f315(rows, keep, prices): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * prices[group] kept.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f315([('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3), ('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":6,"groups":{"core":308,"edge":90,"north":189,"south":15},"top":["AN29","AN77","AN03"],"total":602} ``` ### 316 ```python def f316(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): nextmap.setdefault(a, []).append(b) steps = {start: 0} if limit > 0 or limit < 0 else {} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < min(limit, 3): for nxt in sorted(nextmap.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) sequence = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f316([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 1) ``` ``` {"count":3,"depth":{"A":0,"B":1,"C":1},"frontier":["B","C"],"order":["A","B","C"]} ``` ### 317 ```python def f317(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.append(list(point)) known.add(point) far = 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 ``` ``` f317([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1)]) ``` ``` {"bounds":[0,0,6,8],"end":[2,6],"far":10,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":30} ``` ### 318 ```python def f318(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f318([('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12), ('FP', 8), ('GQ', 10), ('HR', 6)], [('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4), ('EV', 10), ('FP', 5), ('GQ', 3)], 3) ``` ``` {"count":7,"items":[["EV",123],["DL",43],["FP",43],["AN",38],["BR",35]],"missing":["HR"],"total":342} ``` ### 319 ```python def f319(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit] 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 ``` ``` f319([5, 23, 12, 4, 12, 20, 9, 19, 10, 16, 16, 17, 24, 14, 10], 4, 48) ``` ``` {"best":73,"hits":[1,2,4,5,6,7,8,9,10,11],"rises":4,"sums":[44,51,48,45,60,58,54,61,59,73,71,65]} ``` ### 320 ```python def f320(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, 3 + (count - count))): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f320([('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2), ('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2)]) ``` ``` {"bounds":[0,0,6,8],"end":[4,7],"far":11,"revisits":[[3,4],[3,3],[5,4],[4,4],[6,4]],"steps":31} ``` ### Final Query Give me the output of the final query without executing it! ```python import json, re from collections import deque out = {} values = list((5, 23, 12, 4, 12, 20) + (9, 16, 10, 21, 16, 17, 24, 14, 10)) width = 4 limit = 48 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} token = record['bb'] spans = list(((2, 5), (5, 7), (14, 21), (18, 23), (28, 30)) + ((33, 39), (43, 49), (48, 53), (59, 65))) gap = 1 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))} memo = record['cc'] edges = list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'))) start = 'A' route = token cases = { 78: 3, 73: 1, } limit = 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)} clue = record['dd'] keep = list(('ready',) + ('checked',)) rates = dict((('north', 7),) + (('south', 5), ('core', 11), ('edge', 3))) route = memo cases = { 40: list((('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3)) + (('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 21), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 27), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7))), 38: list((('BR70', 'north', 'void', 8), ('BR07', 'north', 'void', 7), ('BR44', 'south', 'hold', 11), ('BR81', 'edge', 'hold', 15), ('AN18', 'core', 'ready', 5), ('CX55', 'south', 'checked', 3)) + (('CX92', 'core', 'hold', 16), ('AN29', 'core', 'ready', 23), ('BR66', 'edge', 'void', 8), ('AN03', 'edge', 'checked', 25), ('CX40', 'north', 'void', 24), ('AN77', 'north', 'ready', 24), ('DL14', 'edge', 'ready', 9), ('AN51', 'edge', 'void', 7))), } rows = cases[route] chosen = [(code, group, value) for code, group, state, value in rows if state in keep] priced = [(code, group, value * rates[group]) for code, group, value in chosen] groups = {} for code, group, price in priced: groups[group] = groups.get(group, 0) + price ranked = sorted(priced, key=lambda row: (-row[2], row[0])) record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups} stamp = record['bb'] left = list((('AN', 5), ('BR', 8), ('CX', 4), ('DL', 10), ('EV', 12)) + (('FP', 8), ('GQ', 10), ('HR', 6))) fee = 3 route = (token, memo) cases = { (78, 38): list((('AN', 7), ('BR', 4), ('CX', 6), ('DL', 4)) + (('EV', 10), ('FP', 5), ('GQ', 3))), (78, 40): list((('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4)) + (('EV', 10), ('FP', 5), ('GQ', 3))), (73, 38): list((('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4)) + (('EV', 10), ('FP', 5), ('GQ', 3))), (73, 40): list((('AN', 4), ('BR', 5), ('CX', 6), ('DL', 4)) + (('EV', 10), ('FP', 5), ('GQ', 3))), } right = cases[route] joined = [] for key, count in left: matches = [rate for name, rate in right if name == key] if matches: joined.append([key, count * matches[0] + fee]) joined.sort(key=lambda item: (-item[1], item[0])) rightkeys = {key for key, rate in right} record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)} ticket = record['bb'] nodes = list(('ui', 'proxy', 'auth', 'cache') + ('worker', 'db', 'api', 'jobs')) route = (clue, ticket) cases = { (3, 335): list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy'))), (3, 342): list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy'))), (8, 335): list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('auth', 'proxy'))), (8, 342): list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('jobs', 'worker'), ('auth', 'proxy'))), } 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)} badge = record['aa'][3] route = (stamp, badge) cases = { (593, 'jobs'): list((('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2)) + (('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('S', 1))), (593, 'worker'): list((('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2)) + (('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2))), (602, 'jobs'): list((('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2)) + (('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 2))), (602, 'worker'): list((('N', 2), ('E', 3), ('N', 3), ('S', 2), ('N', 1), ('E', 1), ('E', 2)) + (('W', 2), ('E', 2), ('S', 1), ('W', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 1), ('E', 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} permit = record['bb'] lines = list(('Cyan, amber. garnet blue', 'Ember, blue. blue cyan ember', 'Blue, blue. amber blue', 'Amber, cyan. helium garnet blue') + ('Helium, blue. cyan cyan', 'Blue, ember. helium frost helium', 'Blue, frost. frost cyan')) route = permit cases = { 11: list(('helium',) + ('amber',)), 10: list(('garnet',) + ('helium',)), } stop = cases[route] 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))} out['aa'] = record['aa'] out['bb'] = record['bb'] out['cc'] = record['cc'] out['dd'] = record['dd'] print(json.dumps(out, separators=(",", ":"), sort_keys=True)) ```