Exemplo n.º 1
0
def solve1(condition=valid1):
    valid_passwords = 0

    with get_input_lines_stream(FILE_NUM) as f:
        for line in f:
            valid_passwords += call_parsed(line, condition)

    return valid_passwords
Exemplo n.º 2
0
def solve1(valid_func=valid1):
    current_passport = {}
    valid_pass_count = 0

    for line in get_input_lines_stream(FILE_NUM).read().split('\n\n'):
        segments = line.split()

        for key, value in map(lambda segment: segment.split(':'), segments):
            current_passport[key] = value

        valid_pass_count += valid_func(current_passport)
        current_passport = {}

    return valid_pass_count
Exemplo n.º 3
0
def solve1(is_sample=False):
    lines = get_input_lines_stream(FILE_NUM,
                                   SAMPLES_FOLDER if is_sample else None,
                                   HOME_DIR).read()
    formatted_lines = lines.split('\n\n')
    values = formatted_lines[0].split(',')
    boards = get_boards(formatted_lines)

    for value in values:
        for board in boards:
            if activate_board_value(value, board) and is_winner(board):
                unmarked_sum = sum(get_unmarked(board))

                return unmarked_sum * int(value)
Exemplo n.º 4
0
def solve2(is_sample=False):
    lines = get_input_lines_stream(FILE_NUM,
                                   SAMPLES_FOLDER if is_sample else None,
                                   HOME_DIR).read()
    formatted_lines = lines.split('\n\n')
    values = formatted_lines[0].split(',')
    boards = get_boards(formatted_lines)

    for value in values:
        for board_index in range(0, len(boards)):
            board = boards[board_index]

            if not board:
                continue

            if activate_board_value(value, board) and is_winner(board):
                boards[board_index] = None

                if all(board is None for board in boards):
                    unmarked_sum = sum(get_unmarked(board))

                    return unmarked_sum * int(value)
Exemplo n.º 5
0
def get_rules() -> Dict[str, list]:
    rules = {}

    with get_input_lines_stream(FILE_NUM) as file:
        for line in file:
            parent_bag, child_parts = line.split(' bags contain ')
            children = child_parts.split(', ')

            if parent_bag not in rules:
                rules[parent_bag] = []

            for child in children:
                if child == LOOP_ABORT:
                    continue

                quantity_delimiter = child.index(' ')
                name_delimiter = child.rindex(' ')
                quantity = int(child[:quantity_delimiter])
                child_bag = child[quantity_delimiter + 1:name_delimiter]

                rules[parent_bag].append([child_bag, quantity])

    return rules
Exemplo n.º 6
0
def get_sorted_abstract_instructions() -> List[int]:
    return sorted(
        map(
            lambda bin_num: int(bin_num, 2),
            get_input_lines_stream(FILE_NUM).read().replace('F', '0').replace(
                'B', '1').replace('L', '0').replace('R', '1').split()))