### 001 ```python def f1(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (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 ``` ``` f1([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 002 ```python def f2(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) 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 ``` ``` f2(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 003 ```python def f3(rows, keep, ratemap): rows = list(rows) kept = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * ratemap[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 ``` ``` f3([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 004 ```python def f4(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 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 ``` ``` f4(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 005 ```python def f5(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges[:16]: nextmap.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(nextmap.get(cur, []))[:20]: if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f5([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 006 ```python def f6(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * ratemap[group] kept.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost grand += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f6([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 007 ```python def f7(spans, gap): spans = list(spans) segments = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = max(0, segments[-1][1], right) breaks = [] for pos in range(1, min(50, len(segments))): left = segments[pos][0] breaks.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f7([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 008 ```python def f8(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += 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 ``` ``` f8([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 009 ```python def f9(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f9([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 010 ```python def f10(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' \n').upper() excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f10(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 011 ```python def f11(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 012 ```python def f12(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 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 ``` ``` f12(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 013 ```python def f13(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 if normalize(value) != ''} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f13(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 014 ```python def f14(rows, keep, prices): rows = list(rows) selected = [] buckets = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * prices[group] selected.append((code, score)) buckets[group] = buckets.get(group, 0) + score grand += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': buckets, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f14([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 015 ```python def f15(rows, keep, prices): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state 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], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f15([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 016 ```python def f16(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f16([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 017 ```python def f17(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 ``` ``` f17([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 018 ```python def f18(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f18(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 019 ```python def f19(rows, keep, prices): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * prices[group] matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f19([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 020 ```python def f20(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue pending.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f20(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 021 ```python def f21(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = max(0, 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 ``` ``` f21([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 022 ```python def f22(left, right, ignored): left = list(left) def canon(value): return ' '.join(str(value).split()).upper()[:12] ignored = {canon(value) for value in ignored} a = {canon(value) for value in left} - ignored b = {canon(value) for value in right if canon(value) != ''} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f22(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 023 ```python def f23(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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 ``` ``` f23([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 024 ```python def f24(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) 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 ``` ``` f24(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 025 ```python def f25(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])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f25([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 026 ```python def f26(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = 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 ``` ``` f26([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 027 ```python def f27(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 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 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 ``` ``` f27([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 028 ```python def f28(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 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 ``` ``` f28(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 029 ```python def f29(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) levels = {start: 0} if any((start in edge for edge in edges)) else {} work = [start] while work: cur = work.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: if nxt not in levels: 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 ``` ``` f29([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 030 ```python def f30(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 combined = [] for key, count in left[:20]: if key in prices: combined.append((key, count * prices[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) 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 ``` ``` f30([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 031 ```python def f31(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 limit > 0 or limit < 0 else {} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(routes.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) listing = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f31([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 032 ```python def f32(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) 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([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 033 ```python def f33(board): board = list(board) known = set() regions = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] 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) 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 ``` ``` f33(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 034 ```python def f34(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): nextmap.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(nextmap.get(cur, [])): 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 ``` ``` f34([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 035 ```python def f35(board): board = list(board) found = set() regions = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue queue.append(nxt) 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 ``` ``` f35(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 036 ```python def f36(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} grand = 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 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([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 037 ```python def f37(rows, keep, prices): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * abs(prices[group]) kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f37([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 038 ```python def f38(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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f38(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 039 ```python def f39(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 if normalize(value) != ''} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f39(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 040 ```python def f40(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 ``` ``` f40([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 041 ```python def f41(board): board = list(board) found = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f41(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 042 ```python def f42(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 ``` ``` f42([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 043 ```python def f43(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])) unmatched = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` ``` f43([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 044 ```python def f44(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 = 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 ``` ``` f44([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 045 ```python def f45(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (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 ``` ``` f45([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 046 ```python def f46(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f46([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 047 ```python def f47(left, right, excluded): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b 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 ``` ``` f47(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 048 ```python def f48(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] < 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) 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 ``` ``` f48([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 049 ```python def f49(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 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 ``` ``` f49(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 050 ```python def f50(board): board = list(board) found = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] 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) 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":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 051 ```python def f51(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} work = [start] while work: cur = work.pop(0) if steps[cur] < limit: for nxt in sorted(nextmap.get(cur, []))[:20]: if nxt not in steps: steps[nxt] = steps[cur] + 1 work.append(nxt) sequence = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest[:3], 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f51([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 052 ```python def f52(board): board = list(board) found = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f52(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 053 ```python def f53(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used = 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, 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 ``` ``` f53([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 054 ```python def f54(spans, gap): spans = list(spans) segments = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f54([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 055 ```python def f55(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + max(0, gap): segments.append([left, right]) else: segments[-1][1] = 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 ``` ``` f55([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 056 ```python def f56(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \n').upper()[:12] excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - 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 ``` ``` f56(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 057 ```python def f57(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:32]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f57(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 058 ```python def f58(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) 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 ``` ``` f58([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 059 ```python def f59(edges, start, limit): edges = list(edges) routes = {} for a, b in edges[:16]: routes.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < limit: for nxt in sorted(routes.get(cur, [])): 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[:3], 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f59([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 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 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 ``` ``` f60([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 061 ```python def f61(board): board = list(board) known = set() pieces = [] for row in range(len(board)): for col in range(len(board[0])): if col >= 12 or board[row][col] != '#' or (row, col) in known: continue cells = [] 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 ``` ``` f61(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 062 ```python def f62(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = max(0, segments[-1][1], right) breaks = [] for pos in range(1, len(segments)): left = segments[pos][0] breaks.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f62([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 063 ```python def f63(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f63(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 064 ```python def f64(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])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f64(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 065 ```python def f65(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap % 5: result.append([left, right]) else: result[-1][1] = 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 ``` ``` f65([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 066 ```python def f66(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap and count > 0: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f66([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 067 ```python def f67(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.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(routes.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) result = list(levels) edge = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': result, 'depth': levels, 'frontier': edge, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f67([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 068 ```python def f68(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 = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f68(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 069 ```python def f69(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in repeats: repeats.append(list(point)) found.add(point) far = max(far, 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 ``` ``` f69([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 070 ```python def f70(left, right, banned): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right 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 ``` ``` f70(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 071 ```python def f71(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f71([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 072 ```python def f72(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) < 2: continue frequency[word] = min(9, frequency.get(word, 0) + 1) ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f72(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 073 ```python def f73(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().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 if normalize(value) != ''} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f73(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 074 ```python def f74(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f74([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 075 ```python def f75(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} - 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 ``` ``` f75(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 076 ```python def f76(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, 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': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` ``` f76([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 077 ```python def f77(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * abs(prices[group]) selected.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f77([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 078 ```python def f78(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop 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 ``` ``` f78(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 079 ```python def f79(board): board = list(board) found = 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 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 ``` ``` f79(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 080 ```python def f80(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' ').upper()[:12] excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f80(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 081 ```python def f81(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): routes.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(routes.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f81([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 082 ```python def f82(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: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f82([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 083 ```python def f83(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.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(routes.get(cur, [])): if nxt not in distance and (not nxt.startswith('Z')): 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 ``` ``` f83([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 084 ```python def f84(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 ``` ``` f84([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 085 ```python def f85(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} if limit > 0 or limit < 0 else {} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < limit: for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) 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 ``` ``` f85([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 086 ```python def f86(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({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f86([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 087 ```python def f87(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f87([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 088 ```python def f88(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper()[:12] ignored = {normalize(value) for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f88(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 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 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f89([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 090 ```python def f90(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = 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 ``` ``` f90(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 091 ```python def f91(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] work = [(row, col)] visited.add((row, col)) while work: cur = work.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue work.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f91(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 092 ```python def f92(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 ``` ``` f92(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 093 ```python def f93(board): board = list(board) known = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f93(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 094 ```python def f94(rows, keep, prices): 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 state in keep and value != 0: cost = value * prices[group] selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f94([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 095 ```python def f95(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f95([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 096 ```python def f96(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: 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 ``` ``` f96(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 097 ```python def f97(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' ').upper()[:12] 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 ``` ``` f97(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 098 ```python def f98(rows, keep, ratemap): rows = list(rows) matches = [] 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] matches.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f98([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 099 ```python def f99(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * ratemap[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f99([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 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 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 = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f100([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 101 ```python def f101(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left[:20]: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f101([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 102 ```python def f102(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 ``` ``` f102([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 103 ```python def f103(edges, start, limit): edges = list(edges) routes = {} for a, b in edges[:16]: routes.setdefault(a, []).append(b) distance = {start: 0} if limit > 0 or limit < 0 else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: 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 ``` ``` f103([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 104 ```python def f104(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 work.append(nxt) sequence = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f104([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 105 ```python def f105(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 items = [] for key, count in left[:20]: if key in ratemap: items.append((key, count * ratemap[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f105([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 106 ```python def f106(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 = max(-64, min(64, x + dx)) y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.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': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f106([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 107 ```python def f107(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] < min(limit, 3): for nxt in sorted(nextmap.get(cur, []))[:20]: 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 ``` ``` f107([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 108 ```python def f108(rows, keep, prices): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * abs(prices[group]) matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f108([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 109 ```python def f109(spans, gap): spans = list(spans) segments = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not segments or left > segments[-1][1] + max(0, gap): segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) breaks = [] for pos in range(1, len(segments)): left = segments[pos][0] breaks.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in segments)))} output = dict() output.update(returnvalue) return output ``` ``` f109([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 110 ```python def f110(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:32]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': 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 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 ``` ``` f110(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 111 ```python def f111(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f111([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 112 ```python def f112(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 or len(word) < 2: continue tallies[word] = tallies.get(word, 0) + 1 sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in tallies.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f112(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 113 ```python def f113(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) 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 ``` ``` f113([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 114 ```python def f114(rows, keep, prices): rows = list(rows) matches = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep: cost = value * prices[group] matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost grand += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f114([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 115 ```python def f115(board): board = list(board) visited = set() regions = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f115(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 116 ```python def f116(spans, gap): spans = list(spans) segments = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = right spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f116([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 117 ```python def f117(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 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 ``` ``` f117(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 118 ```python def f118(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])) leftover = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f118([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 119 ```python def f119(board): board = list(board) visited = set() regions = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] 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 ``` ``` f119(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 120 ```python def f120(rows, keep, factors): rows = list(rows) matches = [] groupmap = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value != 0: cost = value * factors[group] matches.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost grand += cost matches.sort(key=lambda item: (-item[1], item[0])) 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 ``` ``` f120([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 121 ```python def f121(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: charge = value * abs(ratemap[group]) selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge grand += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f121([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 122 ```python def f122(rows, keep, ratemap): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * abs(ratemap[group]) selected.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f122([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 123 ```python def f123(rows, keep, prices): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: cost = value * abs(prices[group]) selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f123([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 124 ```python def f124(board): board = list(board) visited = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f124(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 125 ```python def f125(rows, keep, ratemap): rows = list(rows) selected = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: charge = value * ratemap[group] selected.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge sumvalue += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f125([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 126 ```python def f126(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) > 12: continue wordmap[word] = min(9, wordmap.get(word, 0) + 1) ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f126(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 127 ```python def f127(board): board = list(board) visited = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f127(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 128 ```python def f128(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f128([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 129 ```python def f129(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = right gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f129([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 130 ```python def f130(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 = max(-64, min(64, 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 ``` ``` f130([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 131 ```python def f131(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' ').upper()[:12] excluded = {str(value).strip() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f131(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 132 ```python def f132(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in 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 ``` ``` f132([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) 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 ``` ``` f133([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 134 ```python def f134(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \r').upper() excluded = {str(value).strip() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - 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 ``` ``` f134(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 135 ```python def f135(board): board = list(board) known = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if 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) 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 ``` ``` f135(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 136 ```python def f136(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \r').upper()[:12] excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - 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 ``` ``` f136(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 137 ```python def f137(left, right, ignored): left = list(left) def tidy(value): return str(value).strip(' ').upper() ignored = {tidy(value) for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b 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 ``` ``` f137(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 138 ```python def f138(rows, keep, factors): rows = list(rows) matches = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep: cost = value * factors[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(dict.fromkeys(matches)), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f138([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 139 ```python def f139(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \r').upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f139(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 140 ```python def f140(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 ``` ``` f140(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 141 ```python def f141(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in tallies.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f141(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 142 ```python def f142(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, 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 ``` ``` f142([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 143 ```python def f143(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] 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) 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 ``` ``` f143(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 144 ```python def f144(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 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 ``` ``` f144(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 145 ```python def f145(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (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 ``` ``` f145([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 146 ```python def f146(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' 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] = 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 ``` ``` f146(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 147 ```python def f147(board): board = list(board) found = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) if len(found) >= 100: continue queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f147(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 148 ```python def f148(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 = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f148(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 149 ```python def f149(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = max(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((min(100, right - left) for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f149([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 150 ```python def f150(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in factors)) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} output = dict() output.update(returnvalue) return output ``` ``` f150([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 151 ```python def f151(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): routes.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f151([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in steps: 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 ``` ``` f152([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 153 ```python def f153(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges: nextmap.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 (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 queue.append(nxt) result = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f153([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 154 ```python def f154(edges, start, limit): edges = list(edges) children = {} for a, b in edges[:16]: children.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] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f154([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 155 ```python def f155(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap and rate0 != 0: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in ratemap}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f155([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 156 ```python def f156(left, right, excluded): left = list(left) def canon(value): return ' '.join(str(value).split()).upper() excluded = {str(value).strip() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b 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 ``` ``` f156(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 157 ```python def f157(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(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 ``` ``` f157([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 158 ```python def f158(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' ').upper()[:12] excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f158(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 159 ```python def f159(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[: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 ``` ``` f159(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 160 ```python def f160(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = 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 ``` ``` f160([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 161 ```python def f161(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (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 ``` ``` f161([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 162 ```python def f162(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f162([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 163 ```python def f163(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) 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 ``` ``` f163(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 164 ```python def f164(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) > 12: continue wordmap[word] = wordmap.get(word, 0) + 1 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f164(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 165 ```python def f165(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' \n').upper()[:12] excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f165(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 166 ```python def f166(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: charge = value * abs(ratemap[group]) selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge sumvalue += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f166([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 167 ```python def f167(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f167([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 168 ```python def f168(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) 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 ``` ``` f168([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 169 ```python def f169(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f169([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 170 ```python def f170(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 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 ``` ``` f170(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 171 ```python def f171(rows, keep, factors): rows = list(rows) matches = [] totals = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: cost = value * factors[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': list(dict.fromkeys((code for code, cost in matches)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f171([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 172 ```python def f172(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(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((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 ``` ``` f172([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 173 ```python def f173(board): board = list(board) found = set() pieces = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] pending = [(row, col)] found.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) pending.append(nxt) 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 ``` ``` f173(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 174 ```python def f174(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) steps = {start: 0} if limit > 0 or limit < 0 else {} 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) 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 ``` ``` f174([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 175 ```python def f175(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = right spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f175([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 176 ```python def f176(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f176([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 177 ```python def f177(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])) 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 ``` ``` f177([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 178 ```python def f178(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) 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 ``` ``` f178([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 179 ```python def f179(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: 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 crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f179([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 180 ```python def f180(left, right, ignored): left = list(left) def canon(value): return ' '.join(str(value).split()).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 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(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 181 ```python def f181(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 ``` ``` f181([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 182 ```python def f182(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text[: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 tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f182(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 183 ```python def f183(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text[: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 tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f183(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 184 ```python def f184(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 ``` ``` f184(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 185 ```python def f185(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = right spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f185([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 186 ```python def f186(left, right, banned): left = list(left) def tidy(value): return ' '.join(str(value).split()).upper()[:12] banned = {tidy(value) for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f186(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 187 ```python def f187(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * prices[group] selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f187([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 188 ```python def f188(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, 64)): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f188([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 189 ```python def f189(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors: factors[name0] = rate0 matched = [] for key, count in left: if key in factors: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f189([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 190 ```python def f190(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} 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, []))[:20]: 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 ``` ``` f190([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 191 ```python def f191(rows, keep, prices): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: cost = value * abs(prices[group]) kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f191([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 192 ```python def f192(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f192([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 193 ```python def f193(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop or len(word) > 12: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f193(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 194 ```python def f194(left, right, ignored): left = list(left) def tidy(value): return str(value).strip().upper() ignored = {str(value).upper() for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f194(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 195 ```python def f195(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 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) 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 ``` ``` f195(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 196 ```python def f196(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] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f196([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 197 ```python def f197(left, right, excluded): left = list(left) def canon(value): return str(value).strip().upper()[:12] excluded = {str(value).strip() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f197(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 198 ```python def f198(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 = max(-64, min(64, x + dx)) y += dy used = min(64, used + 1) point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f198([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 199 ```python def f199(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f199(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 200 ```python def f200(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] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f200(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 201 ```python def f201(left, right, excluded): left = list(left) def tidy(value): return str(value).strip().upper() excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right if tidy(value) != ''} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f201(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 202 ```python def f202(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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f202(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 203 ```python def f203(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices or rate0 <= rate0 + 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f203([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 204 ```python def f204(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): nextmap.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) listing = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f204([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 205 ```python def f205(rows, keep, prices): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if str(state).lower() in {str(item).lower() for item in keep}: score = value * prices[group] matches.append((code, score)) totals[group] = totals.get(group, 0) + score amount += score matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, score in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f205([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 206 ```python def f206(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop or len(word) > 12: continue frequency[word] = min(9, frequency.get(word, 0) + 1) ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f206(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 207 ```python def f207(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 if normalize(value) != normalize(value) or normalize(value) != ''} - excluded b = {normalize(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f207(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 208 ```python def f208(rows, keep, ratemap): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * ratemap[group] kept.append((code, charge)) totals[group] = totals.get(group, 0) + charge amount += charge kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f208([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 209 ```python def f209(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 ``` ``` f209(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 210 ```python def f210(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 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 ``` ``` f210(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 211 ```python def f211(rows, keep, prices): rows = list(rows) kept = [] buckets = {} grand = 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 grand += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f211([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 212 ```python def f212(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f212(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 213 ```python def f213(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) 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(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in steps: steps[nxt] = steps[cur] + 1 work.append(nxt) listing = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f213([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = right gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f214([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 215 ```python def f215(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f215([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 216 ```python def f216(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' \r').upper() excluded = {str(value).upper() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f216(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 217 ```python def f217(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * prices[group] selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f217([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 218 ```python def f218(left, right, fee): left = list(left) factors = {} for name0, rate0 in right: if name0 not in factors or rate0 <= rate0 + 0: factors[name0] = rate0 matched = [] for key, count in left: if key in factors and count > 0: matched.append((key, count * factors[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f218([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 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(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 = 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 ``` ``` f219([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 220 ```python def f220(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f220(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 221 ```python def f221(spans, gap): spans = list(spans) segments = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = max(0, segments[-1][1], right) spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f221([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 222 ```python def f222(board): board = list(board) found = set() components = [] for row in range(len(board)): for col in range(min(len(board[row]), 10)): if col >= 12 or board[row][col] != '#' or (row, col) in found: continue cells = [] queue = [(row, col)] found.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) queue.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f222(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 223 ```python def f223(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' ').upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned b = {normalize(value) for value in right} - 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 ``` ``` f223(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 224 ```python def f224(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f224(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 225 ```python def f225(board): board = list(board) visited = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) pending.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f225(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 226 ```python def f226(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 or len(word) > 12: 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': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f226(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['ember']) ``` ``` {"counts":{"amber":1,"blue":5,"cyan":5,"delta":6,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"b":5,"c":5,"d":6,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","blue","cyan","garnet"]} ``` ### 227 ```python def f227(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f227([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 228 ```python def f228(left, right, excluded): left = list(left) def tidy(value): return str(value).strip().upper() excluded = {tidy(value) for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - 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 ``` ``` f228(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 229 ```python def f229(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 = 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 ``` ``` f229([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 230 ```python def f230(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = right spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f230([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 231 ```python def f231(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in wordmap.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f231(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 232 ```python def f232(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left: if key in prices: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) 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 ``` ``` f232([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 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)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = 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 ``` ``` f233([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 234 ```python def f234(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap or rate0 <= rate0 + 0: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap and count > 0: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f234([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 235 ```python def f235(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 = (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 ``` ``` f235([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 236 ```python def f236(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) 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 ``` ``` f236(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 237 ```python def f237(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, 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 ``` ``` f237([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 238 ```python def f238(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 ``` ``` f238(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 239 ```python def f239(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f239([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 240 ```python def f240(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) 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': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` ``` f240([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 241 ```python def f241(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 gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f241([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 242 ```python def f242(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors and rate0 != 0: factors[name0] = rate0 matched = [] for key, count in left[: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(matched)} output = dict() output.update(returnvalue) return output ``` ``` f242([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 243 ```python def f243(lines, stop): lines = list(lines) frequency = {} 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 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 ``` ``` f243(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 244 ```python def f244(rows, keep, factors): rows = list(rows) matches = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * abs(factors[group]) matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f244([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 245 ```python def f245(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices: prices[name0] = rate0 items = [] for key, count in left: if key in prices and count > 0: items.append((key, count * prices[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} output = dict() output.update(returnvalue) return output ``` ``` f245([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 246 ```python def f246(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` ``` f246(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 247 ```python def f247(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 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 ``` ``` f247(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 248 ```python def f248(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): nextmap.setdefault(a, []).append(b) distance = {start: 0} 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: 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[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f248([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 249 ```python def f249(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f249(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 250 ```python def f250(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 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 amount += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` ``` f250([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 251 ```python def f251(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 ``` ``` f251(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 252 ```python def f252(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 ``` ``` f252([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 253 ```python def f253(rows, keep, prices): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * prices[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 ``` ``` f253([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 254 ```python def f254(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 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 += 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 ``` ``` f254([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 255 ```python def f255(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f255([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 256 ```python def f256(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f256([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 257 ```python def f257(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:32]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) 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 ``` ``` f257(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 258 ```python def f258(rows, keep, prices): rows = list(rows) selected = [] totals = {} sumvalue = 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 sumvalue += cost 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, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f258([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 259 ```python def f259(rows, keep, factors): rows = list(rows) matches = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * factors[group] matches.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f259([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 260 ```python def f260(board): board = list(board) found = set() regions = [] for row in range(len(board)): for col in range(len(board[0])): if board[row][col] != '#' or (row, col) in found: continue cells = [] pending = [(row, col)] found.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): found.add(nxt) pending.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f260(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 261 ```python def f261(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): nextmap.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < min(limit, 3): for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f261([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 262 ```python def f262(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 ``` ``` f262([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 263 ```python def f263(rows, keep, prices): rows = list(rows) matches = [] groupmap = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * abs(prices[group]) matches.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score amount += score matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, score in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f263([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 264 ```python def f264(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f264([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 265 ```python def f265(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 ``` ``` f265(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 266 ```python def f266(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) result = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f266([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 267 ```python def f267(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 = (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 ``` ``` f267([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 268 ```python def f268(rows, keep, prices): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * abs(prices[group]) kept.append((code, charge)) totals[group] = totals.get(group, 0) + charge amount += charge kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f268([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 269 ```python def f269(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] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in levels and (not nxt.startswith('Z')): levels[nxt] = levels[cur] + 1 queue.append(nxt) listing = list(levels) lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f269([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 270 ```python def f270(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = right gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f270([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 271 ```python def f271(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(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f271([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 272 ```python def f272(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (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 ``` ``` f272([('S', 1), ('E', 1), ('E', 6), ('S', 3), ('W', 1), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-2,-12,7,0],"end":[-2,-11],"far":14,"revisits":[[-1,-8],[0,-8],[1,-8],[-2,-11]],"steps":35} ``` ### 273 ```python def f273(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, 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': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f273([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 274 ```python def f274(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * ratemap[group] kept.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost kept.sort(key=lambda item: (-item[1], 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 ``` ``` f274([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 275 ```python def f275(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) 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 ``` ``` f275([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 276 ```python def f276(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 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 ``` ``` f276([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 277 ```python def f277(board): board = list(board) known = set() regions = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in known: continue cells = [] pending = [(row, col)] known.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) pending.append(nxt) regions.append((len(cells), min(cells))) regions.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} output = dict() output.update(returnvalue) return output ``` ``` f277(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 278 ```python def f278(left, right, excluded): left = list(left) def normalize(value): return str(value).strip().upper() excluded = {normalize(value) for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f278(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 279 ```python def f279(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < 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) 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 ``` ``` f279([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 280 ```python def f280(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right[:20]: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left: if key in ratemap: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in ratemap}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f280([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 281 ```python def f281(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 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 ``` ``` f281(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 282 ```python def f282(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) spaces = [] for pos in range(1, len(result)): left = result[pos][0] spaces.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` ``` f282([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 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, 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, 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 ``` ``` f283([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 284 ```python def f284(rows, keep, factors): rows = list(rows) selected = [] groupmap = {} grand = 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 grand += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f284([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 285 ```python def f285(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] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) sequence = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f285([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 286 ```python def f286(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 = [] 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 ``` ``` f286(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 287 ```python def f287(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} if any((start in edge for edge in edges)) else {} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) listing = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f287([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 288 ```python def f288(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in wordmap.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` ``` f288(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 289 ```python def f289(left, right, fee): left = list(left) factors = {} for name0, rate0 in right[:20]: if name0 not in factors: factors[name0] = rate0 combined = [] for key, count in left: if key in factors: combined.append((key, count * factors[key] + fee)) combined.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len({key for key, value in combined})} output = dict() output.update(returnvalue) return output ``` ``` f289([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 290 ```python def f290(left, right, fee): left = list(left) prices = {} for name0, rate0 in right: if name0 not in prices and rate0 != 0: prices[name0] = rate0 matched = [] for key, count in left[:20]: if key in prices: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) leftover = sorted({key for key, count in left if key not in prices}) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f290([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 291 ```python def f291(rows, keep, prices): rows = list(rows) matches = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: cost = value * abs(prices[group]) matches.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f291([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 292 ```python def f292(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) breaks = [] for pos in range(1, min(50, len(combined))): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` ``` f292([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 293 ```python def f293(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` ``` f293([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 294 ```python def f294(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) pending.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f294(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 295 ```python def f295(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])) 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 ``` ``` f295([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 296 ```python def f296(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` ``` f296(['Blue, blue. ember blue', 'Cyan, frost. ember frost delta', 'Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost'], ['blue']) ``` ``` {"counts":{"amber":1,"cyan":5,"delta":6,"ember":4,"frost":6,"garnet":3,"helium":1},"initials":{"a":1,"c":5,"d":6,"e":4,"f":6,"g":3,"h":1},"size":129,"top":["delta","frost","cyan","ember","garnet"]} ``` ### 297 ```python def f297(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` ``` f297([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 298 ```python def f298(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' ').upper() excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b 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 ``` ``` f298(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], [' Amber']) ``` ``` {"all":["BLUE","CYAN","DELTA","EMBER","FROST","GARNET","HELIUM"],"both":["BLUE","CYAN","EMBER"],"left":["DELTA","GARNET"],"right":["FROST","HELIUM"],"score":13} ``` ### 299 ```python def f299(left, right, fee): left = list(left) prices = {} for name0, rate0 in right[:20]: if name0 not in prices: prices[name0] = rate0 matched = [] for key, count in left: if key in prices and count > 0: matched.append((key, count * prices[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) absent = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f299([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 300 ```python def f300(rows, keep, ratemap): rows = list(rows) kept = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value != 0: score = value * ratemap[group] kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score sumvalue += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': buckets, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f300([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":517,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":729} ``` ### 301 ```python def f301(board): board = list(board) visited = set() components = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(len(board[row])): if board[row][col] != '#' or (row, col) in visited: continue cells = [] pending = [(row, col)] visited.add((row, col)) while pending: cur = pending.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue pending.append(nxt) components.append((len(cells), min(cells))) components.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} output = dict() output.update(returnvalue) return output ``` ``` f301(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 302 ```python def f302(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 ``` ``` f302(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 303 ```python def f303(left, right, fee): left = list(left) ratemap = {} for name0, rate0 in right: if name0 not in ratemap: ratemap[name0] = rate0 matched = [] for key, count in left[:20]: if key in ratemap and count > 0: matched.append((key, count * ratemap[key] + fee)) matched.sort(key=lambda item: (-item[1], item[0])) unmatched = sorted((key for key, count in left if key not in ratemap)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} output = dict() output.update(returnvalue) return output ``` ``` f303([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 304 ```python def f304(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper() ignored = {normalize(value) for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f304(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 305 ```python def f305(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: score = value * abs(ratemap[group]) selected.append((code, score)) totals[group] = totals.get(group, 0) + score sumvalue += score selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` ``` f305([('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4), ('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) ``` ``` {"accepted":8,"groups":{"core":564,"edge":72,"north":140},"top":["BR19","AN34","DL67"],"total":776} ``` ### 306 ```python def f306(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((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 ``` ``` f306([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 307 ```python def f307(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap % 5: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` ``` f307([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 0) ``` ``` {"cover":41,"holes":[1,4,4,1,3,2,5],"longest":10,"segments":[[3,5],[6,11],[15,25],[29,33],[34,41],[44,47],[49,55],[60,64]]} ``` ### 308 ```python def f308(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap: segments.append([left, right]) else: segments[-1][1] = right spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(min(100, left - segments[pos - 1][1])) returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` ``` f308([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 309 ```python def f309(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])) unmatched = sorted((key for key, count in left if key not in prices)) returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} output = dict() output.update(returnvalue) return output ``` ``` f309([('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",36],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":168} ``` ### 310 ```python def f310(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: if key in factors: items.append((key, count * factors[key] + fee)) items.sort(key=lambda item: (-item[1], item[0])) absent = sorted({key for key, count in left if key not in factors}) returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} output = dict() output.update(returnvalue) return output ``` ``` f310([('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8), ('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2)], [('DL', 6), ('EV', 10), ('FP', 4), ('GQ', 4), ('HR', 6)], 0) ``` ``` {"count":5,"items":[["DL",48],["EV",40],["FP",32],["GQ",32],["HR",12]],"missing":["AN","BR","CX"],"total":164} ``` ### 311 ```python def f311(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 sorted(nextmap.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) sequence = list(steps) lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` ``` f311([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], '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"]} ``` ### 312 ```python def f312(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges: nextmap.setdefault(a, []).append(b) levels = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(nextmap.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` ``` f312([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### 313 ```python def f313(board): board = list(board) known = set() pieces = [] for row in range(min(len(board), 10 + (len(board) - len(board)))): for col in range(min(len(board[row]), 10)): if board[row][col] != '#' or (row, col) in known: continue cells = [] queue = [(row, col)] known.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): known.add(nxt) if len(known) >= 100: continue queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f313(['..#.##', '.#....', '#.#..#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[2,2],[0,4],[0,2],[1,1],[2,0]],"count":7,"filled":18,"sizes":[6,4,3,2,1,1,1]} ``` ### 314 ```python def f314(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used = 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 = (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 ``` ``` f314([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 315 ```python def f315(board): board = list(board) visited = set() pieces = [] for row in range(len(board)): for col in range(len(board[row])): if col >= 12 or board[row][col] != '#' or (row, col) in visited: continue cells = [] queue = [(row, col)] visited.add((row, col)) while queue: cur = queue.pop() cells.append(cur) for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): nxt = (cur[0] + dr, cur[1] + dc) if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): visited.add(nxt) if len(visited) >= 100: continue queue.append(nxt) pieces.append((len(cells), min(cells))) pieces.sort(key=lambda item: (-item[0], item[1])) returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} output = dict() output.update(returnvalue) return output ``` ``` f315(['.##.##', '..#...', '#....#', '..##.#', '##...#', '####.#']) ``` ``` {"anchors":[[4,0],[2,5],[0,1],[0,4],[3,2],[2,0]],"count":6,"filled":18,"sizes":[6,4,3,2,2,1]} ``` ### 316 ```python def f316(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' \r').upper()[:12] excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b 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 ``` ``` f316(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 317 ```python def f317(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) spaces = [] for pos in range(1, len(segments)): left = segments[pos][0] spaces.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))} output = dict() output.update(returnvalue) return output ``` ``` f317([(3, 5), (6, 11), (15, 22), (19, 25), (29, 33), (34, 41), (44, 47), (49, 55), (60, 64)], 3) ``` ``` {"cover":48,"holes":[4,4,5],"longest":26,"segments":[[3,11],[15,25],[29,55],[60,64]]} ``` ### 318 ```python def f318(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, 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 ``` ``` f318([('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2), ('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1)]) ``` ``` {"bounds":[-7,-12,4,0],"end":[-7,-11],"far":19,"revisits":[[-6,-8],[-5,-8],[-4,-8],[-7,-11]],"steps":34} ``` ### 319 ```python def f319(left, right, ignored): left = list(left) def canon(value): return str(value).strip().upper()[:12] ignored = {canon(value) for value in ignored} a = {canon(value) for value in left} - 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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` ``` f319(['cyan ', ' Amber', 'GARNET', 'Ember', 'delta', 'BLUE', 'cyan '], ['cyan ', 'Ember', 'helium', 'frost', ' Amber', 'BLUE', 'cyan '], ['GARNET']) ``` ``` {"all":["AMBER","BLUE","CYAN","DELTA","EMBER","FROST","HELIUM"],"both":["AMBER","BLUE","CYAN","EMBER"],"left":["DELTA"],"right":["FROST","HELIUM"],"score":18} ``` ### 320 ```python def f320(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` ``` f320([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) ``` ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} ``` ### Final Query Give me the output of the final query without executing it! ```python import json, re from collections import deque out = {} moves = list((('S', 1), ('E', 1), ('E', 3), ('S', 3), ('W', 3), ('W', 3), ('W', 2)) + (('S', 1), ('S', 3), ('W', 3), ('E', 3), ('S', 2), ('W', 3), ('S', 2), ('N', 1))) 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} permit = record['bb'] edges = list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E'))) start = 'A' limit = 3 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)} signal = record['aa'][5] rows = list((('CX97', 'edge', 'void', 10), ('AN34', 'core', 'ready', 17), ('DL71', 'north', 'checked', 3), ('BR08', 'core', 'void', 12), ('DL45', 'core', 'void', 7), ('DL82', 'north', 'ready', 3), ('BR19', 'core', 'checked', 19), ('DL56', 'edge', 'hold', 4)) + (('BR93', 'edge', 'ready', 24), ('AN30', 'north', 'ready', 4), ('DL67', 'core', 'ready', 11), ('AN04', 'south', 'void', 6), ('AN41', 'north', 'ready', 10), ('AN78', 'south', 'hold', 3))) keep = list(('ready',) + ('checked',)) route = permit cases = { 19: dict((('north', 7), ('south', 5), ('core', 12)) + (('edge', 3),)), 14: dict((('north', 7), ('south', 9), ('core', 11)) + (('edge', 3),)), } 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} token = record['bb'] right = list((('DL', 6), ('EV', 10)) + (('FP', 4), ('GQ', 4), ('HR', 6))) fee = 0 route = signal cases = { 'G': list((('AN', 7), ('BR', 3), ('CX', 8), ('DL', 8)) + (('EV', 4), ('FP', 8), ('GQ', 8), ('HR', 2))), 'F': list((('AN', 3), ('BR', 3), ('CX', 8), ('DL', 8)) + (('EV', 4), ('FP', 9), ('GQ', 8), ('HR', 2))), } 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)} memo = record['bb'] route = (permit, signal) cases = { (19, 'F'): list(('..#.##', '.#....') + ('#.#..#', '..##.#', '##...#', '####.#')), (19, 'G'): list(('.##.##', '..#...') + ('#....#', '..##.#', '##...#', '####.#')), (14, 'F'): list(('.##.##', '..#...') + ('#....#', '..##.#', '##...#', '####.#')), (14, 'G'): list(('.##.##', '..#...') + ('#....#', '..##.#', '##...#', '####.#')), } board = cases[route] cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#'] parent = {cell: cell for cell in cells} def root(cell): while parent[cell] != cell: parent[cell] = parent[parent[cell]] cell = parent[cell] return cell def merge(a, b): a = root(a) b = root(b) if a != b: parent[b] = a for row, col in cells: for other in ((row - 1, col), (row, col - 1)): if other in parent: merge((row, col), other) groups = {} for cell in cells: groups.setdefault(root(cell), []).append(cell) parts = [(len(group), min(group)) for group in groups.values()] parts.sort(key=lambda item: (-item[0], item[1])) record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)} clue = record['dd'] left = list(('cyan ', ' Amber', 'GARNET', 'Ember') + ('delta', 'BLUE', 'cyan ')) right = list(('cyan ', 'Ember', 'helium', 'frost') + (' Amber', 'BLUE', 'cyan ')) route = (token, clue) cases = { (729, 6): ['GARNET'], (729, 7): ['GARNET'], (776, 6): ['GARNET'], (776, 7): [' Amber'], } blocked = 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))} stamp = record['ee'] spans = list(((3, 5), (6, 11), (15, 22), (19, 25)) + ((29, 33), (34, 41), (44, 47), (49, 55), (60, 64))) route = (memo, stamp) cases = { (168, 13): 0, (168, 18): 3, (164, 13): 3, (164, 18): 3, } gap = cases[route] spans.sort(key=lambda pair: (pair[0], pair[1])) parts = [] pos = 0 while pos < len(spans): left, right = spans[pos] pos += 1 while pos < len(spans) and spans[pos][0] <= right + gap: right = max(right, spans[pos][1]) pos += 1 parts.append([left, right]) holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} ticket = record['cc'] lines = list(('Blue, blue. ember blue', 'Cyan, frost. ember frost delta') + ('Helium, delta. delta frost', 'Ember, cyan. garnet amber ember', 'Cyan, delta. frost garnet', 'Blue, delta. garnet blue cyan', 'Delta, cyan. frost frost')) route = ticket cases = { 48: ['ember'], 41: ['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))} out['aa'] = record['aa'] out['bb'] = record['bb'] out['cc'] = record['cc'] out['dd'] = record['dd'] print(json.dumps(out, separators=(",", ":"), sort_keys=True)) ```