### 001 ```python def f1(board): board = list(board) known = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue queue.append(nxt) 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 ``` Example call: ``` f1(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 002 ```python def f2(board): board = list(board) found = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in found: continue cells = [] work = [(row, col)] found.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue work.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f2(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 003 ```python def f3(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 ``` Example call: ``` f3(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 004 ```python def f4(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) 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 ``` Example call: ``` f4(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 005 ```python def f5(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap + 1: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f5([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 006 ```python def f6(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 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 ``` Example call: ``` f6([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 007 ```python def f7(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: 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])) 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({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f7([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 008 ```python def f8(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = 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((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 ``` Example call: ``` f8([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 009 ```python def f9(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f9([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 010 ```python def f10(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f10([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 011 ```python def f11(board): board = list(board) known = set() regions = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] work = [(row, col)] known.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue work.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f11(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 012 ```python def f12(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] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f12([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 013 ```python def f13(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 series = [] for pos in range(min(64, len(values) - size + 1)): series.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(series) if value >= limit] increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f13([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 014 ```python def f14(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows[: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 ``` Example call: ``` f14([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 015 ```python def f15(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) > 12: continue wordmap[word] = wordmap.get(word, 0) + 1 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f15(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 016 ```python def f16(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit] ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f16([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 017 ```python def f17(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f17(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 018 ```python def f18(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y = max(-64, min(64, y + dy)) used = min(64, used + 1) point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f18([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 019 ```python def f19(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f19(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 020 ```python def f20(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f20([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 021 ```python def f21(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, 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 ``` Example call: ``` f21([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 022 ```python def f22(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])) 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 ``` Example call: ``` f22([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 023 ```python def f23(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) handled = set() result = [] groups = [] 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 groups.append(current) for node in current: handled.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f23(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 024 ```python def f24(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f24([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 025 ```python def f25(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f25(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 026 ```python def f26(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([0] + windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f26([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 027 ```python def f27(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f27([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 028 ```python def f28(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) needs[b] += 1 initial = dict(needs) handled = set() result = [] layers = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled)) if not nextbatch: break current = nextbatch layers.append(current) for node in current: handled.add(node) result.append(node) for node in current: for nxt in nextmap[node]: needs[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f28(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 029 ```python def f29(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \r').upper()[:12] banned = {normalize(value) for value in banned} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned b = {normalize(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f29(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 030 ```python def f30(left, right, banned): left = list(left) def canon(value): return ' '.join(str(value).split()).upper()[:12] banned = {canon(value) for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right if canon(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 ``` Example call: ``` f30(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 031 ```python def f31(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f31([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 032 ```python def f32(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) indegree[b] += 1 rootsource = 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 children[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f32(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 033 ```python def f33(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 rootsource = dict(incoming) finished = set() listing = [] layers = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f33(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 034 ```python def f34(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) 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 ``` Example call: ``` f34(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 035 ```python def f35(left, right, ignored): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - 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 ``` Example call: ``` f35(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 036 ```python def f36(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(series[:64]) if value >= limit] ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f36([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 037 ```python def f37(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: 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 ``` Example call: ``` f37([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 038 ```python def f38(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f38([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 039 ```python def f39(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(windows) if value >= limit] ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f39([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 040 ```python def f40(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(totals) 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 ``` Example call: ``` f40([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 041 ```python def f41(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + max(0, gap) + 1: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f41([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 042 ```python def f42(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f42([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 043 ```python def f43(board): board = list(board) known = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f43(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 044 ```python def f44(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 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 ``` Example call: ``` f44([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 045 ```python def f45(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f45([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 046 ```python def f46(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 combined = [] for key, count in left: if key in prices: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f46([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 047 ```python def f47(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) 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 ``` Example call: ``` f47([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 048 ```python def f48(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) indegree[b] += 1 startdegree = dict(indegree) handled = set() listing = [] layers = [] 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 layers.append(current) for node in current: handled.add(node) listing.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f48(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 049 ```python def f49(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f49([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 050 ```python def f50(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f50(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 051 ```python def f51(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) finished = set() sequence = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f51(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 052 ```python def f52(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 combined = [] for key, count in left: if key in prices and count > 0: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f52([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 053 ```python def f53(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: 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({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f53([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 054 ```python def f54(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left: if key in prices: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f54([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 055 ```python def f55(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 ``` Example call: ``` f55([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 056 ```python def f56(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 = [] work = [(row, col)] found.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) 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 ``` Example call: ``` f56(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 057 ```python def f57(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) 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 ``` Example call: ``` f57(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 058 ```python def f58(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 + 1: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f58([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 059 ```python def f59(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((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f59(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 060 ```python def f60(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f60([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 061 ```python def f61(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(combined[-1][1], right) breaks = [] for pos in range(1, min(50, len(combined))): left = combined[pos][0] breaks.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f61([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 062 ```python def f62(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) incoming[b] += 1 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=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch layers.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f62(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 063 ```python def f63(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 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 ``` Example call: ``` f63(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 064 ```python def f64(board): board = list(board) found = 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 found: continue cells = [] work = [(row, col)] found.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue work.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f64(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 065 ```python def f65(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f65([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 066 ```python def f66(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({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f66([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 067 ```python def f67(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) < 2: continue frequency[word] = min(9, frequency.get(word, 0) + 1) ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f67(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 068 ```python def f68(left, right, excluded): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f68(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 069 ```python def f69(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)] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f69([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 070 ```python def f70(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f70([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 071 ```python def f71(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) needs[b] += 1 startdegree = dict(needs) finished = set() listing = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch 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 startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f71(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 072 ```python def f72(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left: if key in prices and count > 0: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f72([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 073 ```python def f73(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) 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 ``` Example call: ``` f73(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 074 ```python def f74(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(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 ``` Example call: ``` f74([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 075 ```python def f75(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 % 5: 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 ``` Example call: ``` f75([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 076 ```python def f76(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) 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 ``` Example call: ``` f76(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 077 ```python def f77(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 combined = [] for key, count in left: if key in ratemap and count > 0: combined.append((key, count * ratemap[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) 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({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f77([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 078 ```python def f78(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: 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])) 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 ``` Example call: ``` f78(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 079 ```python def f79(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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f79(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 080 ```python def f80(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text[: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 sorteditems = 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 sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f80(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 081 ```python def f81(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) finished = set() 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 ``` Example call: ``` f81(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 082 ```python def f82(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[: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 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in wordmap.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f82(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 083 ```python def f83(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left > combined[-1][1] + gap + 1: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f83([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 084 ```python def f84(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f84([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 085 ```python def f85(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} - 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 ``` Example call: ``` f85(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 086 ```python def f86(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in 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) 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 ``` Example call: ``` f86(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 087 ```python def f87(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series[:64]) if value > limit] increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f87([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 088 ```python def f88(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] ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f88([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 089 ```python def f89(left, right, excluded): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper()[:12] excluded = {str(value).strip() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b 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 ``` Example call: ``` f89(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 090 ```python def f90(spans, gap): spans = list(spans) segments = [] for left, right in sorted(set(spans)): if not segments or left > segments[-1][1] + gap + 1: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) 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 ``` Example call: ``` f90([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 091 ```python def f91(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 ``` Example call: ``` f91([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 092 ```python def f92(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series) if value >= limit] growth = 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': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f92([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 093 ```python def f93(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f93(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 094 ```python def f94(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f94([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 095 ```python def f95(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, min(50, len(result))): left = result[pos][0] breaks.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f95([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 096 ```python def f96(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f96([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 097 ```python def f97(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f97([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 098 ```python def f98(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 ``` Example call: ``` f98(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 099 ```python def f99(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 items = [] for key, count in left: if key in ratemap: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f99([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 100 ```python def f100(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() sequence = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f100(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"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(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5 + 1: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) gaps = [] for pos in range(1, len(segments)): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f101([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 102 ```python def f102(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 startdegree = dict(incoming) handled = set() listing = [] layers = [] while len(handled) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: handled.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f102(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 103 ```python def f103(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f103(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 104 ```python def f104(left, right, ignored): left = list(left) def tidy(value): return str(value).strip().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 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 ``` Example call: ``` f104(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 105 ```python def f105(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f105([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 106 ```python def f106(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' or (char == "'" and word): word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue wordmap[word] = min(9, 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 ``` Example call: ``` f106(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 107 ```python def f107(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] + 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 ``` Example call: ``` f107([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 108 ```python def f108(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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f108(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 109 ```python def f109(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in tallies.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f109(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 110 ```python def f110(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f110([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 111 ```python def f111(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) 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 ``` Example call: ``` f111([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 112 ```python def f112(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f112([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 113 ```python def f113(left, right, banned): left = list(left) def tidy(value): return ' '.join(str(value).split()).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 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 ``` Example call: ``` f113(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 114 ```python def f114(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 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 ``` Example call: ``` f114(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 115 ```python def f115(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 = [] 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) 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 ``` Example call: ``` f115(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 116 ```python def f116(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap % 5: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f116([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 117 ```python def f117(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' \r').upper() excluded = {str(value).strip() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b 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 ``` Example call: ``` f117(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 118 ```python def f118(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series) if value > limit] 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 ``` Example call: ``` f118([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 119 ```python def f119(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[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop or len(word) > 12: continue 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 ``` Example call: ``` f119(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 120 ```python def f120(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 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f120(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 121 ```python def f121(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: 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': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f121(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 122 ```python def f122(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: 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])) 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 ``` Example call: ``` f122([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 123 ```python def f123(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f123([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 124 ```python def f124(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 items = [] for key, count in left: if key in prices: 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 ``` Example call: ``` f124([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 125 ```python def f125(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])) 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 ``` Example call: ``` f125([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 126 ```python def f126(values, width, limit): values = list(values) size = width series = [] for pos in range(min(64, len(values) - size + 1)): series.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(series) if value >= limit] 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 ``` Example call: ``` f126([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 127 ```python def f127(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: 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 ``` Example call: ``` f127(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 128 ```python def f128(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not result or left > result[-1][1] + max(0, 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': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f128([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 129 ```python def f129(left, right, ignored): left = list(left) def canon(value): return str(value).strip().upper() ignored = {str(value).upper() for value in ignored} a = {canon(value) for value in left} - ignored b = {canon(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f129(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 130 ```python def f130(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] growth = 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': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f130([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 131 ```python def f131(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 rootsource = dict(needs) complete = set() sequence = [] rounds = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) 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 nextmap[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f131(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 132 ```python def f132(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) indegree[b] += 1 initial = dict(indegree) complete = set() result = [] layers = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if indegree[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) result.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f132(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 133 ```python def f133(board): board = list(board) found = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] 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) 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 ``` Example call: ``` f133(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 134 ```python def f134(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() result = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available layers.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in 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 ``` Example call: ``` f134(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 135 ```python def f135(board): board = list(board) visited = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if 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) 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 ``` Example call: ``` f135(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 136 ```python def f136(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 ``` Example call: ``` f136([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 137 ```python def f137(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) handled = set() result = [] layers = [] while len(handled) < len(nodes) and len(handled) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: handled.add(node) 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 ``` Example call: ``` f137(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 138 ```python def f138(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 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 ``` Example call: ``` f138(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 139 ```python def f139(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': min(256, max(totals)), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f139([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 140 ```python def f140(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])) positions = [pos for pos, value in enumerate(series) if value >= limit] increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f140([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 141 ```python def f141(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) positions = [pos for pos, value in enumerate(windows) if value >= limit] ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f141([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 142 ```python def f142(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f142(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 143 ```python def f143(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) handled = set() result = [] rounds = [] while len(handled) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not available: break current = available rounds.append(current) for node in current: handled.add(node) result.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f143(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 144 ```python def f144(spans, gap): spans = list(spans) result = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, min(50, 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 ``` Example call: ``` f144([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 145 ```python def f145(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({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f145([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 146 ```python def f146(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f146([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 147 ```python def f147(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x = max(-64, min(64, x + dx)) y += dy used = min(64, used + 1) point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f147([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 148 ```python def f148(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[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f148(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 149 ```python def f149(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() result = [] 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)) 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 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 ``` Example call: ``` f149(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 150 ```python def f150(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' \n').upper() excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f150(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 151 ```python def f151(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 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 ``` Example call: ``` f151(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 152 ```python def f152(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left > combined[-1][1] + max(0, gap) + 1: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f152([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 153 ```python def f153(left, right, ignored): left = list(left) def tidy(value): return str(value).strip().upper() ignored = {tidy(value) for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f153(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 154 ```python def f154(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 ``` Example call: ``` f154([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 155 ```python def f155(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 ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f155(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 156 ```python def f156(left, right, excluded): left = list(left) def canon(value): return ' '.join(str(value).split()).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 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 ``` Example call: ``` f156(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 157 ```python def f157(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: 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 ``` Example call: ``` f157(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 158 ```python def f158(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in tallies.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f158(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 159 ```python def f159(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] rounds = [] 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 rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f159(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 160 ```python def f160(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) < 2: 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 ``` Example call: ``` f160(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 161 ```python def f161(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f161([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 162 ```python def f162(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 if tidy(value) != tidy(value) or tidy(value) != ''} - ignored b = {tidy(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f162(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 163 ```python def f163(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) 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 ``` Example call: ``` f163(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 164 ```python def f164(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])) 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({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f164([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 165 ```python def f165(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(result[-1][1], right) spaces = [] for pos in range(1, len(result)): left = result[pos][0] spaces.append(min(100, left - result[pos - 1][1])) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f165([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 166 ```python def f166(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: 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])) 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 ``` Example call: ``` f166([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 167 ```python def f167(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) incoming[b] += 1 initial = dict(incoming) handled = set() listing = [] layers = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch layers.append(current) for node in current: handled.add(node) listing.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f167(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 168 ```python def f168(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f168([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 169 ```python def f169(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 = [] 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 ``` Example call: ``` f169(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 170 ```python def f170(board): board = list(board) found = set() regions = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in found: continue cells = [] work = [(row, col)] found.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue work.append(nxt) 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 ``` Example call: ``` f170(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 171 ```python def f171(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() listing = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f171(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 172 ```python def f172(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) needs[b] += 1 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), 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) listing.append(node) for node in current: for nxt in routes[node]: needs[nxt] -= 1 returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f172(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 173 ```python def f173(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) needs[b] += 1 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 nextmap[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 ``` Example call: ``` f173(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 174 ```python def f174(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((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f174(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 175 ```python def f175(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 = [] groups = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch groups.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f175(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 176 ```python def f176(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) indegree[b] += 1 rootsource = dict(indegree) handled = set() sequence = [] groups = [] while len(handled) < len(nodes) and len(handled) < 20: nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled)) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch 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 rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f176(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"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(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f177([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 178 ```python def f178(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f178(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 179 ```python def f179(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 matched = [] for key, count in left: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f179([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 180 ```python def f180(left, right, ignored): left = list(left) def tidy(value): return str(value).strip(' ').upper() ignored = {tidy(value) for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f180(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 181 ```python def f181(board): board = list(board) known = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue queue.append(nxt) 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 ``` Example call: ``` f181(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 182 ```python def f182(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] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) breaks = [] for pos in range(1, min(50, 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 ``` Example call: ``` f182([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 183 ```python def f183(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, min(50, len(combined))): left = combined[pos][0] spaces.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f183([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 184 ```python def f184(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper()[:12] ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f184(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 185 ```python def f185(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 = [] 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 ``` Example call: ``` f185(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 186 ```python def f186(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 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 ``` Example call: ``` f186(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 187 ```python def f187(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[:64]) if value >= limit] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f187([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 188 ```python def f188(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} items = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f188(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 189 ```python def f189(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: 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 ``` Example call: ``` f189([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 190 ```python def f190(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue frequency[word] = min(9, 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 ``` Example call: ``` f190(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 191 ```python def f191(left, right, banned): left = list(left) def canon(value): return str(value).strip().upper() banned = {canon(value) for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f191(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 192 ```python def f192(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] ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f192([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 193 ```python def f193(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: 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 ``` Example call: ``` f193(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 194 ```python def f194(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \r').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 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 ``` Example call: ``` f194(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 195 ```python def f195(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not available: break current = available rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f195(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 196 ```python def f196(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value >= limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f196([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 197 ```python def f197(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(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((min(100, right - left) for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f197([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 198 ```python def f198(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 series = [] for pos in range(min(64, len(values) - size + 1)): series.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(series) if value >= limit] growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f198([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 199 ```python def f199(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f199([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 200 ```python def f200(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 spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f200([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 201 ```python def f201(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f201([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 202 ```python def f202(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left[:20]: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) 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 ``` Example call: ``` f202([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 203 ```python def f203(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + gap % 5: result.append([left, right]) else: result[-1][1] = max(0, result[-1][1], right) breaks = [] for pos in range(1, len(result)): left = result[pos][0] breaks.append(min(100, left - result[pos - 1][1])) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f203([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 204 ```python def f204(left, right, banned): left = list(left) def tidy(value): return str(value).strip(' ').upper() banned = {str(value).upper() for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f204(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 205 ```python def f205(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: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f205([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 206 ```python def f206(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f206(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 207 ```python def f207(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(windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f207([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 208 ```python def f208(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': min(256, max(windows)), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f208([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 209 ```python def f209(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 ``` Example call: ``` f209([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 210 ```python def f210(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) handled = set() result = [] groups = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch groups.append(current) for node in current: handled.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f210(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 211 ```python def f211(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) complete = set() listing = [] layers = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: complete.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f211(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 212 ```python def f212(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f212(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 213 ```python def f213(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 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 ``` Example call: ``` f213(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 214 ```python def f214(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left: if key in prices and count > 0: 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 ``` Example call: ``` f214([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 215 ```python def f215(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) spaces = [] for pos in range(1, min(50, len(combined))): left = combined[pos][0] spaces.append(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 ``` Example call: ``` f215([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 216 ```python def f216(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f216([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 217 ```python def f217(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) needs[b] += 1 initial = dict(needs) finished = set() sequence = [] layers = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: finished.add(node) 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 ``` Example call: ``` f217(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 218 ```python def f218(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f218([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 219 ```python def f219(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f219([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 220 ```python def f220(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f220([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 221 ```python def f221(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 items = [] for key, count in left[:20]: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f221([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 222 ```python def f222(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 = [] work = [(row, col)] known.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue work.append(nxt) 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 ``` Example call: ``` f222(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 223 ```python def f223(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) finished = set() sequence = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f223(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 224 ```python def f224(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 = [] 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 ``` Example call: ``` f224(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 225 ```python def f225(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(0, result[-1][1], right) breaks = [] for pos in range(1, min(50, len(result))): left = result[pos][0] breaks.append(min(100, left - result[pos - 1][1])) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f225([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 226 ```python def f226(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f226(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 227 ```python def f227(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) 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)) 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) 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 ``` Example call: ``` f227(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 228 ```python def f228(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 ``` Example call: ``` f228([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 229 ```python def f229(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])) unmatched = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f229([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 230 ```python def f230(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' ').upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded b = {normalize(value) for value in right} - 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 ``` Example call: ``` f230(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 231 ```python def f231(lines, stop): lines = list(lines) wordmap = {} 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 or len(word) < 2: 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 ``` Example call: ``` f231(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 232 ```python def f232(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f232([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 233 ```python def f233(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(windows[:64]) if value > limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f233([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 234 ```python def f234(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) complete = set() listing = [] layers = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch 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 initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f234(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 235 ```python def f235(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in repeats: repeats.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f235([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 236 ```python def f236(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] 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 ``` Example call: ``` f236([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 237 ```python def f237(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f237([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 238 ```python def f238(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text[: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 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 ``` Example call: ``` f238(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 239 ```python def f239(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 matched = [] for key, count in left: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f239([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 240 ```python def f240(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() listing = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f240(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 241 ```python def f241(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f241(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 242 ```python def f242(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + gap + 1: result.append([left, right]) else: result[-1][1] = right breaks = [] for pos in range(1, len(result)): left = result[pos][0] breaks.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f242([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 243 ```python def f243(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f243(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 244 ```python def f244(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \r').upper() banned = {str(value).upper() for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f244(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 245 ```python def f245(board): board = list(board) known = set() components = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f245(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 246 ```python def f246(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in repeats: repeats.append(list(point)) found.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f246([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 247 ```python def f247(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue 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 ``` Example call: ``` f247(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 248 ```python def f248(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue frequency[word] = min(9, 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 ``` Example call: ``` f248(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 249 ```python def f249(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])) 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 ``` Example call: ``` f249([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 250 ```python def f250(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 items = [] for key, count in left[:20]: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f250([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 251 ```python def f251(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) indegree[b] += 1 initial = dict(indegree) complete = set() result = [] groups = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) if not available: break current = available groups.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f251(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 252 ```python def f252(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f252([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 2) ``` Output example: ``` {"count":7,"items":[["FP",110],["CX",83],["HR",58],["DL",47],["GQ",38]],"missing":["EV"],"total":398} ``` ### 253 ```python def f253(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) 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 ``` Example call: ``` f253(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 254 ```python def f254(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f254([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 255 ```python def f255(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((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f255(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 256 ```python def f256(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f256([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 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: a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() listing = [] groups = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch groups.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f257(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} ``` ### 258 ```python def f258(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[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f258(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 259 ```python def f259(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f259(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 260 ```python def f260(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) 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 ``` Example call: ``` f260(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 261 ```python def f261(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f261([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 262 ```python def f262(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)): 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 ``` Example call: ``` f262(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 263 ```python def f263(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] 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 ``` Example call: ``` f263(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 264 ```python def f264(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 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 ``` Example call: ``` f264(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 265 ```python def f265(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f265([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 266 ```python def f266(values, width, limit): values = list(values) size = width totals = [] for pos in range(min(64, len(values) - size + 1)): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit] increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f266([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 267 ```python def f267(left, right, ignored): left = list(left) def canon(value): return str(value).strip().upper() ignored = {canon(value) for value in ignored} a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - ignored b = {canon(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f267(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 268 ```python def f268(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(windows) if value >= limit] 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': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f268([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 269 ```python def f269(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': 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 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f269(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 270 ```python def f270(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: 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 ``` Example call: ``` f270([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 271 ```python def f271(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])) 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 ``` Example call: ``` f271(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 272 ```python def f272(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' ').upper() excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right if tidy(value) != ''} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f272(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 273 ```python def f273(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f273([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 274 ```python def f274(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f274([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 275 ```python def f275(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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 ``` Example call: ``` f275([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 276 ```python def f276(left, right, banned): left = list(left) def tidy(value): return ' '.join(str(value).split()).upper() banned = {str(value).strip() for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right} - banned both = a & b 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 ``` Example call: ``` f276(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 277 ```python def f277(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 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 ``` Example call: ``` f277(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 278 ```python def f278(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f278([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 279 ```python def f279(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 ``` Example call: ``` f279([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 280 ```python def f280(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 ``` Example call: ``` f280([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 281 ```python def f281(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y = max(-64, min(64, y + dy)) used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f281([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 282 ```python def f282(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 ``` Example call: ``` f282([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 283 ```python def f283(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f283([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 284 ```python def f284(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f284([('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-6,0,3],"end":[0,1],"far":9,"revisits":[[-3,-5],[-3,-4],[-3,-3],[-2,-4],[-2,-3]],"steps":33} ``` ### 285 ```python def f285(board): board = list(board) known = set() components = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f285(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 286 ```python def f286(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) indegree[b] += 1 initial = dict(indegree) finished = set() sequence = [] groups = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch groups.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f286(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')]) ``` Output example: ``` {"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(values, width, limit): values = list(values) size = width totals = [] for pos in range(min(64, len(values) - size + 1)): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) positions = [pos for pos, value in enumerate(totals) if value >= limit] ups = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f287([16, 20, 22, 8, 14, 5, 16, 18, 5, 6, 20, 13, 23, 13, 20], 3, 45) ``` Output example: ``` {"best":58,"hits":[0,1,10,11,12],"rises":6,"sums":[58,50,44,27,35,39,39,29,31,39,56,49,56]} ``` ### 288 ```python def f288(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` Example call: ``` f288([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 289 ```python def f289(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) needs[b] += 1 initial = dict(needs) complete = set() listing = [] layers = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch layers.append(current) for node in current: complete.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 ``` Example call: ``` f289(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 290 ```python def f290(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().upper()[:12] excluded = {str(value).upper() for value in excluded} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded b = {normalize(value) for value in right} - excluded both = a & b 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 ``` Example call: ``` f290(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 291 ```python def f291(board): board = list(board) found = set() pieces = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in found: continue cells = [] work = [(row, col)] found.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) 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 ``` Example call: ``` f291(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 292 ```python def f292(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] 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 ``` Example call: ``` f292(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 293 ```python def f293(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, 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 ``` Example call: ``` f293([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 294 ```python def f294(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit] increases = sum((1 for pos in range(1, 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 ``` Example call: ``` f294([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 295 ```python def f295(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 ``` Example call: ``` f295(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 296 ```python def f296(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f296([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 297 ```python def f297(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \n').upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f297(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 298 ```python def f298(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f298([('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2), ('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1)]) ``` Output example: ``` {"bounds":[-3,-10,2,0],"end":[2,-7],"far":13,"revisits":[[-2,-10],[0,-7],[0,-8],[0,-6]],"steps":33} ``` ### 299 ```python def f299(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': max([0] + windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f299([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 300 ```python def f300(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) 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 ``` Example call: ``` f300([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 301 ```python def f301(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left: if key in prices: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f301([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 302 ```python def f302(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 = [] work = [(row, col)] known.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) work.append(nxt) 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 ``` Example call: ``` f302(['..#...', '#.#.##', '##...#', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[1,4],[4,3],[0,2],[3,4],[5,0]],"count":6,"filled":14,"sizes":[4,3,3,2,1,1]} ``` ### 303 ```python def f303(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 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 ``` Example call: ``` f303([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 304 ```python def f304(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) 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 ``` Example call: ``` f304(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 305 ```python def f305(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(windows), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f305([16, 16, 22, 8, 14, 5, 16, 23, 5, 6, 20, 13, 23, 13, 20], 3, 47) ``` Output example: ``` {"best":56,"hits":[0,10,11,12],"rises":5,"sums":[54,46,44,27,35,44,44,34,31,39,56,49,56]} ``` ### 306 ```python def f306(left, right, banned): left = list(left) def canon(value): return str(value).strip(' \n').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 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 ``` Example call: ``` f306(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 307 ```python def f307(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 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 ``` Example call: ``` f307(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 308 ```python def f308(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap + 1: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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 ``` Example call: ``` f308([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 1) ``` Output example: ``` {"cover":37,"holes":[4,8,3,7],"longest":12,"segments":[[3,11],[15,21],[29,41],[44,53],[60,62]]} ``` ### 309 ```python def f309(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' \r').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 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 ``` Example call: ``` f309(['GARNET', 'helium', 'BLUEx', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","BLUEX","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","GARNET","HELIUM"],"left":["BLUEX","IVORY"],"right":["BLUE","DELTA"],"score":22} ``` ### 310 ```python def f310(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 or len(word) > 12: continue frequency[word] = min(9, frequency.get(word, 0) + 1) ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f310(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 311 ```python def f311(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])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f311([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 312 ```python def f312(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left: if key in factors: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f312([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 313 ```python def f313(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] indegree = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) indegree[b] += 1 initial = dict(indegree) finished = set() listing = [] layers = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in routes[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f313(['jobs', 'cache', 'auth', 'worker', 'db', 'proxy', 'api', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 314 ```python def f314(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])) 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 ``` Example call: ``` f314([('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9), ('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7)], [('AN', 6), ('BR', 7), ('CX', 9), ('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8)], 7) ``` Output example: ``` {"count":7,"items":[["FP",115],["CX",88],["HR",63],["DL",52],["GQ",43]],"missing":["EV"],"total":433} ``` ### 315 ```python def f315(board): board = list(board) known = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in known: continue cells = [] work = [(row, col)] known.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue work.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f315(['..#...', '#.#.#.', '##....', '#...#.', '...#..', '#.##..']) ``` Output example: ``` {"anchors":[[1,0],[4,3],[0,2],[1,4],[3,4],[5,0]],"count":6,"filled":12,"sizes":[4,3,2,1,1,1]} ``` ### 316 ```python def f316(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 ``` Example call: ``` f316(['GARNETx', 'helium', 'BLUE', 'frost', 'ivory', ' Amber', 'Ember'], ['helium', 'BLUE', 'GARNET', 'frost', 'delta', ' Amber', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["AMBER","BLUE","DELTA","FROST","GARNET","GARNETX","HELIUM","IVORY"],"both":["AMBER","BLUE","FROST","HELIUM"],"left":["GARNETX","IVORY"],"right":["DELTA","GARNET"],"score":20} ``` ### 317 ```python def f317(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' or (char == "'" and word): word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop or len(word) > 12: continue 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 ``` Example call: ``` f317(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### 318 ```python def f318(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])) 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 ``` Example call: ``` f318(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6},"size":133,"top":["delta","helium","frost","blue","amber"]} ``` ### 319 ```python def f319(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 breaks = [] for pos in range(1, len(result)): left = result[pos][0] breaks.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((min(100, right - left) for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f319([(3, 7), (6, 11), (15, 18), (19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62)], 3) ``` Output example: ``` {"cover":40,"holes":[4,8,7],"longest":24,"segments":[[3,11],[15,21],[29,53],[60,62]]} ``` ### 320 ```python def f320(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue 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 ``` Example call: ``` f320(['Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost mirth', 'Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue'], ['ember']) ``` Output example: ``` {"counts":{"amber":2,"blue":3,"cyan":2,"delta":7,"frost":4,"garnet":2,"helium":6,"mirth":1},"initials":{"a":2,"b":3,"c":2,"d":7,"f":4,"g":2,"h":6,"m":1},"size":138,"top":["delta","helium","frost","blue","amber"]} ``` ### Final Query Give me the output of the final query without executing it! ```python import json, re from collections import deque out = {} lines = list(('Delta, delta. ember garnet', 'Amber, delta. ember delta ember', 'Cyan, ember. frost frost') + ('Blue, helium. frost frost delta', 'Helium, helium. cyan ember', 'Delta, amber. helium helium delta', 'Garnet, helium. blue blue')) stop = set(word.lower() for word in ['ember']) 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))} left = list((('AN', 5), ('BR', 4), ('CX', 9), ('DL', 9)) + (('EV', 5), ('FP', 12), ('GQ', 6), ('HR', 7))) right = list((('AN', 6), ('BR', 7), ('CX', 9)) + (('DL', 5), ('FP', 9), ('GQ', 6), ('HR', 8))) route = record['dd'] cases = { 138: 2, 133: 7, } fee = 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)} route = record['bb'] cases = { 398: list(('..#...', '#.#.##', '##...#') + ('#...#.', '...#..', '#.##..')), 433: 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)} spans = list(((3, 7), (6, 11), (15, 18)) + ((19, 21), (29, 35), (34, 41), (44, 51), (49, 53), (60, 62))) route = record['cc'] cases = { 14: 3, 12: 1, } gap = cases[route] spans.sort(key=lambda pair: (pair[0], pair[1])) parts = [] pos = 0 while pos < len(spans): left, right = spans[pos] pos += 1 while pos < len(spans) and spans[pos][0] <= right + gap: right = max(right, spans[pos][1]) pos += 1 parts.append([left, right]) holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} nodes = list(('jobs', 'cache', 'auth', 'worker', 'db') + ('proxy', 'api', 'ui')) route = record['cc'] cases = { 40: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth'))), 37: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db'))), } edges = cases[route] remaining = set(nodes) done = set() order = [] batches = [] while remaining: ready = sorted((node for node in remaining if all((before in done for before, after in edges if after == node)))) if not ready: break batches.append(ready) order.extend(ready) done.update(ready) remaining.difference_update(ready) roots = sorted((node for node in nodes if all((after != node for before, after in edges)))) record = {'aa': order, 'bb': batches, 'cc': roots, 'dd': len(order)} route = record['aa'][0] cases = { 'cache': list((('S', 3), ('W', 3), ('S', 3), ('N', 3), ('S', 1), ('E', 2)) + (('W', 1), ('N', 2), ('E', 2), ('N', 2), ('W', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1))), 'auth': list((('S', 3), ('W', 3), ('S', 3), ('S', 3), ('S', 1), ('E', 2)) + (('W', 1), ('N', 2), ('E', 2), ('N', 2), ('S', 2), ('N', 3), ('S', 3), ('E', 2), ('N', 1))), } moves = cases[route] delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} points = [[0, 0]] for direction, count in moves: dx, dy = delta[direction] for turn in range(count): points.append([points[-1][0] + dx, points[-1][1] + dy]) seen = set() revisits = [] for point in points: key = tuple(point) if key in seen and point not in revisits: revisits.append(point) seen.add(key) record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} width = 3 route = record['bb'] cases = { 13: (list((16, 20, 22, 8, 14, 5, 16, 18) + (5, 6, 20, 13, 23, 13, 20)), 45), 9: (list((16, 16, 22, 8, 14, 5, 16, 23) + (5, 6, 20, 13, 23, 13, 20)), 47), } values, limit = cases[route] current = sum(values[:width]) sums = [current] for pos in range(width, len(values)): current += values[pos] - values[pos - width] sums.append(current) hits = [] rises = 0 for pos, value in enumerate(sums): if value >= limit: hits.append(pos) if pos and value > sums[pos - 1]: rises += 1 record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} right = list(('helium', 'BLUE', 'GARNET') + ('frost', 'delta', ' Amber', 'Ember')) blocked = ['EMBER'] route = record['bb'] cases = { 58: list(('GARNETx', 'helium', 'BLUE') + ('frost', 'ivory', ' Amber', 'Ember')), 56: list(('GARNET', 'helium', 'BLUEx') + ('frost', 'ivory', ' Amber', 'Ember')), } left = cases[route] a = set() b = set() ban = set() for value in blocked: ban.add(value.strip().upper()) for value in left: value = value.strip().upper() if value not in ban: a.add(value) for value in right: value = value.strip().upper() if value not in ban: b.add(value) both = a.intersection(b) record = {'aa': sorted(both), 'bb': sorted(a.difference(b)), 'cc': sorted(b.difference(a)), 'dd': sorted(a.union(b)), 'ee': sum(map(len, both))} out['aa'] = record['aa'] out['cc'] = record['cc'] out['ee'] = record['ee'] out['bb'] = record['bb'] out['dd'] = record['dd'] print(json.dumps(out, separators=(",", ":"), sort_keys=True)) ```