Beispiel #1
0
def run_part_2(in_file: str, _weakness, debug: bool = False) -> int:
    pretty.printHeader(DAY, 2, inspect.stack()[0].function, in_file)
    result = 0
    numbers = loadingUtils.importToIntArray(in_file)
    result = find_weaknes(numbers, _weakness)
    print("Result = {}".format(result))
    return result
Beispiel #2
0
def run_part_1(in_file: str, preamble=25, debug: bool = False) -> int:
    pretty.printHeader(DAY, 1, inspect.stack()[0].function, in_file)
    result = 0
    numbers = loadingUtils.importToIntArray(in_file)
    result = find_wrong_number(numbers, preamble, debug)
    print("Result = {}".format(result))
    return result
Beispiel #3
0
def run_part_1(in_file: str, debug: bool = False) -> int:
    pretty.printHeader(DAY, 1, inspect.stack()[0].function, in_file)
    result = 0
    adapters = loadingUtils.importToIntArray(in_file)
    adapters.sort()
    print(adapters)
    result = validate_chain(adapters)
    # code here
    print("Result = {}".format(result))
    return result
Beispiel #4
0
def run_part_2_no_cache(in_file: str, debug: bool = False) -> int:
    pretty.printHeader(DAY, 2, inspect.stack()[0].function, in_file)
    result = 0
    _adapters = loadingUtils.importToIntArray(in_file)
    _adapters.append(0)
    _adapters.sort()
    adapters = tuple(_adapters)
    print(adapters)
    result = get_number_of_chains_no_cache(adapters)
    # code here
    print("Result = {}".format(result))
    return result
Beispiel #5
0
def runPart1(inFile: str, debug: bool = False) -> int:
    pretty.printHeader(DAY, 1, inspect.stack()[0].function, inFile)
    result = 0
    numbers = loadingUtils.importToIntArray(inFile)
    results = []
    for i in range(len(numbers)):
        a = numbers[i]
        for j in range(i, len(numbers)):
            b = numbers[j]
            if debug: print("{} {} : {} + {} = {}".format(i, j, a, b, a + b))
            if (a + b == 2020):
                results.append((a, b))
    print(results)
    print(len(results))
    assert len(results) != 0
    assert len(results[0]) == 2
    result = results[0][0] * results[0][1]
    print("Numbers {} and {} add to 2020 and have a product of {}".format(
        results[0][0], results[0][1], result))
    return result
Beispiel #6
0
def run_part_2(in_file: str, debug: bool = False) -> int:
    """
    Runs part 2 of the day
    """
    pretty.printHeader(DAY, 2, inspect.stack()[0].function, in_file)
    result = 0
    numbers = loadingUtils.importToIntArray(in_file)
    results = []
    # Code here
    ## itertools.product(numbers, repeat=3) gives many repetitions
    for tripel in itertools.combinations(numbers, 3):
        if debug:
            print(tripel)
        if functools.reduce(lambda a, b: a + b, tripel) == 2020:
            results.append(tripel)
    print(results)
    print(len(results))
    assert len(results) != 0
    assert len(results[0]) == 3
    winner_tripel = results[0]
    result = functools.reduce(lambda a, b: a * b, results[0])
    print("Numbers {} add to 2020 and have a product of {}".format(
        winner_tripel, result))
    return result
def test_importToIntArray():
    out = loadingUtils.importToIntArray("helper/test_input_importToArray")
    assert out == [2, 4, 6, 8, 99, -42]