|
Download tasks/program-execution-tracing/contexts/sample-00027.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 383 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00027.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00027.md
-
curl -L -o sample-00027.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00027.md
383 kB
| ### 001 | |
| ```python | |
| def f1(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| 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: | |
| 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 | |
| ``` | |
| ``` | |
| f1(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(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: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f2(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| matches.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| grand += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': buckets, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f3([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f4([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f5([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f6(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f7([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(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 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f8(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f9(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(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 if normalize(value) != normalize(value) or normalize(value) != ''} - excluded | |
| b = {normalize(value) for value in right if normalize(value) != ''} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f10(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5 + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(factors[group]) | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f12([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f13([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(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] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.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 | |
| ``` | |
| ``` | |
| f14([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| 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 | |
| ``` | |
| ``` | |
| f15([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f16(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f17([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(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 col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f18(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f19([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f20([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f21(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(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 | |
| ``` | |
| ``` | |
| f22(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f23([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f24([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = 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 ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f25(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f26([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(rows, keep, factors): | |
| 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: | |
| cost = value * abs(factors[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(dict.fromkeys(kept)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(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 % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f28([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(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 | |
| ``` | |
| ``` | |
| f29(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| 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 | |
| ``` | |
| ``` | |
| f30(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f31([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 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(min(count, 3 + (count - 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 = (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 | |
| ``` | |
| ``` | |
| f33([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(rows, keep, factors): | |
| 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} and value >= 0: | |
| score = value * abs(factors[group]) | |
| kept.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': groupmap, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f35([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f36([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f37([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, 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': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f39(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f40(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 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 | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f41([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f42([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f43([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f44([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f45([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(result))): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper()[:12] | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f49(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(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 = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f50(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(ratemap[group]) | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f52([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(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 board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f53(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(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 | |
| ``` | |
| ``` | |
| f54(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(prices[group]) | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| sumvalue += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f55([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {tidy(value) for value in left} - 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 | |
| ``` | |
| ``` | |
| f56(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper() | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right if canon(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 | |
| ``` | |
| ``` | |
| f57(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(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 or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f58(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right if tidy(value) != ''} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f59(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f60([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(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 {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| 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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f61([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f62(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f63(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f64([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f65([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f66(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(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 | |
| 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 | |
| ``` | |
| ``` | |
| f67(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f68([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f69(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f70(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = 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 | |
| ``` | |
| ``` | |
| f71(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(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: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f72(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right if tidy(value) != ''} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f73(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(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[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f74(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 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 | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f75([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f76(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, 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': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f77(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(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 | |
| ``` | |
| ``` | |
| f78(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f79(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.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(nextmap.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f80([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(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, 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 | |
| ``` | |
| ``` | |
| f81([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f82([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f83([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| result = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f84([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(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 col >= 12 or 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 (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f85(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.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 sorted(routes.get(cur, []))[:20]: | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| 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 | |
| ``` | |
| ``` | |
| f86([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(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} | |
| 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 | |
| ``` | |
| ``` | |
| f87([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f88([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 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 | |
| ``` | |
| ``` | |
| f89([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| 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 | |
| ``` | |
| ``` | |
| f90(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + min(gap, 2): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f91([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(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[:20]: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f93([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f94([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f95(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f96(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| 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 | |
| ``` | |
| ``` | |
| f97(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(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] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f98([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f99(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| 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 += 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 | |
| ``` | |
| ``` | |
| f100([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f101(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(rows, keep, prices): | |
| 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} and value >= 0: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| grand += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': buckets, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f102([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f103(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f104([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper() | |
| banned = {canon(value) for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f105(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| 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], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f106([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f107(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f108([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| 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, 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 | |
| ``` | |
| ``` | |
| f109([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f110([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| 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 | |
| ``` | |
| ``` | |
| f111([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f112([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap) + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f114(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 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[: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 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 | |
| ``` | |
| ``` | |
| f115([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - excluded | |
| b = {tidy(value) for value in right if tidy(value) != ''} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f116(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * ratemap[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f117([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 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 | |
| ``` | |
| ``` | |
| f118([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f119([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f120([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(result))): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f121([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f122([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f123([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices 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 | |
| ``` | |
| ``` | |
| f124([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f125(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(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 if normalize(value) != ''} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f126(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < 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) | |
| 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 | |
| ``` | |
| ``` | |
| f127([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f128(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': lastlevel[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f129([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right if tidy(value) != ''} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f130(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state 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(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f131([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(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] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': edge, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f132([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop 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 | |
| ``` | |
| ``` | |
| f133(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(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])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f134([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f135([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f137(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f138([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(rows, keep, ratemap): | |
| 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 * ratemap[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': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f139([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f140([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f141(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + max(0, gap) + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f142([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(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 | |
| 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 | |
| ``` | |
| ``` | |
| f143(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, 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 | |
| ``` | |
| ``` | |
| f144([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * ratemap[group] | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| sumvalue += score | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f145([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f146(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f147([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f148([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f149([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| 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) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f150(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| result = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f151([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f152(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f153(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f154([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(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])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f156([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f157(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f158([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f159(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f160([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f161([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f162(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (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 | |
| ``` | |
| ``` | |
| f163([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(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])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(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: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f165(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| 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])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f166([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f167(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f168([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f169([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(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 | |
| ``` | |
| ``` | |
| f170(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f171([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(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 | |
| ``` | |
| ``` | |
| f172(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f173(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(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 sorted(children.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 | |
| ``` | |
| ``` | |
| f174([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f175([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(prices[group]) | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f176([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(board): | |
| board = list(board) | |
| known = 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 known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f177(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| 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 | |
| ``` | |
| ``` | |
| f178(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f179(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f180(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| work.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 | |
| ``` | |
| ``` | |
| f181([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f182([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f183([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(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 | |
| ``` | |
| ``` | |
| f184([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right if tidy(value) != ''} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f185(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| 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 | |
| ``` | |
| ``` | |
| f186([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(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])) | |
| 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({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f188(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used = min(64, 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 | |
| ``` | |
| ``` | |
| f189([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f190([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f191([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f192([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f193(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f194([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f195([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f196([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f197([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f198(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f199([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f200([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f201(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(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, 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 | |
| ``` | |
| ``` | |
| f202([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().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 | |
| 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 | |
| ``` | |
| ``` | |
| f203(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f205([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(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 str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * prices[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f207([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += 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 | |
| ``` | |
| ``` | |
| f208([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(board): | |
| board = list(board) | |
| found = 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 found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f211(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(result))): | |
| left = result[pos][0] | |
| gaps.append(min(100, left - result[pos - 1][1])) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f212([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f213([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f214(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(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} - banned | |
| b = {normalize(value) for value in right if normalize(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 | |
| ``` | |
| ``` | |
| f215(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f216([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f217(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.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 | |
| ``` | |
| ``` | |
| f218([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f219([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f220([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f221([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| 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 | |
| ``` | |
| ``` | |
| f222([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right if tidy(value) != ''} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f223(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f224([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f225([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f226(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f227([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not combined or left > combined[-1][1] + min(gap, 2): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f228([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f229([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f230(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f231([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < 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) | |
| 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 | |
| ``` | |
| ``` | |
| f232([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f233([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f234(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(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} | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f235([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f236([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(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 | |
| ``` | |
| ``` | |
| f237(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f238(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(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[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f239(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f240([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| score = value * abs(factors[group]) | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f241([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f242([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f243(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f244([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(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 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 | |
| ``` | |
| ``` | |
| f245([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| 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 | |
| ``` | |
| ``` | |
| f246(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 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 % 100): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = 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 | |
| ``` | |
| ``` | |
| f247([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| ``` | |
| ``` | |
| f248(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f249(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f250([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f251(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f252(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f253([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f254([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f255(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f256([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f257([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f258(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f259([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f260([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f261(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f262([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| 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 | |
| ``` | |
| ``` | |
| f263([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * abs(prices[group]) | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f264([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f265([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(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 | |
| ``` | |
| ``` | |
| f266([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f267(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y = 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 | |
| ``` | |
| ``` | |
| f268([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep} 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], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f269([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f270(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').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 | |
| ``` | |
| ``` | |
| f271(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f272(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| 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(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f273([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(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 | |
| ``` | |
| ``` | |
| f274(['..###.', '#.#..#', '..##..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,2],[1,0],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[15,1,1,1,1]} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f275(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(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 | |
| ``` | |
| ``` | |
| f276(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f277([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(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 or len(word) > 12: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f278(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * ratemap[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 | |
| ``` | |
| ``` | |
| f279([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| 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 | |
| ``` | |
| ``` | |
| f280([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f282(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y = 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 | |
| ``` | |
| ``` | |
| f283([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(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 | |
| ``` | |
| ``` | |
| f284([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(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 | |
| ``` | |
| ``` | |
| f285([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(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, 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 | |
| ``` | |
| ``` | |
| f286([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').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 | |
| 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 | |
| ``` | |
| ``` | |
| f287(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(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 | |
| ``` | |
| ``` | |
| f288([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(rows, keep, factors): | |
| 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(factors[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f289([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(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, 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 | |
| ``` | |
| ``` | |
| f291([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(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) < 2: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['ember']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":5,"delta":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"b":3,"c":5,"d":4,"f":1,"g":6,"h":5},"size":138,"top":["garnet","cyan","helium","delta","amber"]} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f293([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, gap) + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f294([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(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 | |
| ``` | |
| ``` | |
| f295([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(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) | |
| 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 | |
| ``` | |
| ``` | |
| f296([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f297([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f298([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(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] < 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) | |
| 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 | |
| ``` | |
| ``` | |
| f299([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":1,"G":3,"H":2},"frontier":["G"],"order":["A","B","C","F","D","E","H","G"]} | |
| ``` | |
| ### 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 | |
| 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, 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 | |
| ``` | |
| ``` | |
| f300([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(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 | |
| ``` | |
| ``` | |
| f301([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(result))): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((min(100, right - left) for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303([(2, 9), (5, 10), (14, 21), (18, 24), (28, 34), (33, 36), (43, 47), (48, 52), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":37,"holes":[4,4,7,7],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,52],[59,61]]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(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[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f304(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(factors[group]) | |
| kept.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| grand += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f305([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * ratemap[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 | |
| ``` | |
| ``` | |
| f306([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":180,"north":413,"south":80},"top":["BR79","DL42","AN01"],"total":1003} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f307([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f308([(2, 9), (5, 10), (14, 24), (18, 24), (28, 34), (33, 36), (43, 47), (48, 50), (59, 61)], 1) | |
| ``` | |
| ``` | |
| {"cover":35,"holes":[4,4,7,9],"longest":10,"segments":[[2,10],[14,24],[28,36],[43,50],[59,61]]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(board): | |
| board = list(board) | |
| found = 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 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 (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 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 | |
| ``` | |
| ``` | |
| f309(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f310([('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10), ('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":12,"groups":{"core":330,"edge":108,"north":590,"south":80},"top":["AN01","BR53","BR79"],"total":1108} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(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 | |
| ``` | |
| ``` | |
| f311([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10), ('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",104],["AN",67],["CX",64],["GQ",48],["BR",16]],"missing":["EV","FP","HR"],"total":299} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper() | |
| banned = {canon(value) for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right if canon(value) != ''} - banned | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f312(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUE', 'cyanx', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","CYANX","DELTA","FROST","HELIUM","IVORY"],"both":["BLUE","FROST","IVORY"],"left":["CYAN","DELTA"],"right":["AMBER","CYANX","HELIUM"],"score":14} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f313([('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11), ('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7)], [('AN', 7), ('BR', 4), ('CX', 5), ('DL', 10), ('GQ', 4)], 4) | |
| ``` | |
| ``` | |
| {"count":5,"items":[["DL",114],["AN",67],["CX",64],["GQ",32],["BR",16]],"missing":["EV","FP","HR"],"total":293} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f314(['BLUE', 'Ember', 'ivory', 'cyan ', 'delta', 'frost', 'Ember'], ['BLUEx', 'cyan ', 'ivory', 'frost', 'helium', ' Amber', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","BLUEX","CYAN","DELTA","FROST","HELIUM","IVORY"],"both":["CYAN","FROST","IVORY"],"left":["BLUE","DELTA"],"right":["AMBER","BLUEX","HELIUM"],"score":14} | |
| ``` | |
| ### 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 | |
| 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(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f315([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f316([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(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 repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317([('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-4,0,4,10],"end":[3,10],"far":13,"revisits":[[1,4],[3,4],[2,4]],"steps":27} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| 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 | |
| ``` | |
| ``` | |
| f318(['.####.', '##...#', '...#..', '#####.', '.###.#', '.#..#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[2,3],[0,1],[1,5],[4,5],[5,4]],"count":5,"filled":19,"sizes":[10,6,1,1,1]} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f319(['Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium', 'Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium'], ['blue']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"cyan":5,"delta":4,"ember":4,"frost":1,"garnet":6,"helium":5},"initials":{"a":3,"c":5,"d":4,"e":4,"f":1,"g":6,"h":5},"size":146,"top":["garnet","cyan","helium","delta","ember"]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += 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 | |
| ``` | |
| ``` | |
| f320([('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1), ('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-5,6,2],"end":[5,2],"far":10,"revisits":[[-2,-4],[3,-4],[5,-4],[4,-4]],"steps":27} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| board = list(('..###.', '#.#..#') + ('..##..', '#####.', '.###.#', '.#..#.')) | |
| 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)} | |
| clue = record['aa'][0] | |
| route = clue | |
| cases = { | |
| 10: list((('W', 2), ('W', 2), ('N', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1)) + (('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1))), | |
| 15: list((('S', 2), ('W', 2), ('S', 3), ('N', 1), ('E', 2), ('E', 2), ('E', 1), ('N', 1)) + (('S', 1), ('E', 1), ('E', 2), ('W', 2), ('N', 3), ('N', 3), ('E', 1))), | |
| } | |
| moves = cases[route] | |
| delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| points = [[0, 0]] | |
| for direction, count in moves: | |
| dx, dy = delta[direction] | |
| for turn in range(count): | |
| points.append([points[-1][0] + dx, points[-1][1] + dy]) | |
| seen = set() | |
| revisits = [] | |
| for point in points: | |
| key = tuple(point) | |
| if key in seen and point not in revisits: | |
| revisits.append(point) | |
| seen.add(key) | |
| record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} | |
| stamp = record['bb'] | |
| left = list(('BLUE', 'Ember', 'ivory') + ('cyan ', 'delta', 'frost', 'Ember')) | |
| blocked = ['EMBER'] | |
| route = stamp | |
| cases = { | |
| 10: list(('BLUEx', 'cyan ', 'ivory') + ('frost', 'helium', ' Amber', 'Ember')), | |
| 13: list(('BLUE', 'cyanx', 'ivory') + ('frost', 'helium', ' Amber', 'Ember')), | |
| } | |
| right = cases[route] | |
| a = set() | |
| b = set() | |
| ban = set() | |
| for value in blocked: | |
| ban.add(value.strip().upper()) | |
| for value in left: | |
| value = value.strip().upper() | |
| if value not in ban: | |
| a.add(value) | |
| for value in right: | |
| value = value.strip().upper() | |
| if value not in ban: | |
| b.add(value) | |
| both = a.intersection(b) | |
| record = {'aa': sorted(both), 'bb': sorted(a.difference(b)), 'cc': sorted(b.difference(a)), 'dd': sorted(a.union(b)), 'ee': sum(map(len, both))} | |
| ticket = record['aa'][0] | |
| lines = list(('Amber, garnet. garnet ember', 'Delta, garnet. delta delta helium') + ('Cyan, blue. ember frost', 'Helium, amber. helium amber cyan', 'Ember, helium. garnet blue', 'Cyan, cyan. cyan delta ember', 'Garnet, garnet. blue helium')) | |
| route = ticket | |
| cases = { | |
| 'BLUE': ['ember'], | |
| 'CYAN': ['blue'], | |
| } | |
| 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))} | |
| badge = record['dd'] | |
| rows = list((('CX31', 'north', 'ready', 8), ('BR68', 'edge', 'ready', 18), ('DL05', 'south', 'ready', 6), ('DL42', 'core', 'ready', 11), ('BR79', 'core', 'ready', 14), ('AN16', 'edge', 'checked', 18), ('BR53', 'north', 'ready', 16), ('BR90', 'south', 'ready', 10)) + (('AN27', 'core', 'checked', 5), ('CX64', 'edge', 'hold', 8), ('AN01', 'north', 'checked', 17), ('DL38', 'north', 'ready', 8), ('CX75', 'north', 'ready', 10), ('AN12', 'edge', 'void', 24))) | |
| keep = list(('ready',) + ('checked',)) | |
| route = stamp | |
| cases = { | |
| 10: dict((('north', 10), ('south', 5), ('core', 11)) + (('edge', 3),)), | |
| 13: dict((('north', 7), ('south', 5), ('core', 11)) + (('edge', 5),)), | |
| } | |
| rates = 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} | |
| permit = record['bb'] | |
| right = list((('AN', 7), ('BR', 4)) + (('CX', 5), ('DL', 10), ('GQ', 4))) | |
| fee = 4 | |
| route = (badge, permit) | |
| cases = { | |
| (138, 1003): list((('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10)) + (('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7))), | |
| (138, 1108): list((('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10)) + (('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7))), | |
| (146, 1003): list((('AN', 9), ('BR', 3), ('CX', 12), ('DL', 10)) + (('EV', 8), ('FP', 6), ('GQ', 11), ('HR', 7))), | |
| (146, 1108): list((('AN', 9), ('BR', 3), ('CX', 12), ('DL', 11)) + (('EV', 8), ('FP', 6), ('GQ', 7), ('HR', 7))), | |
| } | |
| 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)} | |
| signal = record['bb'] | |
| start = 'A' | |
| limit = 3 | |
| route = signal | |
| cases = { | |
| 293: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('E', 'H'))), | |
| 299: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F')) + (('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'), ('A', 'F'))), | |
| } | |
| 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)} | |
| token = record['aa'][3] | |
| gap = 1 | |
| route = token | |
| cases = { | |
| 'F': list(((2, 9), (5, 10), (14, 21), (18, 24), (28, 34)) + ((33, 36), (43, 47), (48, 52), (59, 61))), | |
| 'D': list(((2, 9), (5, 10), (14, 24), (18, 24), (28, 34)) + ((33, 36), (43, 47), (48, 50), (59, 61))), | |
| } | |
| spans = cases[route] | |
| spans.sort(key=lambda pair: (pair[0], pair[1])) | |
| parts = [] | |
| pos = 0 | |
| while pos < len(spans): | |
| left, right = spans[pos] | |
| pos += 1 | |
| while pos < len(spans) and spans[pos][0] <= right + gap: | |
| right = max(right, spans[pos][1]) | |
| pos += 1 | |
| parts.append([left, right]) | |
| holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] | |
| record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |