from lib import readinput my_input = readinput(__file__) from collections import defaultdict, namedtuple Node = namedtuple('Node', ['name', 'weight', 'subtowers']) def parse_program(program_string): name, weight_repr, *relation = program_string.split() weight = int(weight_repr[1:-1]) subtowers = tuple(''.join(relation[1:]).split(',')) if relation else () return Node(name, weight, subtowers) def bottom_program(programs): holders = set(program.name for program in programs if program.subtowers) subtower_bottoms = set( subtower_name for program in programs for subtower_name in program.subtowers ) rott_bottoms = holders - subtower_bottoms return next(iter(rott_bottoms)) day_7_1 = bottom_program def day_7_2(programs): programs_by_name = dict((program.name, program) for program in programs) def tower_weight(program): return program.weight + sum(
from lib import readinput my_input = readinput(__file__, strip=False) VERTICAL_PATH = '|' HORIZONTAL_PATH = '-' INTERSECTION = '+' VOID = ' ' def packet_steps(diagram): y = 0 # We start at the top of the diagram x = diagram[y].index(VERTICAL_PATH) dx, dy = 0, 1 width, height = len(diagram[0]), len(diagram) def at(x, y): in_bound = 0 <= x < width and 0 <= y < height return diagram[y][x] if in_bound else VOID def turn(x, y, dx, dy): if dx: new_dy = 1 if at(x, y + 1) != VOID else -1 return (0, new_dy) else: new_dx = 1 if at(x + 1, y) != VOID else -1 return (new_dx, 0) while at(x, y) != VOID: yield (x, y) if at(x, y) == INTERSECTION: dx, dy = turn(x, y, dx, dy)