Exemple #1
0
    def __str__(self):
        return "\n".join(["".join(row) for row in self.seats[-1]])

    def count(self, seat_type):
        how_many = 0
        for row in self.seats[-1]:
            for seat in row:
                if seat == seat_type:
                    how_many += 1
        return how_many


if __name__ == "__main__":
    lines = test_input.split("\n")
    lines = ioaoc.read_file("day11_input.txt")

    board = Board(lines)

    while True:
        board.next_iteration(Board.adjacent, occupied_threshold=4)
        if board.seats[-1] == board.seats[-2]:
            break

    print(">", board.count(Board.OCCUPIED))

    board = Board(lines)

    while True:
        board.next_iteration(Board.visible, occupied_threshold=5)
        if board.seats[-1] == board.seats[-2]:
Exemple #2
0
        names = [name for name, _ in all_bags[next_to_check]]
        to_visit.update(names)

    return False


def bag_count(bag, all_bags):
    nested_bag_count = 0
    for name, count in all_bags[bag]:
        nested_bag_count += count + count * bag_count(name, all_bags)
    return nested_bag_count


NEEDLE = "shiny gold"

if __name__ == "__main__":
    lines = test_input.split("\n")
    all_bags = parse(ioaoc.read_file("day07_input.txt"))

    has_needle = set([NEEDLE])
    visited = set()

    for bag, nested in all_bags.items():
        to_visit = set([name for name, _ in nested])
        if traverse(NEEDLE, to_visit, all_bags, visited):
            has_needle.add(bag)
        visited.add(bag)

    print(">", len(has_needle) - 1)
    print(">>", bag_count(NEEDLE, all_bags))