def flip_tiles(lines): if isinstance(lines, str): lines = to_lines(lines) tiles = [re.findall(DIRECTION_REGEX, line) for line in lines] black_tiles = set() for tile in tiles: pos = Tile(0, 0) for move in tile: if move == "e": pos = pos.e() elif move == "se": pos = pos.se() elif move == "sw": pos = pos.sw() elif move == "w": pos = pos.w() elif move == "nw": pos = pos.nw() elif move == "ne": pos = pos.ne() if pos in black_tiles: black_tiles.remove(pos) else: black_tiles.add(pos) return black_tiles
def parse(lines): if isinstance(lines, str): lines = to_lines(lines) tiles = group_lines(lines) tiles = [Tile(tile_lines) for tile_lines in tiles] tiles = {tile.num: tile for tile in tiles} return tiles
def parse(lines): if isinstance(lines, str): lines = to_lines(lines) lines = group_lines(lines) decks = [player[1:] for player in lines] decks = [[int(card) for card in deck] for deck in decks] return decks
def part_1(lines): if isinstance(lines, str): lines = to_lines(lines) earliest_departure_time = int(lines[0]) bus_ids = [int(bus_id) for bus_id in lines[1].split(",") if bus_id != "x"] next_departures = [(n, ((earliest_departure_time + n - 1) // n) * n) for n in bus_ids] earliest_bus = sorted(next_departures, key=itemgetter(1))[0] wait_time = earliest_bus[1] - earliest_departure_time return earliest_bus[0] * wait_time
def parse(lines): if isinstance(lines, str): lines = to_lines(lines) (rules, your_ticket, nearby_tickets) = group_lines(lines) rules = [parse_rule(line) for line in rules] your_ticket = parse_ticket(your_ticket[1]) nearby_tickets = [parse_ticket(line) for line in nearby_tickets[1:]] return rules, your_ticket, nearby_tickets
def part_2(program): if isinstance(program, str): program = to_lines(program) mask = None memory = {} for line in program: if m := re.match(MASK_REGEX, line): mask = m.group(1) elif m := re.match(MEM_REGEX, line): address = int(m.group(1)) value = int(m.group(2)) result = apply_bitmask(address, mask) for a in actual_addresses(result): memory[a] = value
def part_1(program): if isinstance(program, str): program = to_lines(program) mask_and = mask_or = None memory = {} for line in program: if m := re.match(MASK_REGEX, line): mask = m.group(1) mask_and = int(mask.replace("0", "0").replace("1", "0").replace("X", "1"), 2) mask_or = int(mask.replace("X", "0"), 2) elif m := re.match(MEM_REGEX, line): address = int(m.group(1)) value = int(m.group(2)) result = (value & mask_and) | mask_or memory[address] = result
def part_2(lines): if isinstance(lines, str): lines = to_lines(lines) active_cubes = set() for y in range(len(lines)): for x in range(len(lines[0])): coords = (x, y, 0, 0) if lines[y][x] == ACTIVE: active_cubes.add(coords) for cycle in range(1, 7): (min_x, max_x) = min_max(active_cubes, 0) (min_y, max_y) = min_max(active_cubes, 1) (min_z, max_z) = min_max(active_cubes, 2) (min_w, max_w) = min_max(active_cubes, 3) new_active_cubes = set() for w in range(min_w - 1, max_w + 2): for z in range(min_z - 1, max_z + 2): for y in range(min_y - 1, max_y + 2): for x in range(min_x - 1, max_x + 2): pos = (x, y, z, w) old_state = ACTIVE if (pos in active_cubes) else INACTIVE neighbours = count_neighbours_part_2( active_cubes, x, y, z, w) new_state = determine_new_state(old_state, neighbours) if new_state == ACTIVE: new_active_cubes.add(pos) active_cubes = new_active_cubes return len(active_cubes)
def test_with_part_1_example(self): bus_ids = to_lines(PART_1_EXAMPLE)[1] self.assertEqual(part_2(bus_ids), 1068781)
def parse(lines): if isinstance(lines, str): lines = to_lines(lines) return [parse_food(line) for line in lines]
def part_1_single(rules, message): rules = to_lines(rules) rules = process_rules(rules) regex = rules[0] return re.fullmatch(regex, message) is not None
def parse_rules_and_messages(lines): if isinstance(lines, str): lines = to_lines(lines) return group_lines(lines)