Example #1
0
def main():
    input_data = load_string()
    passports = get_passports(input_data)
    print(f"Number of passports: {len(passports)}")
    ignorable_fields = ["cid"]
    count = count_valid_passports(passports, ignorable_fields)
    return count
Example #2
0
def part1():
    maximum_seat_id = 0

    for boarding_pass in load_string():
        seat_id = compute_seat_id(boarding_pass)
        maximum_seat_id = max(seat_id, maximum_seat_id)

    return maximum_seat_id
Example #3
0
def part2():

    seats = set()

    for boarding_pass in load_string():
        seat_id = compute_seat_id(boarding_pass)
        seats.add(seat_id)

    all_seats = set(range(min(seats), max(seats) + 1))

    my_seat = all_seats - seats
    if len(my_seat) != 1:
        raise ValueError(f"Your butt does not need {len(my_seat)} seats!")

    my_seat = my_seat.pop()

    return my_seat
Example #4
0
            continue
        new_program = program.copy()
        if instruction.startswith("jmp"):
            new_program[index] = new_program[index].replace("jmp", "nop")
        elif instruction.startswith("nop"):
            new_program[index] = new_program[index].replace("nop", "jmp")
        else:
            raise RuntimeError(f"Invalid instruction: {instruction}")
        try:
            accumulator_value = run(new_program)
            print(
                f"Edited instruction at index {index}: "
                f"{program[index]} --> {new_program[index]}"
            )
            return accumulator_value
        except RuntimeError:
            continue
    return None


if __name__ == "__main__":
    file_contents = load_string()
    try:
        accumulator_value_2 = run(file_contents)
        raise Exception("No loop found")
    except InfiniteLoopException as exc:
        print(f"Part 1: {exc.accumulator}")

    accumulator_value = fix_program(file_contents)
    print(f"Part 2: {accumulator_value}")
def main():
    tree_pattern = load_string()
    print(f'Part 1 result: {count_trees(tree_pattern, right=3, down=1)}')
    print(f'Part 2 result: {multiply_all_the_trees(tree_pattern)}')
    # now we have all the paths from bag_name,
    # we need to traverse the graph following these paths to get the number of child bags
    count = []
    for path in all_paths_from_bag:
        path_bag_count = []
        for bag in range(1, len(path)):
            inner_bag = [
                elem for elem in organised_rules[path[bag - 1]]
                if path[bag] in elem
            ]
            path_bag_count.append(inner_bag[0][path[bag]])

        path_sum = 0
        for i in range(1, len(path_bag_count)):
            path_sum += path_bag_count[
                i - 1] + path_bag_count[i - 1] * path_bag_count[i]

        count.append(path_sum)

    return np.sum(count)


if __name__ == "__main__":
    rules = loaders.load_string()
    # count = find_count_possible_outermost_bags(rules, "shiny gold")
    #
    # print(count)
    # 372
    # part 2 = how many bags can a given bag contain?
    count2 = get_max_num_of_bags_inside(rules, "shiny gold")
    print(count2)
Example #7
0
def main(input=None):
    if input is None:
        input = loaders.load_string()
    print(f"Part 1 solution: {part1(input)}")
    print(f"Part 2 solution: {part2(input)}")
def main():
    input_data = load_string()
    something = parse_passport(input_data)
    pass