def get_bag_colors(file, query): lines = pnlp.read_lines(file) dct = {} for line in lines: tmp = line.split(" bags contain ") val = tmp[0] if not val: continue keys = tmp[1].split(" bag") for key in keys: key = " ".join(key.split()[-2:]) if " " not in key: continue if key in dct: dct[key].append(val) else: dct[key] = [val] res = set() stack = [query] while stack: q = stack.pop(0) for adj in dct.get(q, []): stack.append(adj) res.add(adj) return len(res)
def get_dist(file): lines = pnlp.read_lines(file) x, y = 0, 0 direct = "E" for line in lines: nvi = line[0] num = int(line[1:]) if nvi == "R": direct = get_direct(DIRECTS, direct, num, "R") elif nvi == "L": direct = get_direct(DIRECTS, direct, num, "L") else: if nvi == "F": tmp = direct else: tmp = nvi if tmp == "N": y += num elif tmp == "S": y -= num elif tmp == "W": x -= num elif tmp == "E": x += num return abs(x) + abs(y)
def get_pron_rawdict(fp: str): lines = pnlp.read_lines(fp) dct = {} for line in lines: if line.startswith("#"): continue unic, lang, pron = line.strip().split("\t") if unic not in dct: dct[unic] = {} dct[unic][lang] = pron return dct
def get_char_and_comp_rawdict(fp: str): lines = pnlp.read_lines(fp) char_dct = {} comp_dct = {} for line in lines: if line.startswith("#"): continue item = line.strip().split("\t") unic = item[0] char = item[1] comp = item[2:] if unic not in char_dct: char_dct[unic] = {} if char not in comp_dct: comp_dct[char] = {} char_dct[unic] = char comp_dct[char] = comp return char_dct, comp_dct
def get_tree_num(file, right: int, down: int): data = [] row, col = 0, 0 lines = pnlp.read_lines(file) for line in lines: line = line.strip() data.append(list(line)) rows = len(data) cols = len(data[0]) res = [] num = 0 while row < rows - down: col += right row += down val = data[row][col%cols] if val == "#": num += 1 res.append(val) return num
import pnlp import copy file = "day8Test.txt" file = "day8.txt" lines = pnlp.read_lines(file) lst = [1, 2, 3, 4, 5] def inf_loop(lst: list, start: int, n: int): length = len(lst) assert start < length if n < 0: length - n % length reminder = n % length idx = (start + reminder) % length return idx assert inf_loop(list(range(8)), 2, 4) == 6 assert inf_loop(lst, 0, 10) == 0 assert inf_loop(lst, 1, 10) == 1 assert inf_loop(lst, 1, 11) == 2 assert inf_loop(lst, 4, 11) == 0 assert inf_loop(lst, 4, -3) == 1 assert inf_loop(lst, 3, -13) == 0 def read_line(line):
import pnlp file = "day1.txt" nums = list(map(int, pnlp.read_lines(file))) year = 2020 def add_two_is(year: int, nums: list) -> int: lst = [] for i in nums: if year - i in nums: lst.append((i, year - i)) res = [] for pair in lst: res.append(pair[0] * pair[1]) return (max(res)) def add_three_is(year: int, nums: list) -> int: lst = set() for i in nums: need = year - i for j in nums: if need - j in nums: pair = sorted((i, j, need - j)) lst.add(tuple(pair))
row = e s, e = 0, 7 for t in string[-3:]: s, e = bs(s, e, t) col = e return row * 8 + col for s in ["FBFBBFFRLR", "BFFFBBFRRR", "FFFBBBFRRR", "BBFFBBFRLL"]: res = get_seatid(s) print(res) lines = pnlp.read_lines("day5.txt") ids = set() for line in lines: id = get_seatid(line) ids.add(id) print() print(max(ids)) mx = max(ids) mn = min(ids) for i in range(mn+1, mx): if i not in ids: print(i) break