Ejemplo n.º 1
0
def part_1(value_list, timestamp=1000340):
    value_list = list(filter(filter_values, value_list.split(',')))
    value_list = convert_to_int(value_list)

    best = sys.maxsize
    to_return = 0

    for idx in range(len(value_list)):
        if value_list[idx] - (timestamp % value_list[idx]) < best:
            to_return = value_list[idx]
            best = value_list[idx] - (timestamp % value_list[idx])

    return best * to_return
Ejemplo n.º 2
0
def part_1(value_list):
    counter = 0

    for idx, value in enumerate(value_list):
        if idx % 3 == 0:
            value_range = convert_to_int(value.split('-'))

            letter = value_list[idx + 1][0:1]
            password = value_list[idx + 2]

            letter_occurrences = password.count(letter)

            if value_range[0] <= letter_occurrences <= value_range[1]:
                counter += 1

    return counter
Ejemplo n.º 3
0
def part_1_recursive(value_list, counter):
    print(len(value_list))

    if len(value_list) == 0:
        return counter
    else:
        value_range = convert_to_int(value_list[0].split('-'))
        letter = value_list[1][0:1]
        password = value_list[2]

        letter_occurrences = password.count(letter)

        if value_range[0] <= letter_occurrences <= value_range[1]:
            counter += 1

        return part_1_recursive(value_list[3:], counter)
Ejemplo n.º 4
0
def part_1(value_list, scan_error=0):
    if len(value_list) == 0:
        return scan_error

    to_check = convert_to_int(value_list[0].split(','))

    for val in range(len(to_check)):
        found = False
        for k in fields:
            if fields.get(k, lambda x: False)(to_check[val]):
                found = True
                break
        if not found:
            scan_error += to_check[val]

    return part_1(value_list[1:], scan_error)
Ejemplo n.º 5
0
def part_2(value_list):
    counter = 0

    for idx, value in enumerate(value_list):
        if idx % 3 == 0:
            value_range = convert_to_int(value.split('-'))

            letter = value_list[idx + 1][0:1]
            password = value_list[idx + 2]

            idx0 = value_range[0] - 1
            idx1 = value_range[1] - 1

            if password[idx0] == letter or password[idx1] == letter:
                if password[idx0] != password[idx1]:
                    counter += 1

    return counter
Ejemplo n.º 6
0
    for i in range(list_length):
        for k in range(i + 1, list_length):
            idx = binary_search_recursive(value_list,
                                          2020 - value_list[i] - value_list[k],
                                          k, list_length)

            if idx != -1:
                return value_list[i] * value_list[k] * value_list[idx]


if __name__ == '__main__':
    dir_path = os.path.dirname(os.path.realpath(__file__))

    input_read = read_file(dir_path + "/" + "input1.txt")

    value_list = convert_to_int(input_read)
    value_list.sort()

    start_time = time.time()
    print("part 1: ", part_1(value_list))
    print("--- %s seconds ---" % (time.time() - start_time), "\n")

    start_time = time.time()
    print("part 1 // binary search: ", part_1_binary_search(value_list))
    print("--- %s seconds ---" % (time.time() - start_time), "\n")

    start_time = time.time()
    print("part 2: ", part_2(value_list))
    print("--- %s seconds ---" % (time.time() - start_time), "\n")

    start_time = time.time()
Ejemplo n.º 7
0
def part_2(value_list):
    counter = 0
    idx = 0

    while counter < 393911906:
        counter += value_list[idx]
        idx += 1

    if counter == 393911906:
        partial = value_list[:idx]
        partial.sort()
        return partial[0] + partial[len(partial) - 1]
    else:
        return part_2(value_list[1:])


if __name__ == '__main__':
    dir_path = os.path.dirname(os.path.realpath(__file__))

    input_read = read_file(dir_path + "/" + "input1.txt", "\n")

    input_read = convert_to_int(input_read)

    start_time = time.time()
    print('part 1:', part_1(input_read))
    print("--- %s seconds ---" % (time.time() - start_time), "\n")

    start_time = time.time()
    print('part 2:', part_2(input_read))
    print("--- %s seconds ---" % (time.time() - start_time), "\n")
Ejemplo n.º 8
0
def part_2(value_list, value_list_parsed=None):
    if value_list_parsed is None:
        value_list_parsed = []

    if len(value_list) == 0:

        final = []

        for idx in range(len(value_list_parsed[0])):
            partial = []
            for idx2 in range(len(value_list_parsed)):
                partial.append(value_list_parsed[idx2][idx])
            final.append(partial)

        keys = defaultdict(list)

        for idx3 in range(len(final)):
            for k in fields:
                found = True
                for val in range(len(final[idx3])):
                    if not fields.get(k, lambda x: False)(final[idx3][val]):
                        found = False
                        break
                if found:
                    if not keys[idx3]:
                        to_append = []
                    else:
                        to_append = keys[idx3]

                    keys[idx3] = [*to_append, k]

        has_finished = False
        already_done = []

        while not has_finished:
            for k in keys:
                if k not in already_done and len(keys[k]) == 1:
                    already_done.append(k)
                    for k2 in keys:
                        if k is not k2 and keys[k][0] in keys[k2]:
                            keys[k2].remove(keys[k][0])

            for k_idx in keys:
                if len(keys[k_idx]) > 1:
                    has_finished = False
                    break
                else:
                    has_finished = True

        input_read = read_file(dir_path + "/" + "my.txt", ",")
        input_read = convert_to_int(input_read)

        total = 1

        for k in keys:
            if 'departure' in keys[k][0]:
                total *= input_read[k]

        return total

    to_check = convert_to_int(value_list[0].split(','))

    for val in range(len(to_check)):
        found = False
        for k in fields:
            if fields.get(k, lambda x: False)(to_check[val]):
                found = True
                break
        if not found:
            return part_2(value_list[1:], value_list_parsed)

    value_list_parsed.append(to_check)

    return part_2(value_list[1:], value_list_parsed)