Esempio n. 1
0
def total_required_fuel_accounting_for_fuel(starting_mass: int = None) -> int:
    if not starting_mass:
        module_mass_list = read_input(input_file=_INPUT_FILE)
        module_fuel_requirements = (module_required_fuel(mm)
                                    for mm in module_mass_list)
    else:
        module_fuel_requirements = [module_required_fuel(starting_mass)]
    return sum(mfr + fuel_required_for_fuel(mfr)
               for mfr in module_fuel_requirements)
Esempio n. 2
0
def find_noun_verb_for_value(value: int) -> List[int]:
    for n in range(100):
        for v in range(100):
            logging.info("Trying noun {} and verb {}".format(n, v))
            intcode = read_input(input_file=_INPUT_FILE)
            intcode[1] = n
            intcode[2] = v
            intcode = process_intcode_with_condition(intcode, value)
            if intcode[0] == value:
                logging.info("Found noun {} and verb {} for value {}".format(
                    n, v, intcode[0]))
                return n, v
Esempio n. 3
0
def main():
    orbits = read_input(input_file=_INPUT_FILE)
    [process_orbit(orbit) for orbit in orbits]
    g = Graph(len(space_collection.keys()))
    int_space_collection = {
        s: i
        for i, s in enumerate(space_collection.keys())
    }
    print(int_space_collection)
    for k in space_collection.keys():
        print(k, space_collection[k].orbiting)
        current_int = int_space_collection[k]
        if space_collection[k].orbiting != None:
            orbiting_int = int_space_collection[space_collection[k].orbiting]
            g.addEdge(current_int, orbiting_int, 1)
            g.addEdge(orbiting_int, current_int, 1)
    print(f"SANTA is {int_space_collection['SAN']}")
    print(f"Z3P is {int_space_collection['Z3P']}")
    g.shortestPath(int_space_collection['YOU'])
Esempio n. 4
0
from part1 import read_input, find_sum_pair


def find_sum_trio(target_value, arr):
    if len(arr) < 3: raise ValueError
    try:
        num1, num2 = find_sum_pair(target_value - arr[0], arr[1:])
        return (arr[0], num1, num2)
    except:
        return find_sum_trio(target_value, arr[1:])


if __name__ == '__main__':
    num1, num2, num3 = find_sum_trio(2020, read_input())
    print("{0} * {1} * {2} = {3}".format(num1, num2, num3, num1 * num2 * num3))
Esempio n. 5
0
from part1 import read_input

def extract_max_delta(arr, base, delta):
    subarr = []
    for num in arr:
        if num <= base+delta:
            subarr.append(num)
        else:
            return subarr
    return subarr

def count_combos(arr, target, delta, history=set()):
    first = arr[0]
    if first in history: return 0 # already been counted
    subarr = extract_max_delta(arr[1:], first, delta)
    #print("subarr: ({}) {}".format(first, subarr))
    count = 1 if len(subarr) > 0 else 0
    for subindex in range(0, len(subarr)): # [(1), 2, 5, 6]
        count += count_combos(arr[(1+subindex):], target, delta, history)
    history.add(first)
    #history.clear()
    return count

if __name__ == '__main__':
    nums = read_input()
    delta = 3
    target = max(nums) + delta
    print("{}".format(count_combos(nums, target, delta)-2))
Esempio n. 6
0
def main():
    layers = build_layers(read_input(input_file=_INPUT_FILE))
    return build_image(layers)
Esempio n. 7
0
        return (current_loc[0] - length, current_loc[1]), {(current_loc[0] - l, current_loc[1]): current_steps+l for l in range(length + 1)}, current_steps + length 


def find_intersections(cable_paths: Generator[Tuple[str, str], None, None]):
    current_loc_a = (0,0)
    current_loc_b = (0,0)
    current_steps_a = 0
    current_steps_b = 0
    cable_a_locations = {(0,0): 0}
    cable_b_locations = {(0,0): 0}
    for mov_a, mov_b in cable_paths:
        current_loc_a, positions_a, current_steps_a = str_to_location(current_loc_a, mov_a, current_steps_a)
        cable_a_locations = {**positions_a, **cable_a_locations}
        current_loc_b, positions_b, current_steps_b = str_to_location(current_loc_b, mov_b, current_steps_b)
        cable_b_locations = {**positions_b, **cable_b_locations}
    intersection = set(cable_a_locations.keys()).intersection(set(cable_b_locations.keys()))
    return [cable_a_locations[intpoint] + cable_b_locations[intpoint] for intpoint in intersection if intpoint != (0,0)]


def main(cable_paths: Generator[Tuple[str, str], None, None]):
    intersection_points = find_intersections(cable_paths)
    return min(intersection_points)

if __name__  == '__main__':
    if len(sys.argv) > 2:
        cable_path_a = sys.argv[1].split(',')
        cable_path_b = sys.argv[2].split(',')
        cable_paths = zip(cable_path_a, cable_path_b)
    else:
        cable_paths = read_input(input_file=_INPUT_FILE)
    print(f"Minimum number of steps is: {main(cable_paths)}")
Esempio n. 8
0
import part1

if __name__ == "__main__":
    s = part1.read_input("input.txt")

    results = {}
    # store the length of reaction string when 'c' is removed
    for c in "abcdefghijklmnopqrstuvwxyz":
        results[c] = len(part1.polymer_reaction(s, c))

    # find the minimum value in the dictionary
    minimum_val = min(results.items())
    # what letter does it correspond to
    result = [key for key, val in results.items() if val == minimum_val][0]
    print("Answer: {0} ('{1}')".format(results[result], result))