Example #1
0
def part_two(input_file: str) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    # parse inputs into instructions
    original_instructions = [
        Instruction(line.split(' ')[0], int(line.split(' ')[1]))
        for line in inputs
    ]

    # find all of the nop, or jump instruction indexes instructions idx
    corruptions_candidate_fixes = []
    for i, instr in enumerate(original_instructions):
        if instr.operation == 'nop':
            corruptions_candidate_fixes.append((i, 'jmp'))

        if instr.operation == 'jmp':
            corruptions_candidate_fixes.append((i, 'nop'))

    # iterate through the candidate fixes, create a fixed instruction set, try to run and if
    # we terminate then we found the fix and can return the accumulator
    for i, new_op in corruptions_candidate_fixes:

        # create deep copy of original instructions
        new_instructions = copy.deepcopy(original_instructions)

        # fix current candidate instruction
        new_instructions[i].operation = new_op

        program = Program(new_instructions)
        program.run_until_loop_detected_or_completion()

        if program.is_terminated:
            return program.accumulator
Example #2
0
def part_one(input_file: str, task: str) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    #instantiate WaitingArea object and run until stable
    waiting_area = WaitingArea(inputs=inputs, task=task)
    waiting_area.run_process_until_stable()

    return waiting_area.get_occupied_count()
Example #3
0
def part_one(input_file: str, total: int) -> int:
    # read in input file
    expenses = read_input_list(input_file, line_type=int)

    # iterate through the values and get what the other value would
    # need to be for this pair to be the solution
    for value in expenses:
        other_value = total - value
        if other_value in expenses:
            return value * other_value
Example #4
0
def part_two(input_file: str, total: int) -> int:
    # read in input file
    expenses = read_input_list(input_file, line_type=int)

    # iterate through with indexes so we dont re-check the first pair of
    # numbers
    for i, value in enumerate(expenses):
        for second_value in expenses[i + 1:]:
            third_value = total - value - second_value
            if third_value in expenses:
                return value * second_value * third_value
Example #5
0
def part_two(input_file: str) -> int:
    # read input fil
    inputs = read_input_list(input_file, strip_new_line=True)

    # parse into instructions and instantiate boat
    instructions = [Instruction(instr[0], int(instr[1:])) for instr in inputs]
    boat = Boat(instructions=instructions)

    # navigate and return final distance
    boat.naviagate_waypoint()

    return boat.get_manhatten_dist()
Example #6
0
def part_one(input_file: str, plane_size: Tuple[int, int]) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    # iterate through tickets and find the seat id
    # keep track of max seat id along the way
    max_seat_id = -float('inf')
    for ticket in inputs:
        seat_id = find_seat(ticket=ticket, plane_size=plane_size)
        if seat_id > max_seat_id:
            max_seat_id = seat_id

    return max_seat_id
Example #7
0
def part_one(input_file: str) -> int:
    # read input file
    inputs = read_input_list(input_file, line_type=int)

    # sort inputs to get adapters in order adding outlet and computer in as well
    adapters = [0] + sorted(inputs) + [max(inputs) + 3]

    # we know we have to use min joltage jump we will have to use all of our adapters
    # so we can just take the difs of all out adapters
    differences = Counter(
        [cur - prev for (prev, cur) in zip(adapters, adapters[1:])])

    return differences[1] * differences[3]
Example #8
0
def part_one(input_file: str, preamble_len: int) -> int:
    # read input file
    inputs = read_input_list(input_file, line_type=int)

    # iterate over all values starting after the preamble and
    # check if they are valid. Return once we find the first invalid
    for i, num in enumerate(inputs[preamble_len:]):
        # since we are enumerating starting after the preamble the i in our
        # loop will actually be the start of the preamble (our first value is inputs[25])
        # and our i = 0, so the preamble is the slice [i, i + preamble_len]
        preamble = inputs[i:i + preamble_len]
        if not is_valid(num, preamble):
            return num
Example #9
0
def part_two(input_file: str) -> int:
    # read input file
    inputs = read_input_list(input_file)

    # parse inputs
    passwords = [
        PasswordData.from_input_str(input_str) for input_str in inputs
    ]

    # iterate over all passwords and count if valid
    total_valid = 0
    for password in passwords:
        if password.is_valid_position:
            total_valid += 1

    return total_valid
Example #10
0
def part_one(input_file: str) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    # parse inputs into instructions
    instructions = [
        Instruction(line.split(' ')[0], int(line.split(' ')[1]))
        for line in inputs
    ]

    # instantiate the program and run until we detect a loop
    program = Program(instructions)
    program.run_until_loop_detected_or_completion()

    #return the value of the accumulator
    return program.accumulator
