|
Download tasks/program-execution-tracing/contexts/sample-00044.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 381 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00044.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00044.md
-
curl -L -o sample-00044.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00044.md
381 kB
| ### 001 | |
| ```python | |
| def f1(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 | |
| ``` | |
| ``` | |
| f1(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f2([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 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 | |
| ``` | |
| ``` | |
| f3([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f4(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f5([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f6([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f7([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f8([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(board): | |
| board = list(board) | |
| known = 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 known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f9(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| 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 | |
| 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 | |
| ``` | |
| ``` | |
| f10(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f12([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f13([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(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 | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f14(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(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])) | |
| leftover = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f15([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f16(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f17(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f18(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f19(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges[:16]: | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(levels) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': edge, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f20([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(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 | |
| ``` | |
| ``` | |
| f21(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f22([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f23(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f24([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f25([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f26([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f28(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f29([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) < 2: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f30(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f31(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(factors[group]) | |
| selected.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| 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 | |
| ``` | |
| ``` | |
| f33([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(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 % 5: | |
| 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': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f35([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f36(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| ``` | |
| f37([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f39(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f40([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(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' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = min(9, 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 | |
| ``` | |
| ``` | |
| f41(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(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] = right | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f42([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f43(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * prices[group] | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f44([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f45([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[: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 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper()[:12] | |
| ignored = {tidy(value) 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 if tidy(value) != ''} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f49([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(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) | |
| 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 | |
| ``` | |
| ``` | |
| f50(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f52(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f53([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f54([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f55(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f56([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f57([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f58([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| 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 | |
| ``` | |
| ``` | |
| f59(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f60([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(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] = 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 | |
| ``` | |
| ``` | |
| f61([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f62(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f63(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(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[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f64(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f65(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(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] = 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 | |
| ``` | |
| ``` | |
| f66([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper()[:12] | |
| excluded = {canon(value) for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right if canon(value) != ''} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f67(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f68(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper()[:12] | |
| 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 | |
| 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 | |
| ``` | |
| ``` | |
| f69(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * prices[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f70([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(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 = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| pending.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f71(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 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: | |
| 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 | |
| ``` | |
| ``` | |
| f72([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f73([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f74([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(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({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f75([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f76([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(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[0])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 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 | |
| ``` | |
| ``` | |
| f77(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + max(0, 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 | |
| ``` | |
| ``` | |
| f78([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f79([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f80(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f81(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| result = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f82([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f83([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f84([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(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] + 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(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 | |
| ``` | |
| ``` | |
| f85([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f86([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, 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 | |
| ``` | |
| ``` | |
| f87([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f88(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(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 col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f89(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| 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 | |
| ``` | |
| ``` | |
| f90([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[: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': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f91(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f93(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 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 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f94([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f95([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f96([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in 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) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f97(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f98([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(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) | |
| 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 | |
| ``` | |
| ``` | |
| f99([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f100([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - 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 | |
| ``` | |
| ``` | |
| f101(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f102([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f103(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f104([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f105(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(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 += 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 | |
| ``` | |
| ``` | |
| f106([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(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 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 | |
| ``` | |
| ``` | |
| f107([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f108(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f109([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f110([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f111([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f112(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(factors[group]) | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| sumvalue += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f114([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(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 | |
| 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 | |
| ``` | |
| ``` | |
| f115(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(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 | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f116([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).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 | |
| 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 | |
| ``` | |
| ``` | |
| f117(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f118([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(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 | |
| ``` | |
| ``` | |
| f119(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f120([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f121([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(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, 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 | |
| ``` | |
| ``` | |
| f122([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f123([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f124(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f125(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f126(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f127(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(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 | |
| ``` | |
| ``` | |
| f128([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f129(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(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 | |
| 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 | |
| ``` | |
| ``` | |
| f130(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f131([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(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 | |
| ``` | |
| ``` | |
| f132([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(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': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f133([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(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 % 100): | |
| 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 = 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 | |
| ``` | |
| ``` | |
| f134([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f135([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': lastlevel[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f137([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| score = value * prices[group] | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f138([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f139([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f140([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f141([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f142(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(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 = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f143([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f144([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(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 = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f145(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| 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 = (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 | |
| ``` | |
| ``` | |
| f146([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f147([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(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: | |
| 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 | |
| ``` | |
| ``` | |
| f148(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f149(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f150(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f151([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(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': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) < 2: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f152(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f153([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f154([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(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])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f156([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f157(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f158([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f159([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(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] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| ``` | |
| f160([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(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] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| ``` | |
| f161([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f162([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f163([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(factors[group]) | |
| kept.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| grand += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| 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 | |
| ``` | |
| ``` | |
| f165(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper()[:12] | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f166(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': edge[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f167([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f168([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(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(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f169([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f170([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f171(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(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 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 | |
| ``` | |
| ``` | |
| f172([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f173([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f174([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f175(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f176([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f177([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f178([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(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 | |
| ``` | |
| ``` | |
| f179([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f180([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(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] = right | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f181([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| 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 | |
| ``` | |
| ``` | |
| f182([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f183([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f184([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f185([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(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 | |
| 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 | |
| ``` | |
| ``` | |
| f186([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(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 | |
| ``` | |
| ``` | |
| f188(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f189([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = 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 | |
| ``` | |
| ``` | |
| f190(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f191([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f192([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f193([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| 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 % 100): | |
| 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, 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 | |
| ``` | |
| ``` | |
| f194([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 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 = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f195([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| 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 = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f196([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(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 | |
| ``` | |
| ``` | |
| f197([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f198(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| charge = value * prices[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f199([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f200(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 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 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f201([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f202([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * prices[group] | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f203([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: | |
| score = value * abs(prices[group]) | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f205([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f207(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(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 | |
| ``` | |
| ``` | |
| f208([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f211([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(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] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f212([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(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 = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f213(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(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] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f214([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(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: | |
| 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 | |
| ``` | |
| ``` | |
| f215([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f216([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| 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) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f217(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(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: | |
| 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 | |
| ``` | |
| ``` | |
| f218([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(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[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f219(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f220([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f221([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - excluded | |
| b = {tidy(value) for value in right if tidy(value) != ''} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f222(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f223([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f224(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f225(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f226([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f227([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - banned | |
| b = {tidy(value) for value in right if tidy(value) != ''} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f228(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(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 = 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 | |
| ``` | |
| ``` | |
| f229([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| 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 | |
| ``` | |
| ``` | |
| f230(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f231([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(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 | |
| ``` | |
| ``` | |
| f232([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f233(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f234([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f235(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f236([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f237([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(factors[group]) | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f238([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(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) | |
| 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 | |
| ``` | |
| ``` | |
| f239(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(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 | |
| 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 | |
| ``` | |
| ``` | |
| f240(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f241([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right if normalize(value) != ''} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f242(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f243(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(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, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, 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 | |
| ``` | |
| ``` | |
| f244([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| banned = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f245(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f246([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(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 | |
| 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 | |
| ``` | |
| ``` | |
| f247(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(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 = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = max(far, 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 | |
| ``` | |
| ``` | |
| f248([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(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({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f249([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f250(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f251([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f252(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f253([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * factors[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f254([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += 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 | |
| ``` | |
| ``` | |
| f255([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f256([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(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 = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f257(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f258(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| score = value * prices[group] | |
| matches.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f259([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f260(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f261(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f262([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f263([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f264([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[: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 | |
| ``` | |
| ``` | |
| f265(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f266([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f267([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(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 = 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 | |
| ``` | |
| ``` | |
| f268([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f269(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f270([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f271([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(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 and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f272([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f273(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f274([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 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(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 | |
| ``` | |
| ``` | |
| f275([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(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 = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f276(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * ratemap[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f277([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(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 and count > 0: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f278([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f279([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(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 | |
| ``` | |
| ``` | |
| f280([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f282(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f283(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(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 = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f284(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(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 | |
| 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 | |
| ``` | |
| ``` | |
| f285(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| 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])) | |
| 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 | |
| ``` | |
| ``` | |
| f286([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 12), ('EV', 12), ('FP', 3), ('GQ', 4), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",111],["GQ",39],["CX",33],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":220} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f287(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).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 | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f288(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f289(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f291([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * abs(ratemap[group]) | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(factors[group]) | |
| selected.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f293([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f294([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f295([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f296(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f297([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(board): | |
| board = list(board) | |
| known = 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 known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f298(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f299([('W', 2), ('N', 3), ('N', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-7,-4,2,5],"end":[-7,-4],"far":11,"revisits":[[1,5],[0,5],[-1,5],[-2,2],[-1,2]],"steps":33} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f300([('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":96,"south":180},"top":["CX29","BR55","CX40"],"total":540} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f301([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 2) | |
| ``` | |
| ``` | |
| {"count":6,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2},"frontier":["D","E","F"],"order":["A","B","C","D","E","F"]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f304(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': edge, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f305([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 4) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(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 | |
| 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 | |
| ``` | |
| ``` | |
| f306(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f307([(3, 9), (6, 12), (15, 20), (19, 22), (29, 32), (34, 39), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":42,"holes":[3,7,5,7],"longest":10,"segments":[[3,12],[15,22],[29,39],[44,53],[60,67]]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f308(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"delta":6,"ember":5,"frost":6,"garnet":2,"helium":1},"initials":{"a":5,"b":1,"d":6,"e":5,"f":6,"g":2,"h":1},"size":132,"top":["delta","frost","amber","ember","garnet"]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f309([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f310(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, 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 | |
| ``` | |
| ``` | |
| f311([('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1), ('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3)]) | |
| ``` | |
| ``` | |
| {"bounds":[-5,-10,4,1],"end":[-5,-10],"far":15,"revisits":[[0,-1],[0,0],[3,-1],[2,-1],[1,-1]],"steps":33} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f312([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f313(['Delta, frost. blue garnet', 'Frost, cyan. amber frost amber', 'Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan'], ['helium']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":5,"blue":1,"cyan":5,"delta":6,"ember":5,"frost":6,"garnet":2},"initials":{"a":5,"b":1,"c":5,"d":6,"e":5,"f":6,"g":2},"size":146,"top":["delta","frost","amber","cyan","ember"]} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f314(['Ember', 'frostx', 'helium', 'ivory', 'cyan ', 'delta', 'Ember'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","FROST","FROSTX","HELIUM","IVORY"],"both":["CYAN","DELTA","IVORY"],"left":["FROSTX","HELIUM"],"right":["AMBER","FROST"],"score":14} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(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 = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f315(['.#....', '.#.#..', '..##..', '##.#.#', '...#.#', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[3,5],[5,1]],"count":5,"filled":13,"sizes":[6,2,2,2,1]} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| 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((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 | |
| ``` | |
| ``` | |
| f316([(3, 9), (6, 12), (15, 20), (19, 23), (29, 32), (34, 36), (44, 48), (49, 53), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[3,6,8,7],"longest":9,"segments":[[3,12],[15,23],[29,36],[44,53],[60,67]]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(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 = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317(['.#....', '.#.#..', '..##..', '##.#..', '...###', '.#.#..']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,1],[3,0],[5,1]],"count":4,"filled":13,"sizes":[8,2,2,1]} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(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 | |
| ``` | |
| ``` | |
| f318([('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16), ('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4)], [('CX', 5), ('DL', 9), ('FP', 5), ('GQ', 9), ('HR', 4)], 3) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",147],["CX",33],["GQ",21],["HR",19],["FP",18]],"missing":["AN","BR","EV"],"total":238} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f319([('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4), ('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":9,"groups":{"core":264,"edge":102,"south":165},"top":["CX29","BR55","CX40"],"total":531} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(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 | |
| 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 | |
| ``` | |
| ``` | |
| f320(['Ember', 'frost', 'helium', 'ivory', 'cyan ', 'delta', 'Emberx'], [' Amber', 'Ember', 'delta', 'ivory', 'cyan ', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","CYAN","DELTA","EMBERX","FROST","HELIUM","IVORY"],"both":["CYAN","DELTA","FROST","IVORY"],"left":["EMBERX","HELIUM"],"right":["AMBER"],"score":19} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| left = list((('AN', 12), ('BR', 5), ('CX', 6), ('DL', 16)) + (('EV', 12), ('FP', 3), ('GQ', 2), ('HR', 4))) | |
| right = list((('CX', 5), ('DL', 9)) + (('FP', 5), ('GQ', 9), ('HR', 4))) | |
| fee = 3 | |
| joined = [] | |
| for key, count in left: | |
| matches = [rate for name, rate in right if name == key] | |
| if matches: | |
| joined.append([key, count * matches[0] + fee]) | |
| joined.sort(key=lambda item: (-item[1], item[0])) | |
| rightkeys = {key for key, rate in right} | |
| record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)} | |
| stamp = record['bb'] | |
| right = list((' Amber', 'Ember') + ('delta', 'ivory', 'cyan ', 'frost', 'Ember')) | |
| blocked = ['EMBER'] | |
| route = stamp | |
| cases = { | |
| 238: list(('Ember', 'frost') + ('helium', 'ivory', 'cyan ', 'delta', 'Emberx')), | |
| 220: list(('Ember', 'frostx') + ('helium', 'ivory', 'cyan ', 'delta', '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))} | |
| ticket = record['ee'] | |
| moves = list((('S', 2), ('N', 3), ('S', 2), ('E', 1), ('E', 3), ('W', 3), ('S', 3), ('W', 1)) + (('E', 2), ('S', 1), ('S', 2), ('S', 3), ('W', 1), ('W', 3), ('W', 3))) | |
| delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| points = [[0, 0]] | |
| for direction, count in moves: | |
| dx, dy = delta[direction] | |
| for turn in range(count): | |
| points.append([points[-1][0] + dx, points[-1][1] + dy]) | |
| seen = set() | |
| revisits = [] | |
| for point in points: | |
| key = tuple(point) | |
| if key in seen and point not in revisits: | |
| revisits.append(point) | |
| seen.add(key) | |
| record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} | |
| badge = record['bb'] | |
| edges = list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'))) | |
| start = 'A' | |
| route = badge | |
| cases = { | |
| 15: 4, | |
| 11: 2, | |
| } | |
| limit = cases[route] | |
| links = {} | |
| for edge in edges: | |
| links.setdefault(edge[0], []).append(edge[1]) | |
| distance = {start: 0} | |
| queue = deque([start]) | |
| while queue: | |
| node = queue.popleft() | |
| if distance[node] == limit: | |
| continue | |
| for child in sorted(links.get(node, [])): | |
| if child in distance: | |
| continue | |
| distance[child] = distance[node] + 1 | |
| queue.append(child) | |
| furthest = max(distance.values()) | |
| record = {'aa': list(distance), 'bb': distance, 'cc': sorted((node for node in distance if distance[node] == furthest)), 'dd': len(distance)} | |
| permit = record['dd'] | |
| lines = list(('Delta, frost. blue garnet', 'Frost, cyan. amber frost amber') + ('Amber, cyan. frost ember', 'Garnet, cyan. cyan ember helium', 'Delta, delta. ember frost', 'Delta, amber. frost ember amber', 'Delta, delta. ember cyan')) | |
| route = permit | |
| cases = { | |
| 6: ['cyan'], | |
| 8: ['helium'], | |
| } | |
| stop = cases[route] | |
| normalized = set() | |
| for item in stop: | |
| normalized.add(item.lower()) | |
| stop = normalized | |
| words = [] | |
| for line in lines: | |
| words.extend(re.findall('[a-z]+', line.lower())) | |
| words = [word for word in words if word not in stop] | |
| counts = {} | |
| initials = {} | |
| for word in words: | |
| counts[word] = counts.get(word, 0) + 1 | |
| initials[word[0]] = initials.get(word[0], 0) + 1 | |
| top = sorted(counts, key=lambda word: (-counts[word], word))[:5] | |
| record = {'aa': top, 'bb': counts, 'cc': initials, 'dd': sum((len(word) for word in words))} | |
| signal = record['dd'] | |
| gap = 2 | |
| route = (ticket, signal) | |
| cases = { | |
| (19, 146): list(((3, 9), (6, 12), (15, 20), (19, 22), (29, 32)) + ((34, 39), (44, 48), (49, 53), (60, 67))), | |
| (19, 132): list(((3, 9), (6, 12), (15, 20), (19, 23), (29, 32)) + ((34, 36), (44, 48), (49, 53), (60, 67))), | |
| (14, 146): list(((3, 9), (6, 12), (15, 20), (19, 23), (29, 32)) + ((34, 36), (44, 48), (49, 53), (60, 67))), | |
| (14, 132): list(((3, 9), (6, 12), (15, 20), (19, 23), (29, 32)) + ((34, 36), (44, 48), (49, 53), (60, 67))), | |
| } | |
| spans = cases[route] | |
| spans.sort(key=lambda pair: (pair[0], pair[1])) | |
| parts = [] | |
| pos = 0 | |
| while pos < len(spans): | |
| left, right = spans[pos] | |
| pos += 1 | |
| while pos < len(spans) and spans[pos][0] <= right + gap: | |
| right = max(right, spans[pos][1]) | |
| pos += 1 | |
| parts.append([left, right]) | |
| holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] | |
| record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} | |
| token = record['cc'] | |
| route = token | |
| cases = { | |
| 40: list(('.#....', '.#.#..', '..##..') + ('##.#..', '...###', '.#.#..')), | |
| 42: list(('.#....', '.#.#..', '..##..') + ('##.#.#', '...#.#', '.#.#..')), | |
| } | |
| board = cases[route] | |
| cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#'] | |
| parent = {cell: cell for cell in cells} | |
| def root(cell): | |
| while parent[cell] != cell: | |
| parent[cell] = parent[parent[cell]] | |
| cell = parent[cell] | |
| return cell | |
| def merge(a, b): | |
| a = root(a) | |
| b = root(b) | |
| if a != b: | |
| parent[b] = a | |
| for row, col in cells: | |
| for other in ((row - 1, col), (row, col - 1)): | |
| if other in parent: | |
| merge((row, col), other) | |
| groups = {} | |
| for cell in cells: | |
| groups.setdefault(root(cell), []).append(cell) | |
| parts = [(len(group), min(group)) for group in groups.values()] | |
| parts.sort(key=lambda item: (-item[0], item[1])) | |
| record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)} | |
| memo = record['dd'] | |
| keep = list(('ready',) + ('checked',)) | |
| rates = dict((('north', 7),) + (('south', 5), ('core', 11), ('edge', 3))) | |
| route = memo | |
| cases = { | |
| 5: list((('DL59', 'south', 'ready', 11), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4)) + (('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 24), ('CX40', 'core', 'ready', 9))), | |
| 4: list((('DL59', 'south', 'ready', 8), ('DL96', 'core', 'ready', 4), ('AN33', 'edge', 'void', 9), ('AN70', 'core', 'void', 16), ('AN07', 'core', 'hold', 17), ('DL44', 'edge', 'ready', 4)) + (('DL81', 'core', 'hold', 5), ('DL18', 'edge', 'checked', 4), ('BR55', 'south', 'ready', 22), ('CX92', 'edge', 'hold', 4), ('CX29', 'core', 'checked', 11), ('BR66', 'south', 'checked', 3), ('CX03', 'edge', 'ready', 26), ('CX40', 'core', 'ready', 9))), | |
| } | |
| rows = cases[route] | |
| chosen = [(code, group, value) for code, group, state, value in rows if state in keep] | |
| priced = [(code, group, value * rates[group]) for code, group, value in chosen] | |
| groups = {} | |
| for code, group, price in priced: | |
| groups[group] = groups.get(group, 0) + price | |
| ranked = sorted(priced, key=lambda row: (-row[2], row[0])) | |
| record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |