Пример #1
0
def part_one(input_file: str) -> int:
    # read input file
    inputs = read_input_blob(input_file)

    # split groups and combine all group answers into a single line
    groups = []
    for line in inputs.split('\n\n'):
        groups.append(line.replace('\n', ''))

    # iterate through groups and count uniques
    total_answer_counts = 0
    for group in groups:
        total_answer_counts += len(set(group))

    return total_answer_counts
Пример #2
0
def part_two(input_file: str) -> int:
    # read input file as blob
    input_str = read_input_blob(input_file)

    # split by mask and relevant mask instr, parse into masked instruction set objects
    mask_split = input_str.split('mask = ')[1:]
    masked_instr_sets = [
        MaskedInstructionSet.from_input_str(input_str)
        for input_str in mask_split
    ]

    # instantiate computer system and run instruction sets
    computer = ComputerSystem()
    computer.run_address_masked_instruction_set(
        instruction_sets=masked_instr_sets)

    return computer.get_sum_of_mem_vals()
Пример #3
0
def part_one(input_file: str, required_fields: Set[str]) -> int:
    # read input file
    inputs = read_input_blob(input_file)

    # parse raw input and marshal into Passport object
    passports = []
    for line in inputs.split('\n\n'):
        passport = Passport.from_input_str(line.replace('\n', " "))
        passports.append(passport)

    # iterate through passports and count the number that are valid
    total_valid = 0
    for passport in passports:
        if passport.has_valid_fields(required_fields=required_fields):
            total_valid += 1

    return total_valid
Пример #4
0
def part_one(input_file: str, last_turn: int) -> int:
    # read input list
    inputs = read_input_blob(input_file)

    # part to form starting list
    starting_list = [int(num) for num in inputs.split(',')]

    # instantiate turn counter, last work and dict to keep track data about the number
    # that was last said
    turn = 0
    last_spoken_num = None

    previously_spoken = dict()

    for num in starting_list:
        turn += 1
        last_spoken_num = num
        previously_spoken[num] = NumberData(times_spoken=1, last_spoken=turn)

    # follow rules based on last_spoken_num until we reach our turn limit
    while turn != last_turn:
        turn += 1
        # if last spoken work was spoken for the first time then the cur num spoken
        # is 0, otherwise it is the delta between the last spoken turn and the next to
        # last spoken turn
        if previously_spoken[last_spoken_num].times_spoken == 1:
            cur_num = 0
        else:
            cur_num = previously_spoken[
                last_spoken_num].last_spoken - previously_spoken[
                    last_spoken_num].next_to_last_spoken

        # update the previously spoken dict with the current num
        if cur_num not in previously_spoken:
            previously_spoken[cur_num] = NumberData(times_spoken=1,
                                                    last_spoken=turn)
        else:
            previously_spoken[cur_num].times_spoken += 1
            previously_spoken[cur_num].next_to_last_spoken = previously_spoken[
                cur_num].last_spoken
            previously_spoken[cur_num].last_spoken = turn

        last_spoken_num = cur_num

    return last_spoken_num
Пример #5
0
def part_two(input_file: str):
    # read and parse input file
    inputs = read_input_blob(input_file)
    _, raw_buses = inputs.split('\n')

    # process inputs and get diffs for crt
    buses = [int(bus) if bus[0] != 'x' else 0 for bus in raw_buses.split(",")]
    diffs = [bus - i for i, bus in enumerate(buses)]

    # remove the 0s (formerly xs) from buses and difs
    rel_buses = []
    rel_diffs = []
    for i, bus in enumerate(buses):
        if bus > 0:
            rel_buses.append(bus)
            rel_diffs.append(diffs[i])

    return chinese_remainder(rel_buses, rel_diffs)
Пример #6
0
def part_one(input_file: str):
    # read and parse input file
    inputs = read_input_blob(input_file)
    raw_earliest_time, raw_potential_buses = inputs.split('\n')

    # process inputs
    earliest_time = int(raw_earliest_time)
    potential_buses = [int(bus) for bus in raw_potential_buses.replace(',x', '').split(',')]

    # for each bus get the closest time the bus departs from our earliest time and track min
    min_wait_bus_id = 0
    min_wait_time = float('inf')
    for bus in potential_buses:
        wait_time =  math.ceil(earliest_time / bus) * bus - earliest_time
        if wait_time < min_wait_time:
            min_wait_time = wait_time
            min_wait_bus_id = bus

    return min_wait_bus_id * min_wait_time
Пример #7
0
def part_two(input_file: str) -> int:
     # read input file
    inputs = read_input_blob(input_file)

    # split groups and then split each member for the group
    groups = []
    for line in inputs.split('\n\n'):
        group = line.split('\n')
        groups.append(group)

    # iterate through groups and get the count of questions that
    # were answered by all memebers, tracking total along the way
    total_all_yes_counts = 0
    for group in groups:
        for question in group[0]:
            if all([question in memember for memember in group]):
                total_all_yes_counts += 1

    return total_all_yes_counts