|
Download tasks/program-execution-tracing/contexts/sample-00009.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 389 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00009.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00009.md
-
curl -L -o sample-00009.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00009.md
389 kB
| ### 001 | |
| ```python | |
| def f1(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] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f1([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(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 = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f2(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f3(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f4([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f5([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f6(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| 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] | |
| kept.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f7([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) < 2: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f8(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(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 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f9([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(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} - ignored | |
| b = {tidy(value) for value in right if tidy(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f10(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f11([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f12(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f13([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 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 | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f14([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * abs(prices[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f15([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f16([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(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) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f17(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f18([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f19([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f20(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f21([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f22(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(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(matches), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f23([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f24([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 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)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f25([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(rows, keep, factors): | |
| 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 str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * factors[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': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f26([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(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 = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f27(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(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 limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f28([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f29([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 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[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f30(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f31(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f32(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(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 += 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f33([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 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 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f34([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f35([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f36(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f37(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(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])) | |
| leftover = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f38([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f39(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 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)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f40([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(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 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f41(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f42([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f43([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(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 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f44(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').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 | |
| ``` | |
| Example call: | |
| ``` | |
| f45(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f46([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f47(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f48(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f49([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| grand = 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 | |
| grand += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f50([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f51([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f52([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f53([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f54(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(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, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f55([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f56(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(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 % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f57([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f58([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f59([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges[:16]: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(routes.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f60([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f61(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f62([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f63([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': edge, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f64([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f65(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f66(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f67(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f68(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f69([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f70([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f71(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f72([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[: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 | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f73(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f74([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in 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) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f75(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f76([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f77(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(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])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f78(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f79(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f80([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f81(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(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 = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f82([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(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}: | |
| cost = value * prices[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(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f83([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f84([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f85([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f86([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(board): | |
| board = list(board) | |
| known = 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 known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f87(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(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 if normalize(value) != ''} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f88(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f89(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(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[: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 | |
| ``` | |
| Example call: | |
| ``` | |
| f90([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[: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 | |
| ``` | |
| Example call: | |
| ``` | |
| f91(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f92([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state 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': list(dict.fromkeys((code for code, charge in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f93([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f94([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(ratemap[group]) | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f95([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f96([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| 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 | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f97(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f98([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors and count > 0: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f99([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(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])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f100([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f101([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f102(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f103(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f104(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f105(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f106([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f107([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f108(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f109(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f110([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 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 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f111([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f112(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.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 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f113([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * ratemap[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': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f114([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(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 = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f115([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f116([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f117(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f118([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f119([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(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 = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f120(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f121([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(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 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f122(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f123([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f124([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f125([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(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': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f126([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f127(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f128([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(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) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f129(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f130([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(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) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f131(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f132(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f133(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(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] = min(9, frequency.get(word, 0) + 1) | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f134(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| 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 * abs(prices[group]) | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f135([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f136([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f137(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f138([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(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) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f139(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| banned = {canon(value) for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right if canon(value) != ''} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f140(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(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 = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f141(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f142(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f143([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f144([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f145([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f146([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f147([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(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 if normalize(value) != normalize(value) or normalize(value) != ''} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f148(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f149(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f150(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(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 and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f151([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors and count > 0: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f152([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f153(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f154(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(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: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f155(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f156([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y = max(-64, min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f157([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f158([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(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 = [] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f159(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f160(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f161([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f162(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges[:16]: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.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 | |
| 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[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f163([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f164(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f165([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(ratemap[group]) | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f166([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f167(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f168([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f169([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(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 = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f170(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(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 sorted(children.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f171([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f172(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(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} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f173([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| 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 += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f174([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f175([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(board): | |
| board = list(board) | |
| found = 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 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) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f176(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f177([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| ``` | |
| Example call: | |
| ``` | |
| f178(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 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)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f179([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f180([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f181(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(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} if any((start in edge for edge in edges)) else {} | |
| 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: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f182([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(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 * abs(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f183([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f184([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(totals) if value > limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f185([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f186([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f187([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f188([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f189([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f190([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f191([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f192([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f193([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f194(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f195(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f196(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f197([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f198([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f199(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f200([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f201(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f202([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f203([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f204([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f205(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(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, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f206([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f207([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f208([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f209([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f210([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f211([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f212([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f213([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f214(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f215(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f216([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(factors[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f217([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y = 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, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f218([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f219([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f220([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f221([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f222([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f223(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f224(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f225([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f226([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f227([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f228(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(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 | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f229(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * ratemap[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(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f230([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f231([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f232(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f233([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f234(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f235(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(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} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f236(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(ratemap[group]) | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f237([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right if normalize(value) != ''} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f238(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f239([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(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])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f240([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f241(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f242(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f243([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f244(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| 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) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f245(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value > limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f246([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(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 = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f247([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f248([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(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) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f249(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f250(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f251([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f252([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f253([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(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} | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f254([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(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])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f255([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').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 | |
| ``` | |
| Example call: | |
| ``` | |
| f256(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f257([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {canon(value) for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f258(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(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 = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f259([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| 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) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f260(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} if any((start in edge for edge in edges)) else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f261([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f262([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f263([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f264([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f265([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f266(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {canon(value) 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f267(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(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 | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f268(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(series) if value > limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f269([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f270([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(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} - 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f271(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f272([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.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': groupmap, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f273([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(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} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f274(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f275([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f276([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f277([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(rows, keep, ratemap): | |
| 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}: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f278([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f279([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f280([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(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] = min(9, frequency.get(word, 0) + 1) | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f281(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(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 = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f282([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(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[:20]: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f283([('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["EV",64],["BR",54],["FP",53],["GQ",40],["CX",34]],"missing":["DL"],"total":281} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(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 if canon(value) != canon(value) or canon(value) != ''} - 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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f284(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f285([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value > limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f286([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| charge = value * prices[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f287([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f288(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(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 sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f289([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(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])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f290(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['amber', 'blue']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"cyan":3,"delta":5,"ember":5,"frost":3,"garnet":4,"helium":4},"initials":{"c":3,"d":5,"e":5,"f":3,"g":4,"h":4},"size":125,"top":["delta","ember","garnet","helium","cyan"]} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f291([('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8), ('FP', 7), ('GQ', 12), ('HR', 4)], [('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5), ('FP', 7), ('GQ', 3), ('HR', 5)], 4) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"items":[["BR",64],["FP",53],["EV",44],["GQ",40],["CX",34]],"missing":["DL"],"total":271} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f292([20, 24, 12, 20, 11, 21, 22, 19, 16, 10, 18, 16, 7, 22, 9], 3, 46) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,43,52,54,62,57,45,44,44,41,45,38]} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f293(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f294([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f295([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f296(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","HELIUM","IVORY"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| 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 = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f297([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(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 sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f298([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f299(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(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 = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f300([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if 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) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f301(['#...##', '.###..', '#..#.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,1],[2,5],[0,4],[0,0],[5,3]],"count":6,"filled":18,"sizes":[7,4,3,2,1,1]} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f302([20, 24, 12, 20, 15, 21, 22, 19, 16, 10, 18, 16, 7, 19, 9], 3, 48) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":62,"hits":[0,1,3,4,5,6],"rises":4,"sums":[56,56,47,56,58,62,57,45,44,44,41,42,35]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f303(['Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta', 'Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember'], ['frost', 'garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":3,"blue":4,"cyan":3,"delta":5,"ember":5,"helium":4},"initials":{"a":3,"b":4,"c":3,"d":5,"e":5,"h":4},"size":117,"top":["delta","ember","blue","helium","amber"]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| 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] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f304([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f305([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(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 {} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f306([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f307(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y = 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 = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f308([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,12],"end":[-1,10],"far":15,"revisits":[[1,4],[-3,11],[-3,10],[-3,9],[-4,10]],"steps":29} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f309([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":329,"south":40},"top":["DL37","DL59","DL63"],"total":619} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f310([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f311([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f312(['#...##', '.#.#..', '#.##.#', '#...##', '###...', '##.#..']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,0],[1,3],[2,5],[0,4],[0,0],[1,1],[5,3]],"count":7,"filled":18,"sizes":[7,3,3,2,1,1,1]} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f313([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f314([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f315([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f316([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":1,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","E","D","F","G","H"]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: | |
| score = value * ratemap[group] | |
| matches.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| grand += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': buckets, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f317([('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8), ('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":8,"groups":{"core":88,"edge":162,"north":357,"south":25},"top":["DL37","DL59","DL63"],"total":632} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| banned = {canon(value) for value in banned} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f318(['ivory', ' Amber', 'Ember', 'frost', 'GARNET', 'helium', 'Ember'], ['delta', ' Amber', 'ivory', 'cyan ', 'BLUE', 'helium', 'Ember'], ['ivory']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["AMBER","EMBER","HELIUM"],"left":["FROST","GARNET"],"right":["BLUE","CYAN","DELTA"],"score":16} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f319([('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3), ('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-5,0,2,14],"end":[-4,12],"far":17,"revisits":[[1,4],[-3,13],[-3,12],[-3,11],[-4,12]],"steps":28} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f320([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C')], 'A', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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"]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| left = list(('ivory', ' Amber', 'Ember', 'frost') + ('GARNET', 'helium', 'Ember')) | |
| right = list(('delta', ' Amber', 'ivory', 'cyan ') + ('BLUE', 'helium', 'Ember')) | |
| blocked = ['EMBER'] | |
| 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))} | |
| width = 3 | |
| route = record['aa'][1] | |
| cases = { | |
| 'EMBER': (list((20, 24, 12, 20, 15, 21, 22) + (19, 16, 10, 18, 16, 7, 19, 9)), 48), | |
| 'HELIUM': (list((20, 24, 12, 20, 11, 21, 22) + (19, 16, 10, 18, 16, 7, 22, 9)), 46), | |
| } | |
| values, limit = cases[route] | |
| current = sum(values[:width]) | |
| sums = [current] | |
| for pos in range(width, len(values)): | |
| current += values[pos] - values[pos - width] | |
| sums.append(current) | |
| hits = [] | |
| rises = 0 | |
| for pos, value in enumerate(sums): | |
| if value >= limit: | |
| hits.append(pos) | |
| if pos and value > sums[pos - 1]: | |
| rises += 1 | |
| record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} | |
| start = 'A' | |
| limit = 3 | |
| route = record['dd'][2] | |
| cases = { | |
| 43: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G')) + (('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('G', 'C'))), | |
| 47: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G')) + (('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'E'))), | |
| } | |
| edges = cases[route] | |
| links = {} | |
| for edge in edges: | |
| links.setdefault(edge[0], []).append(edge[1]) | |
| distance = {start: 0} | |
| queue = deque([start]) | |
| while queue: | |
| node = queue.popleft() | |
| if distance[node] == limit: | |
| continue | |
| for child in sorted(links.get(node, [])): | |
| if child in distance: | |
| continue | |
| distance[child] = distance[node] + 1 | |
| queue.append(child) | |
| furthest = max(distance.values()) | |
| record = {'aa': list(distance), 'bb': distance, 'cc': sorted((node for node in distance if distance[node] == furthest)), 'dd': len(distance)} | |
| lines = list(('Frost, frost. blue amber', 'Ember, helium. delta helium ember', 'Ember, garnet. garnet delta', 'Blue, amber. blue ember delta') + ('Helium, helium. cyan delta', 'Frost, cyan. blue cyan delta', 'Amber, garnet. garnet ember')) | |
| route = record['aa'][3] | |
| cases = { | |
| 'D': list(('amber',) + ('blue',)), | |
| 'E': list(('frost',) + ('garnet',)), | |
| } | |
| 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))} | |
| route = record['dd'] | |
| cases = { | |
| 125: list((('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3)) + (('N', 3), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 1))), | |
| 117: list((('E', 1), ('N', 1), ('N', 3), ('E', 1), ('W', 3), ('W', 2), ('N', 3)) + (('N', 1), ('N', 2), ('N', 2), ('S', 1), ('S', 2), ('N', 1), ('W', 2), ('E', 4))), | |
| } | |
| moves = cases[route] | |
| delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| points = [[0, 0]] | |
| for direction, count in moves: | |
| dx, dy = delta[direction] | |
| for turn in range(count): | |
| points.append([points[-1][0] + dx, points[-1][1] + dy]) | |
| seen = set() | |
| revisits = [] | |
| for point in points: | |
| key = tuple(point) | |
| if key in seen and point not in revisits: | |
| revisits.append(point) | |
| seen.add(key) | |
| record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} | |
| keep = list(('ready',) + ('checked',)) | |
| rates = dict((('north', 7),) + (('south', 5), ('core', 11), ('edge', 3))) | |
| route = record['bb'] | |
| cases = { | |
| 17: list((('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8)) + (('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 21), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 8), ('DL59', 'north', 'checked', 14))), | |
| 15: list((('BR78', 'south', 'void', 18), ('DL15', 'north', 'ready', 12), ('DL52', 'north', 'hold', 21), ('BR89', 'edge', 'void', 19), ('DL26', 'edge', 'void', 16), ('DL63', 'core', 'ready', 8)) + (('DL00', 'edge', 'checked', 18), ('DL37', 'north', 'ready', 25), ('BR74', 'north', 'hold', 23), ('DL11', 'edge', 'checked', 17), ('AN48', 'edge', 'ready', 19), ('DL85', 'core', 'hold', 10), ('DL22', 'south', 'checked', 5), ('DL59', 'north', 'checked', 14))), | |
| } | |
| 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} | |
| route = record['bb'] | |
| cases = { | |
| 632: list(('#...##', '.###..', '#..#.#') + ('#...##', '###...', '##.#..')), | |
| 619: 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)} | |
| right = list((('AN', 4), ('BR', 10), ('CX', 6), ('EV', 5)) + (('FP', 7), ('GQ', 3), ('HR', 5))) | |
| fee = 4 | |
| route = record['dd'] | |
| cases = { | |
| 7: list((('AN', 2), ('BR', 5), ('CX', 5), ('DL', 10), ('EV', 12)) + (('FP', 7), ('GQ', 12), ('HR', 4))), | |
| 6: list((('AN', 2), ('BR', 6), ('CX', 5), ('DL', 10), ('EV', 8)) + (('FP', 7), ('GQ', 12), ('HR', 4))), | |
| } | |
| left = cases[route] | |
| joined = [] | |
| for key, count in left: | |
| matches = [rate for name, rate in right if name == key] | |
| if matches: | |
| joined.append([key, count * matches[0] + fee]) | |
| joined.sort(key=lambda item: (-item[1], item[0])) | |
| rightkeys = {key for key, rate in right} | |
| record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)} | |
| out['aa'] = record['aa'] | |
| out['cc'] = record['cc'] | |
| out['bb'] = record['bb'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |