### 001 ```python def f1(rows, keep, prices): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: 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(kept), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f1([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 002 ```python def f2(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper() ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f2(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 003 ```python def f3(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, min(65, len(series))) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f3([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 004 ```python def f4(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[:20]: if key in prices and count > 0: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f4([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 005 ```python def f5(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue work.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f5(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 006 ```python def f6(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) indegree[b] += 1 startdegree = dict(indegree) finished = set() sequence = [] rounds = [] 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 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 startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f6(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 007 ```python def f7(spans, gap): spans = list(spans) segments = [] for left, right in sorted(set(spans)): if not segments or left > segments[-1][1] + max(0, gap): 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 ``` ``` f7([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 008 ```python def f8(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) positions = [pos for pos, value in enumerate(totals) if value >= limit] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f8([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 009 ```python def f9(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() listing = [] rounds = [] 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 rounds.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f9(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 010 ```python def f10(left, right, banned): left = list(left) def normalize(value): return str(value).strip().upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f10(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 011 ```python def f11(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 ``` ``` f11([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 012 ```python def f12(rows, keep, prices): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * prices[group] matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f12([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 013 ```python def f13(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])) 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 ``` ``` f13([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 014 ```python def f14(left, right, banned): left = list(left) def canon(value): return str(value).strip(' \n').upper() banned = {str(value).strip() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f14(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 015 ```python def f15(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 ``` ``` f15([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 016 ```python def f16(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + max(0, gap): result.append([left, right]) else: result[-1][1] = right gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` ``` f16([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 017 ```python def f17(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = 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 ``` ``` f17([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 018 ```python def f18(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f18(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 019 ```python def f19(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] = 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 ``` ``` f19([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 020 ```python def f20(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(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, min(50, len(combined))): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f20([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 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: a, b = (before, after) children[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=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f21(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 022 ```python def f22(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors and rate0 != 0: factors[name0] = rate0 matched = [] for key, count in left: if key in factors and count > 0: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f22([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 023 ```python def f23(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap or rate0 <= rate0 + 0: ratemap[name0] = rate0 items = [] for key, count in left[:20]: if key in ratemap: 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 ``` ``` f23([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 024 ```python def f24(left, right, banned): left = list(left) def normalize(value): return str(value).strip().upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f24(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 025 ```python def f25(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] = 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 ``` ``` f25([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 026 ```python def f26(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' ').upper() excluded = {str(value).strip() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f26(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 027 ```python def f27(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f27(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 028 ```python def f28(rows, keep, factors): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * factors[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(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f28([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 029 ```python def f29(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])) matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit and (limit >= 0 or pos > 0)] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f29([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 030 ```python def f30(board): board = list(board) known = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in known: continue cells = [] pending = [(row, col)] known.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue pending.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f30(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 031 ```python def f31(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' ').upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f31(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 032 ```python def f32(rows, keep, prices): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep: cost = value * prices[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost grand += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f32([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 033 ```python def f33(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': min(256, max(windows)), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f33([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 034 ```python def f34(board): board = list(board) visited = set() regions = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) pending.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f34(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 035 ```python def f35(rows, keep, factors): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * factors[group] kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f35([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 036 ```python def f36(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: 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 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 ``` ``` f36(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 037 ```python def f37(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 ``` ``` f37([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 038 ```python def f38(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().upper() excluded = {str(value).upper() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f38(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 039 ```python def f39(rows, keep, prices): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * prices[group] selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f39([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 040 ```python def f40(rows, keep, ratemap): rows = list(rows) matches = [] groupmap = {} sumvalue = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if str(state).lower() in {str(item).lower() for item in keep}: cost = value * abs(ratemap[group]) matches.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost sumvalue += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f40([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 041 ```python def f41(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * prices[group] selected.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f41([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 042 ```python def f42(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * abs(ratemap[group]) kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f42([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 043 ```python def f43(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 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 ``` ``` f43([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 044 ```python def f44(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 items = [] for key, count in left: if key in ratemap: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f44([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 045 ```python def f45(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 rootsource = 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)) 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 rootsource[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f45(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 046 ```python def f46(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if state 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], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f46([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 047 ```python def f47(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap or rate0 <= rate0 + 0: ratemap[name0] = rate0 combined = [] for key, count in left[:20]: if key in ratemap and count > 0: combined.append((key, count * ratemap[key] + fee)) combined.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 combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f47([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 048 ```python def f48(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 ``` ``` f48([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 049 ```python def f49(board): board = list(board) known = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] pending = [(row, col)] known.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) pending.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f49(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 050 ```python def f50(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value >= limit] 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': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f50([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 051 ```python def f51(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit and (limit >= 0 or pos > 0)] 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': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f51([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 052 ```python def f52(left, right, banned): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f52(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 053 ```python def f53(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' \r').upper()[:12] excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - excluded b = {tidy(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f53(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 054 ```python def f54(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f54(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 055 ```python def f55(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' ').upper() ignored = {str(value).strip() for value in ignored} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f55(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 056 ```python def f56(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(windows) if value >= limit] growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f56([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 057 ```python def f57(board): board = list(board) visited = set() regions = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue pending.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f57(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 058 ```python def f58(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices and 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])) 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 ``` ``` f58([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 059 ```python def f59(left, right, excluded): left = list(left) def canon(value): return ' '.join(str(value).split()).upper() excluded = {str(value).upper() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f59(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 060 ```python def f60(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \r').upper() excluded = {str(value).strip() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right if normalize(value) != ''} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f60(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 061 ```python def f61(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' ').upper()[:12] excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f61(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 062 ```python def f62(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])) 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 ``` ``` f62([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 063 ```python def f63(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(0, combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((min(100, 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 ``` ``` f63([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 064 ```python def f64(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) work.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f64(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 065 ```python def f65(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) complete = set() listing = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available layers.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f65(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 066 ```python def f66(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 or len(word) > 12: 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': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` ``` f66(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 067 ```python def f67(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop or len(word) > 12: 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 ``` ``` f67(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 068 ```python def f68(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' \n').upper() excluded = {str(value).strip() for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f68(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 069 ```python def f69(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] indegree = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[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)) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f69(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 070 ```python def f70(rows, keep, factors): rows = list(rows) kept = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep: score = value * abs(factors[group]) kept.append((code, score)) totals[group] = totals.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f70([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 071 ```python def f71(rows, keep, prices): rows = list(rows) kept = [] groupmap = {} grand = 0 for code, group, state, value in rows: if state in keep: score = value * abs(prices[group]) kept.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': groupmap, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f71([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 072 ```python def f72(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) complete = set() sequence = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) 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]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f72(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 073 ```python def f73(board): board = list(board) found = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue queue.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f73(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 074 ```python def f74(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: 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 ``` ``` f74([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 075 ```python def f75(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 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 ``` ``` f75(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 076 ```python def f76(rows, keep, prices): rows = list(rows) matches = [] totals = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if str(state).lower() in {str(item).lower() for item in keep}: charge = value * prices[group] matches.append((code, charge)) totals[group] = totals.get(group, 0) + charge grand += charge matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, charge in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f76([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 077 ```python def f77(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(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 ``` ``` f77([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 078 ```python def f78(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) 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': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f78([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 079 ```python def f79(rows, keep, prices): rows = list(rows) kept = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * prices[group] kept.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f79([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 080 ```python def f80(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, 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 ``` ``` f80([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 081 ```python def f81(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])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f81([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 082 ```python def f82(lines, stop): lines = list(lines) tallies = {} 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 or len(word) > 12: 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 ``` ``` f82(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 083 ```python def f83(left, right, banned): left = list(left) def tidy(value): return str(value).strip(' \n').upper() banned = {str(value).upper() for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f83(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 084 ```python def f84(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' 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) < 2: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` ``` f84(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 085 ```python def f85(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) 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 ``` ``` f85([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 086 ```python def f86(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 ``` ``` f86([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 087 ```python def f87(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 startdegree = 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)) 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 startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f87(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 088 ```python def f88(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' ').upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f88(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 089 ```python def f89(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 = [] 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)) 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]: 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 ``` ``` f89(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 090 ```python def f90(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not available: break current = available rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f90(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 091 ```python def f91(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * prices[group] selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f91([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 092 ```python def f92(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(matched)} output = dict() output.update(returnvalue) return output ``` ``` f92([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 093 ```python def f93(rows, keep, factors): 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}: charge = value * abs(factors[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 ``` ``` f93([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 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: 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(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 095 ```python def f95(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left[:20]: if key in factors: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f95([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 096 ```python def f96(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])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f96([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 097 ```python def f97(left, right, banned): left = list(left) def canon(value): return str(value).strip(' ').upper() banned = {str(value).upper() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f97(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 098 ```python def f98(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 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 ``` ``` f98([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 099 ```python def f99(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 and (limit >= 0 or pos > 0)] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f99([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 100 ```python def f100(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 rootsource = dict(incoming) finished = set() sequence = [] groups = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch 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 rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f100(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 101 ```python def f101(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \r').upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f101(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 102 ```python def f102(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * abs(ratemap[group]) kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': buckets, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f102([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 103 ```python def f103(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 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 ``` ``` f103(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 104 ```python def f104(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] 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 ``` ``` f104([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 105 ```python def f105(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 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 ``` ``` f105(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 106 ```python def f106(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, min(50, len(combined))): left = combined[pos][0] gaps.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f106([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 107 ```python def f107(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().upper()[:12] excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f107(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 108 ```python def f108(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[:64]: 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 ``` ``` f108(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 109 ```python def f109(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors and rate0 != 0: factors[name0] = rate0 matched = [] for key, count in left: if key in factors and count > 0: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) 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 ``` ``` f109([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 110 ```python def f110(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 startdegree = dict(indegree) handled = set() sequence = [] groups = [] while len(handled) < len(nodes): available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled)) if not available: break current = available groups.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in routes[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f110(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 111 ```python def f111(left, right, ignored): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() ignored = {str(value).strip() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f111(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 112 ```python def f112(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(combined)} output = dict() output.update(returnvalue) return output ``` ``` f112([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 113 ```python def f113(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[:64]) if value >= limit] increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f113([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 114 ```python def f114(board): board = list(board) visited = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f114(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 115 ```python def f115(lines, stop): lines = list(lines) tallies = {} stop = {str(word) 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 tallies[word] = tallies.get(word, 0) + 1 sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f115(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 116 ```python def f116(rows, keep, ratemap): rows = list(rows) kept = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * abs(ratemap[group]) kept.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge amount += charge kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f116([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 117 ```python def f117(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 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 ``` ``` f117(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 118 ```python def f118(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 ``` ``` f118([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 119 ```python def f119(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] ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f119([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 120 ```python def f120(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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 ``` ``` f120([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 121 ```python def f121(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) needs[b] += 1 initial = dict(needs) complete = set() sequence = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in routes[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f121(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 122 ```python def f122(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(matched)} output = dict() output.update(returnvalue) return output ``` ``` f122([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 123 ```python def f123(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 ``` ``` f123([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 124 ```python def f124(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(windows) if value > limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f124([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 125 ```python def f125(rows, keep, ratemap): rows = list(rows) matches = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * ratemap[group] matches.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f125([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 126 ```python def f126(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 and count > 0: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f126([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 127 ```python def f127(board): board = list(board) known = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f127(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 128 ```python def f128(lines, stop): lines = list(lines) wordmap = {} 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' 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: continue wordmap[word] = wordmap.get(word, 0) + 1 sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f128(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 129 ```python def f129(rows, keep, prices): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: score = value * prices[group] kept.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f129([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 130 ```python def f130(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 ``` ``` f130(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 131 ```python def f131(left, right, ignored): left = list(left) def tidy(value): return str(value).strip(' \n').upper() ignored = {str(value).strip() for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f131(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 132 ```python def f132(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f132(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 133 ```python def f133(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text[: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 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 ``` ``` f133(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 134 ```python def f134(board): board = list(board) found = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f134(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 135 ```python def f135(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) 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 ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f135(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 136 ```python def f136(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f136(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 137 ```python def f137(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 and (limit >= 0 or pos > 0)] growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f137([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 138 ```python def f138(left, right, banned): left = list(left) def canon(value): return str(value).strip(' ').upper() banned = {str(value).strip() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f138(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 139 ```python def f139(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[: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 ``` ``` f139(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 140 ```python def f140(rows, keep, factors): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: score = value * abs(factors[group]) selected.append((code, score)) totals[group] = totals.get(group, 0) + score sumvalue += score selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f140([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 141 ```python def f141(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) handled = set() sequence = [] layers = [] while len(handled) < len(nodes): available = 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 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]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f141(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 142 ```python def f142(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] + min(gap, 2): 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 ``` ``` f142([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 143 ```python def f143(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([0] + windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f143([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 144 ```python def f144(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f144([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 145 ```python def f145(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * ratemap[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 ``` ``` f145([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 146 ```python def f146(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 rootsource = dict(incoming) finished = set() sequence = [] layers = [] while len(finished) < len(nodes): 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) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f146(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 147 ```python def f147(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: 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': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f147([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 148 ```python def f148(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() sequence = [] rounds = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f148(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 149 ```python def f149(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 items = [] for key, count in left: if key in ratemap and count > 0: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f149([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 150 ```python def f150(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 startdegree = dict(incoming) finished = set() listing = [] 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=len(nodes) > 20) 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 routes[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f150(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 151 ```python def f151(board): board = list(board) found = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in found: continue cells = [] work = [(row, col)] found.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) work.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f151(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 152 ```python def f152(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue work.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f152(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 153 ```python def f153(rows, keep, prices): rows = list(rows) selected = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * prices[group] selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f153([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 154 ```python def f154(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue work.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f154(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 155 ```python def f155(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop or len(word) > 12: continue wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f155(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 156 ```python def f156(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[:64]: if word in stop: continue frequency[word] = min(9, 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 ``` ``` f156(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 157 ```python def f157(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 rootsource = 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=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 children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f157(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 158 ```python def f158(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(matched)} output = dict() output.update(returnvalue) return output ``` ``` f158([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 159 ```python def f159(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(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 ``` ``` f159([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 160 ```python def f160(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) 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 ``` ``` f160([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 161 ```python def f161(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' 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: 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': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` ``` f161(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 162 ```python def f162(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[:64]) if value >= limit and (limit >= 0 or pos > 0)] 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 ``` ``` f162([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 163 ```python def f163(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 = [] 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(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 164 ```python def f164(rows, keep, ratemap): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * abs(ratemap[group]) kept.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f164([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 165 ```python def f165(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) 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 ``` ``` f165([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 166 ```python def f166(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f166([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 167 ```python def f167(left, right, ignored): left = list(left) def canon(value): return str(value).strip(' ').upper() ignored = {canon(value) for value in ignored} a = {canon(value) for value in left} - ignored b = {canon(value) for value in right if canon(value) != ''} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f167(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 168 ```python def f168(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 ``` ``` f168(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 169 ```python def f169(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) 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 ``` ``` f169([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 170 ```python def f170(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 sorteditems = 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 sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f170(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 171 ```python def f171(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f171(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 172 ```python def f172(lines, stop): lines = list(lines) wordmap = {} 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 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 ``` ``` f172(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 173 ```python def f173(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, 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 ``` ``` f173([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 174 ```python def f174(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) pending.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f174(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 175 ```python def f175(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(series) if value >= limit] increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f175([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 176 ```python def f176(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) complete = set() sequence = [] rounds = [] while len(complete) < len(nodes) and len(complete) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in 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 ``` ``` f176(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 177 ```python def f177(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 rootsource = 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)) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch rounds.append(current) for node in current: 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 rootsource[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f177(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 178 ```python def f178(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])) 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 ``` ``` f178([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 179 ```python def f179(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue work.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f179(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 180 ```python def f180(left, right, ignored): left = list(left) def canon(value): return str(value).strip(' ').upper()[:12] ignored = {canon(value) for value in ignored} a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - ignored b = {canon(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f180(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 181 ```python def f181(left, right, banned): left = list(left) def normalize(value): return str(value).strip().upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned b = {normalize(value) for value in right if normalize(value) != ''} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f181(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 182 ```python def f182(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) 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 ``` ``` f182([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 183 ```python def f183(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] indegree = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) indegree[b] += 1 initial = dict(indegree) complete = set() result = [] rounds = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in nextmap[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 ``` ``` f183(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 184 ```python def f184(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else 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 ``` ``` f184(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 185 ```python def f185(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap or rate0 <= rate0 + 0: 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({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f185([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 186 ```python def f186(board): board = list(board) visited = set() regions = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f186(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 187 ```python def f187(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[: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 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 ``` ``` f187(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 188 ```python def f188(board): board = list(board) found = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f188(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 189 ```python def f189(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(totals) if value >= limit] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f189([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 190 ```python def f190(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' \n').upper() ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f190(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 191 ```python def f191(spans, gap): spans = list(spans) segments = [] for left, right in sorted(set(spans)): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = max(0, 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': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f191([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 192 ```python def f192(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[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = 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 sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f192(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 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(totals), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f193([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 194 ```python def f194(spans, gap): spans = list(spans) result = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = right 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': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` ``` f194([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 195 ```python def f195(board): board = list(board) visited = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) work.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f195(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 196 ```python def f196(left, right, banned): left = list(left) def tidy(value): return str(value).strip(' ').upper() banned = {tidy(value) for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right if tidy(value) != ''} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f196(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 197 ```python def f197(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[: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 ``` ``` f197(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 198 ```python def f198(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 ``` ``` f198([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 199 ```python def f199(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: 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 ``` ``` f199(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 200 ```python def f200(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(items)} output = dict() output.update(returnvalue) return output ``` ``` f200([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 201 ```python def f201(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if str(state).lower() in {str(item).lower() for item in keep}: charge = value * 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': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f201([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 202 ```python def f202(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f202([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 203 ```python def f203(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \n').upper()[:12] excluded = {str(value).strip() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f203(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 204 ```python def f204(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} items = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: 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 ``` ``` f204(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 205 ```python def f205(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = right 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 ``` ``` f205([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 206 ```python def f206(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])) 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 ``` ``` f206([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 207 ```python def f207(board): board = list(board) found = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f207(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 208 ```python def f208(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])) indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f208([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 209 ```python def f209(board): board = list(board) visited = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f209(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 210 ```python def f210(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() result = [] layers = [] while len(complete) < len(nodes): nextbatch = 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 nextbatch: break current = nextbatch layers.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f210(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 211 ```python def f211(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(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 ``` ``` f211([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 212 ```python def f212(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = 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 ``` ``` f212([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 213 ```python def f213(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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 ``` ``` f213([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 214 ```python def f214(left, right, banned): left = list(left) def canon(value): return str(value).strip(' \r').upper() banned = {str(value).strip() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f214(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 215 ```python def f215(board): board = list(board) known = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] pending = [(row, col)] known.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue pending.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f215(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 216 ```python def f216(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[: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': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` ``` f216(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 217 ```python def f217(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left[:20]: if key in factors and count > 0: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f217([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 218 ```python def f218(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = right gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((min(100, 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 ``` ``` f218([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 219 ```python def f219(left, right, ignored): left = list(left) def canon(value): return str(value).strip(' \n').upper() ignored = {canon(value) for value in ignored} a = {canon(value) for value in left} - ignored b = {canon(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f219(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 220 ```python def f220(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) 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 ``` ``` f220([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 221 ```python def f221(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap or rate0 <= 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])) unmatched = 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': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f221([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 222 ```python def f222(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(0, combined[-1][1], right) breaks = [] for pos in range(1, len(combined)): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f222([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 223 ```python def f223(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value > limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f223([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 224 ```python def f224(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = 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 ``` ``` f224([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 225 ```python def f225(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * abs(ratemap[group]) selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost grand += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f225([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 226 ```python def f226(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: score = value * abs(prices[group]) selected.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f226([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 227 ```python def f227(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])) unmatched = 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': unmatched, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f227([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 228 ```python def f228(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f228(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 229 ```python def f229(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]))) 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(series), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f229([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 230 ```python def f230(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value > limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f230([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 231 ```python def f231(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) needs[b] += 1 rootsource = dict(needs) complete = set() listing = [] groups = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available groups.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in children[node]: needs[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 ``` ``` f231(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 232 ```python def f232(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) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f232(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 233 ```python def f233(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f233(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 234 ```python def f234(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f234(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 235 ```python def f235(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) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f235([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 236 ```python def f236(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:20]: text = line.lower() word = '' for char in text: 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) < 2: 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 ``` ``` f236(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 237 ```python def f237(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() listing = [] groups = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available groups.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f237(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 238 ```python def f238(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue pending.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f238(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 239 ```python def f239(rows, keep, prices): rows = list(rows) matches = [] groupmap = {} 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] matches.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f239([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 240 ```python def f240(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] layers = [] while len(finished) < len(nodes): 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) sequence.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f240(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 241 ```python def f241(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) complete = set() listing = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available layers.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': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` ``` f241(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 242 ```python def f242(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: 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 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 ``` ``` f242(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 243 ```python def f243(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])) unmatched = 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': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f243([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 244 ```python def f244(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \n').upper()[:12] banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f244(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 245 ```python def f245(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().upper() excluded = {str(value).strip() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f245(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 246 ```python def f246(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f246(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 247 ```python def f247(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] + min(gap, 2): combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) breaks = [] for pos in range(1, len(combined)): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f247([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 248 ```python def f248(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: 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': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` ``` f248(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 249 ```python def f249(left, right, ignored): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() ignored = {normalize(value) for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f249(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 250 ```python def f250(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] ups = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` ``` f250([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 251 ```python def f251(rows, keep, prices): rows = list(rows) selected = [] buckets = {} sumvalue = 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 sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f251([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 252 ```python def f252(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left: if key in factors: 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 ``` ``` f252([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 253 ```python def f253(rows, keep, factors): 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(factors[group]) selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost grand += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f253([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 254 ```python def f254(lines, stop): lines = list(lines) tallies = {} 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 tallies[word] = tallies.get(word, 0) + 1 ordered = 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 ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f254(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 255 ```python def f255(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], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': amount, 'groups': groupmap, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f255([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 256 ```python def f256(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f256([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 257 ```python def f257(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 rootsource = dict(incoming) handled = set() sequence = [] layers = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch layers.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f257(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 258 ```python def f258(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, 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 ``` ``` f258([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 259 ```python def f259(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 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 ``` ``` f259(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 260 ```python def f260(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left: if key in prices and count > 0: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f260([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 261 ```python def f261(rows, keep, prices): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep and value >= 0: score = value * prices[group] selected.append((code, score)) totals[group] = totals.get(group, 0) + score grand += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f261([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 262 ```python def f262(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 ``` ``` f262(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 263 ```python def f263(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f263(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 264 ```python def f264(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) needs[b] += 1 initial = dict(needs) finished = set() listing = [] layers = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch[: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 nextmap[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 ``` ``` f264(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 265 ```python def f265(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = right 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 ``` ``` f265([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 266 ```python def f266(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' \r').upper() excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - excluded b = {tidy(value) for value in right if tidy(value) != ''} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f266(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 267 ```python def f267(spans, gap): spans = list(spans) result = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not result or left > result[-1][1] + min(gap, 2): 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((min(100, 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 ``` ``` f267([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 268 ```python def f268(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 items = [] for key, count in left: if key in ratemap: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f268([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 269 ```python def f269(spans, gap): spans = list(spans) result = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = right 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 ``` ``` f269([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 270 ```python def f270(rows, keep, prices): rows = list(rows) matches = [] buckets = {} grand = 0 for code, group, state, value in rows: if state in keep: charge = value * prices[group] matches.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge grand += charge matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': buckets, 'top': [code for code, charge in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f270([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 271 ```python def f271(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 combined = [] for key, count in left: if key in ratemap: combined.append((key, count * ratemap[key] + fee)) combined.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 combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f271([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 272 ```python def f272(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f272(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 273 ```python def f273(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f273(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 274 ```python def f274(lines, stop): lines = list(lines) tallies = {} stop = {str(word) 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 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 ``` ``` f274(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 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] + gap % 5: 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([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 276 ```python def f276(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f276(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 277 ```python def f277(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = right gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(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 ``` ``` f277([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 278 ```python def f278(rows, keep, factors): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * 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 ``` ``` f278([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 279 ```python def f279(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f279(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 280 ```python def f280(left, right, banned): left = list(left) def tidy(value): return str(value).strip(' \r').upper() banned = {str(value).upper() for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f280(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 281 ```python def f281(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) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes) and len(handled) < 20: nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in 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 ``` ``` f281(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 282 ```python def f282(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] increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f282([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 283 ```python def f283(board): board = list(board) known = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in known: continue cells = [] work = [(row, col)] known.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue work.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f283(['#..###', '#.###.', '#.....', '#.#...', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[3,2],[5,4]],"count":5,"filled":15,"sizes":[6,4,3,1,1]} ``` ### 284 ```python def f284(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) complete = set() sequence = [] rounds = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available rounds.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f284(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 285 ```python def f285(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' ').upper() ignored = {normalize(value) for value in ignored} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f285(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 286 ```python def f286(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) complete = set() sequence = [] groups = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not available: break current = available groups.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f286(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) ``` ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 287 ```python def f287(rows, keep, prices): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in rows: if state in keep: score = value * prices[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(dict.fromkeys(kept)), 'total': grand, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f287([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":3,"groups":{"core":121,"south":155},"top":["BR26","DL11","AN30"],"total":276} ``` ### 288 ```python def f288(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = max(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': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` ``` f288([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 289 ```python def f289(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) indegree[b] += 1 initial = dict(indegree) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes): available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f289(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 290 ```python def f290(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f290(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 291 ```python def f291(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] = 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 ``` ``` f291([(4, 6), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,6],"longest":10,"segments":[[4,12],[15,23],[29,37],[44,54],[60,67]]} ``` ### 292 ```python def f292(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, min(50, len(combined))): left = combined[pos][0] gaps.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f292([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 293 ```python def f293(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop or len(word) > 12: continue wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f293(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 294 ```python def f294(rows, keep, factors): 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: charge = value * abs(factors[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(selected), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f294([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 295 ```python def f295(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value > limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f295([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 296 ```python def f296(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) handled = set() sequence = [] layers = [] while len(handled) < len(nodes) and len(handled) < 20: available = sorted((node for node in nodes if needs[node] == 0 and node not in handled)) 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]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f296(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 297 ```python def f297(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[: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 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 ``` ``` f297(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 298 ```python def f298(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' \n').upper() ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f298(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 299 ```python def f299(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 ``` ``` f299(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 300 ```python def f300(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) > 12: 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 ``` ``` f300(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['frost']) ``` ``` {"counts":{"amber":3,"blue":8,"cyan":5,"delta":1,"ember":3,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"c":5,"d":1,"e":3,"g":5,"h":4},"size":141,"top":["blue","cyan","garnet","helium","amber"]} ``` ### 301 ```python def f301(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f301(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 302 ```python def f302(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \r').upper() banned = {str(value).strip() for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f302(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### 303 ```python def f303(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] = 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 ``` ``` f303([(3, 5), (6, 12), (15, 18), (19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67)], 2) ``` ``` {"cover":41,"holes":[3,6,7,3,4],"longest":9,"segments":[[3,12],[15,23],[29,37],[44,48],[51,56],[60,67]]} ``` ### 304 ```python def f304(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} terms = [] for line in lines[:20]: 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 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 ``` ``` f304(['Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan', 'Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet'], ['cyan']) ``` ``` {"counts":{"amber":3,"blue":8,"delta":1,"ember":3,"frost":2,"garnet":5,"helium":4},"initials":{"a":3,"b":8,"d":1,"e":3,"f":2,"g":5,"h":4},"size":131,"top":["blue","garnet","helium","amber","ember"]} ``` ### 305 ```python def f305(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 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({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f305([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 7)], 3) ``` ``` {"count":7,"items":[["GQ",69],["HR",66],["BR",45],["FP",43],["AN",39]],"missing":["EV"],"total":330} ``` ### 306 ```python def f306(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) complete = set() result = [] groups = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available groups.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': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` ``` f306(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 307 ```python def f307(rows, keep, factors): rows = list(rows) matches = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: charge = value * factors[group] matches.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge sumvalue += charge matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f307([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 308 ```python def f308(rows, keep, ratemap): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * abs(ratemap[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(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f308([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 309 ```python def f309(left, right, excluded): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() excluded = {str(value).upper() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f309(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Emberx', 'ivory', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","EMBERX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET","IVORY"],"left":["HELIUM"],"right":["AMBER","EMBERX"],"score":19} ``` ### 310 ```python def f310(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 rootsource = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: nextbatch = 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 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 rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` ``` f310(['auth', 'ui', 'cache', 'jobs', 'db', 'worker', 'proxy', 'api'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache')]) ``` ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 311 ```python def f311(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)] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f311([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 312 ```python def f312(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * prices[group] selected.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f312([('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18), ('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) ``` ``` {"accepted":4,"groups":{"north":203,"south":100},"top":["CX15","BR41","CX67"],"total":303} ``` ### 313 ```python def f313(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] growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f313([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 314 ```python def f314(board): board = list(board) visited = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f314(['#..###', '#.###.', '#.#...', '#.##..', '......', '###.#.']) ``` ``` {"anchors":[[0,3],[0,0],[5,0],[5,4]],"count":4,"filled":17,"sizes":[9,4,3,1]} ``` ### 315 ```python def f315(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 and (limit >= 0 or pos > 0)] growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` ``` f315([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 316 ```python def f316(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left[:20]: if key in factors: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f316([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 317 ```python def f317(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: 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 ``` ``` f317([('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8), ('FP', 5), ('GQ', 11), ('HR', 9)], [('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8), ('FP', 8), ('GQ', 6), ('HR', 8)], 3) ``` ``` {"count":7,"items":[["HR",75],["GQ",69],["BR",45],["FP",43],["DL",35]],"missing":["EV"],"total":327} ``` ### 318 ```python def f318(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 ``` ``` f318([6, 18, 24, 5, 7, 13, 20, 24, 18, 12, 24, 19, 15, 18, 14], 3, 39) ``` ``` {"best":62,"hits":[0,1,4,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,36,25,40,57,62,54,54,55,58,52,47]} ``` ### 319 ```python def f319(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, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` ``` f319([6, 18, 24, 5, 4, 13, 20, 24, 18, 12, 24, 19, 15, 20, 14], 3, 40) ``` ``` {"best":62,"hits":[0,1,5,6,7,8,9,10,11,12],"rises":5,"sums":[48,47,33,22,37,57,62,54,54,55,58,54,49]} ``` ### 320 ```python def f320(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \r').upper() excluded = {str(value).upper() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f320(['helium', 'ivory', 'BLUE', 'GARNET', 'Ember', 'cyan ', 'Ember'], ['cyan ', ' Amber', 'GARNET', 'Ember', 'ivoryx', 'BLUE', 'Ember'], ['EMBER']) ``` ``` {"all":["AMBER","BLUE","CYAN","GARNET","HELIUM","IVORY","IVORYX"],"both":["BLUE","CYAN","GARNET"],"left":["HELIUM","IVORY"],"right":["AMBER","IVORYX"],"score":14} ``` ### Final Query Give me the output of the final query without executing it! ```python import json, re from collections import deque out = {} values = list((6, 18, 24, 5, 7, 13, 20) + (24, 18, 12, 24, 19, 15, 18, 14)) width = 3 limit = 39 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} memo = record['aa'][2] lines = list(('Ember, amber. ember garnet', 'Helium, cyan. blue garnet cyan') + ('Frost, cyan. blue helium', 'Delta, garnet. frost blue blue', 'Helium, cyan. ember blue', 'Amber, blue. amber garnet cyan', 'Helium, blue. blue garnet')) route = memo cases = { 4: ['cyan'], 5: ['frost'], } 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))} clue = record['dd'] left = list((('AN', 3), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 8)) + (('FP', 5), ('GQ', 11), ('HR', 9))) fee = 3 route = memo cases = { 5: list((('AN', 12), ('BR', 7), ('CX', 3), ('DL', 8)) + (('FP', 8), ('GQ', 6), ('HR', 7))), 4: list((('AN', 8), ('BR', 7), ('CX', 3), ('DL', 8)) + (('FP', 8), ('GQ', 6), ('HR', 8))), } 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)} stamp = record['bb'] left = list(('helium', 'ivory', 'BLUE') + ('GARNET', 'Ember', 'cyan ', 'Ember')) blocked = ['EMBER'] options = { 4: ('near', clue), 5: ('far', stamp), } route = options[memo] cases = { ('near', 131): list(('cyan ', ' Amber', 'GARNET') + ('Emberx', 'ivory', 'BLUE', 'Ember')), ('near', 141): list(('cyan ', ' Amber', 'GARNET') + ('Ember', 'ivoryx', 'BLUE', 'Ember')), ('far', 327): list(('cyan ', ' Amber', 'GARNET') + ('Ember', 'ivoryx', 'BLUE', 'Ember')), ('far', 330): list(('cyan ', ' Amber', 'GARNET') + ('Ember', 'ivoryx', 'BLUE', 'Ember')), } right = cases[route] a = set() b = set() ban = set() for value in blocked: ban.add(value.strip().upper()) for value in left: value = value.strip().upper() if value not in ban: a.add(value) for value in right: value = value.strip().upper() if value not in ban: b.add(value) both = a.intersection(b) record = {'aa': sorted(both), 'bb': sorted(a.difference(b)), 'cc': sorted(b.difference(a)), 'dd': sorted(a.union(b)), 'ee': sum(map(len, both))} ticket = record['ee'] nodes = list(('auth', 'ui', 'cache') + ('jobs', 'db', 'worker', 'proxy', 'api')) route = stamp cases = { 330: list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'cache'))), 327: list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth'))), } 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'][0] route = (ticket, badge) cases = { (19, 'cache'): list(('#..###', '#.###.', '#.#...', '#.##..') + ('......', '###.#.')), (19, 'auth'): list(('#..###', '#.###.', '#.....', '#.#...') + ('......', '###.#.')), (14, 'cache'): list(('#..###', '#.###.', '#.....', '#.#...') + ('......', '###.#.')), (14, 'auth'): list(('#..###', '#.###.', '#.....', '#.#...') + ('......', '###.#.')), } board = cases[route] cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#'] parent = {cell: cell for cell in cells} def root(cell): while parent[cell] != cell: parent[cell] = parent[parent[cell]] cell = parent[cell] return cell def merge(a, b): a = root(a) b = root(b) if a != b: parent[b] = a for row, col in cells: for other in ((row - 1, col), (row, col - 1)): if other in parent: merge((row, col), other) groups = {} for cell in cells: groups.setdefault(root(cell), []).append(cell) parts = [(len(group), min(group)) for group in groups.values()] parts.sort(key=lambda item: (-item[0], item[1])) record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)} permit = record['cc'] rows = list((('AN30', 'south', 'ready', 8), ('CX67', 'south', 'checked', 9), ('AN04', 'edge', 'hold', 21), ('BR41', 'south', 'checked', 11), ('CX78', 'core', 'hold', 9), ('CX15', 'north', 'checked', 24), ('DL52', 'north', 'hold', 18)) + (('CX89', 'core', 'hold', 21), ('BR26', 'core', 'ready', 11), ('BR63', 'south', 'hold', 19), ('DL00', 'north', 'void', 7), ('BR37', 'edge', 'void', 17), ('CX74', 'north', 'checked', 5), ('DL11', 'south', 'ready', 23))) rates = dict((('north', 7), ('south', 5)) + (('core', 11), ('edge', 3))) route = permit cases = { 15: ['checked'], 17: ['ready'], } keep = 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} signal = record['bb'] gap = 2 route = signal cases = { 276: list(((4, 6), (6, 12), (15, 18)) + ((19, 23), (29, 32), (34, 37), (44, 48), (49, 54), (60, 67))), 303: list(((3, 5), (6, 12), (15, 18)) + ((19, 23), (29, 32), (34, 37), (44, 48), (51, 56), (60, 67))), } spans = cases[route] spans.sort(key=lambda pair: (pair[0], pair[1])) parts = [] pos = 0 while pos < len(spans): left, right = spans[pos] pos += 1 while pos < len(spans) and spans[pos][0] <= right + gap: right = max(right, spans[pos][1]) pos += 1 parts.append([left, right]) holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} out['aa'] = record['aa'] out['bb'] = record['bb'] out['cc'] = record['cc'] out['dd'] = record['dd'] print(json.dumps(out, separators=(",", ":"), sort_keys=True)) ```