Example #11
0
def part_two(input_file: str, plane_size: Tuple[int, int]) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    # collect known seats
    known_seats = [
        find_seat(ticket=ticket, plane_size=plane_size) for ticket in inputs
    ]

    # for each seat check if there is an empty seat on either side of the seat
    # between another known seat
    for seat in known_seats:
        if seat - 1 not in known_seats and seat - 2 in known_seats:
            return seat - 1

        if seat + 1 not in known_seats and seat + 2 in known_seats:
            return seat + 1
Example #12
0
def part_one(input_file: str, slope: Tuple[int, int]) -> int:
    # read input file
    inputs = read_input_list(input_file)

    # parse rows into cols and instantiate area
    input_map = [list(line.strip('\n')) for line in inputs]
    area = Area(map=input_map, slope=slope)
    
    # traverse area until we get to the bottom and count trees along
    # the way
    total_trees = 0
    while True:
        area.move()
        if area.reached_bottom:
            break
        
        if area.is_on_tree:
            total_trees += 1

    return total_trees
Example #13
0
def part_two(input_file: str) -> int:
    # read input file
    inputs = read_input_list(input_file, line_type=int)

    # sort inputs to get adapters in order adding outlet and computer in as well
    adapters = [0] + sorted(inputs) + [max(inputs) + 3]

    # we have too many branches to brute force so we need to use dynamic programming
    # let paths[i] be the total number of paths that end adapter i.
    # We know that we can use adapters where there is a voltage difference of 1, 2 or 3
    # therefore paths[i] is paths[i-3] + paths[i-2] + paths[i-1]
    paths = [0 for _ in range(adapters[-1])]

    # fill in the base case, there is one way to have joltage 0
    paths = [1] + paths

    for adapter in adapters[1:]:
        for dif in range(1, 4):
            if adapter - dif >= 0:
                paths[adapter] += paths[adapter - dif]

    return paths[-1]
Example #14
0
def part_one(input_file: str, desired_bag_color: str) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    # parse our rules and create a mapping of bags > inner bags
    bag_tree = {}
    for line in inputs:
        raw_bag_color, raw_inner_bags = line.split('contain')

        bag_color = "_".join(raw_bag_color.replace('bags', '').strip().split())
        inner_bags = process_raw_inner_bags(raw_inner_bags)
        bag_tree[bag_color] = inner_bags

    # initialize container for valid outer colors and do a breadth first search
    # starting at each outer color and see if we find the desired color

    bags_containing_desired_color = set()

    def breadth_first_search(initial_color: str, desired_color: str) -> None:
        queue = [
            inner_bag.color for inner_bag in bag_tree.get(initial_color, [])
        ]

        while queue:

            current_bag_color = queue.pop()

            if current_bag_color == desired_color:
                bags_containing_desired_color.add(initial_color)
                return

            inner_bags = bag_tree.get(current_bag_color, [])
            for inner_bag in inner_bags:
                queue.append(inner_bag.color)

    for initial_color in bag_tree:
        breadth_first_search(initial_color, desired_bag_color)

    return len(bags_containing_desired_color)
Example #15
0
def part_two(input_file: str, preamble_len: int) -> int:
    # read input file
    inputs = read_input_list(input_file, line_type=int)

    # get target value from part one
    target = part_one(input_file, preamble_len)

    # perform a brute force search looking for the cumulative sum equal to our targe
    range_found = False

    for i in range(len(inputs)):
        for j in range(i + 1, len(inputs)):
            if sum(inputs[i:j + 1]) == target:
                range_found = True
                break

        if range_found:
            break

    minimum = min(inputs[i:j + 1])
    maximum = max(inputs[i:j + 1])

    return minimum + maximum
Example #16
0
def part_two(input_file: str, desired_bag_color: str) -> int:
    # read input file
    inputs = read_input_list(input_file, strip_new_line=True)

    # parse our rules and create a mapping of bags > inner bags
    bag_tree = {}
    for line in inputs:
        raw_bag_color, raw_inner_bags = line.split('contain')

        bag_color = "_".join(raw_bag_color.replace('bags', '').strip().split())
        inner_bags = process_raw_inner_bags(raw_inner_bags)
        bag_tree[bag_color] = inner_bags

    def breadth_first_search(initial_color: str, total_bags) -> None:
        # initialize queue of tupples of bags to count along with the number of
        # "parent bags" to multiply our the quantities
        queue = [(inner_bag, 1)
                 for inner_bag in bag_tree.get(initial_color, [])]

        while queue:

            # pop off next bag with parent count and add the number of this bag to the
            # total
            current_bag, parent_count = queue.pop()
            total_bags += current_bag.amount * parent_count

            # iterate over the inner bags of the current bag and add to queue with new
            # parent count
            for inner_bag in bag_tree.get(current_bag.color, []):
                queue.append((inner_bag, parent_count * current_bag.amount))

        return total_bags

    total_bags = breadth_first_search(desired_bag_color, 0)

    return total_bags