Пример #1
0
                            to_analyze[an_idx]) + " - " + str(
                                value_list[to_analyze[an_idx]])
        else:
            already_visited.append(actual_index)
            values = val.split()

            if values[0] == 'acc':
                accumulator += int(values[1])
                continue
            elif values[0] == 'jmp':
                return part_2(value_list, already_visited,
                              actual_index + int(values[1]), accumulator)
            elif values[0] == 'nop':
                continue


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

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

    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(input_read))
    print('part 2:', part_1(input_read_2))
    print("--- %s seconds ---" % (time.time() - start_time), "\n")
Пример #2
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)
Пример #3
0
        lambda x: number_range_validation(x, 9, [0, sys.maxsize]),
        'cid':
        lambda x: 1 == 1
    }

    passport_dict = {
        passport[i][0:3]: passport[i][4:]
        for i in range(0, len(passport))
    }

    if all(k in passport_dict for k in fields) and all(
            validation.get(k, lambda x: False)(v)
            for (k, v) in passport_dict.items()):
        return part_2(value_list[1:], valid + 1)
    else:
        return part_2(value_list[1:], valid)


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

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

    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")
Пример #4
0
        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)


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

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

    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")