コード例 #1
0
    sorted_jolts.sort()
    # Adding my device
    sorted_jolts.append(sorted_jolts[-1]+3)
    previous_joltage = 0
    branches = 1
    ones_handler = 0
    for jolt in sorted_jolts:
        if jolt - previous_joltage == 1:
            ones_handler += 1
        elif jolt - previous_joltage == 3:
            if ones_handler == 0:
                branches *= 1
            elif ones_handler == 1:
                branches *= 1
            elif ones_handler == 2:
                branches *= 2
            elif ones_handler == 3:
                branches *= 4
            elif ones_handler == 4:
                branches *= 7
            else:
                branches *= 0
            ones_handler = 0
        else:
            print("Error")
        previous_joltage = jolt
    print("Ways to connect:", branches)


lvl_runner(sys.argv, first_combination, all_combinations)
コード例 #2
0
import sys
import re
from utils import records_getter, lvl_runner


def oldPass():
    passwords = records_getter('d2_passwords.txt')
    correct_passwords = 0
    for passw in passwords:
        l_lim, h_lim, letter, code = re.findall(
            "^(\d+)-(\d+)\s(\w):\s(\w+)$", passw)[0]
        occurs = code.count(letter)
        if int(l_lim) <= occurs <= int(h_lim):
            correct_passwords += 1
    print("Old password is:", correct_passwords)


def newPass():
    passwords = records_getter('d2_passwords.txt')
    correct_passwords = 0
    for passw in passwords:
        l_lim, h_lim, letter, code = re.findall(
            "^(\d+)-(\d+)\s(\w):\s(\w+)$", passw)[0]
        if (code[int(l_lim)-1] == letter) ^ (code[int(h_lim)-1] == letter):
            correct_passwords += 1
    print("New password is:", correct_passwords)


lvl_runner(sys.argv, oldPass, newPass)
コード例 #3
0
    cmd = cmd_getter()
    memory_v2 = {}
    for c in cmd:
        if c[0] == -1:
            actual_mask = c[1]
        else:
            mem_v = "{:0>36b}".format(int(c[0]))
            mask_temp = "".join([
                mem_v[idx] if m == "0" else m
                for idx, m in enumerate(actual_mask)
            ])
            combination = 2**mask_temp.count('X')
            for i in range(combination):
                mm = ""
                i_fix = ("{:0>" + str(mask_temp.count('X')) + "b}").format(
                    int(i))
                character_no = 0
                for im in mask_temp:
                    if im == "X":
                        mm += i_fix[character_no]
                        character_no += 1
                    else:
                        mm += im
                memory_v2[mm] = c[1]

    print("Suma of the whole memory with floating points:",
          sum([int(i) for i in memory_v2.values()]))


lvl_runner(sys.argv, first_lvl, second_lvl)
コード例 #4
0
import sys
from utils import records_getter, lvl_runner


def second_lvl():
    strings = records_getter("d1_elves.txt")
    digits = [int(i) for i in strings]

    for idx, digit in enumerate(digits):
        for idxx, d in enumerate(digits[idx+1:]):
            for dd in digits[idxx+2:]:
                if digit + d + dd == 2020:
                    print(digit * d * dd)
                    break


lvl_runner(sys.argv, None, second_lvl)
コード例 #5
0
        else:
            last_occurence = max(loc for loc, val in enumerate(
                first_eg[:i]) if val == first_eg[i])
            first_eg.append(i-last_occurence)

    print("2020th word is", first_eg[-1], "   [O(n2)]")


def big_o_linear():
    # init
    second_eg = input.copy()
    occur_dict = {}
    for idx, sec in enumerate(second_eg[:-1]):
        occur_dict[sec] = idx

    # play game
    for i in range(len(second_eg)-1, 30000000-1):
        if not second_eg[i] in occur_dict:
            second_eg.append(0)
        else:
            second_eg.append(i-occur_dict.get(second_eg[i]))
        occur_dict[second_eg[i]] = i

        if(i % 2000000 == 0):
            print("Iter = ", "{:.1%}".format(i/30000000), end='\r')

    print("30.000.000th number is", second_eg[-1], "   [O(n)]")


lvl_runner(sys.argv, big_o_square, big_o_linear)
コード例 #6
0
        for cc in containing_color[old_temp:]:
            for bag in bags:
                for ins in bag["containing"]:
                    if cc in ins:
                        containing_color.append(bag["name"])
        new_bunch = len(containing_color)
    # print(containing_color)
    print("As set:", len(set(containing_color)) - 1)


def how_many_bugs_to_buy():
    bags = creating_rules_list()
    containing_color = [(1, "shiny gold")]
    new_bunch = 1
    old_bunch = 0
    # How many bags in mine
    while new_bunch > old_bunch:
        old_temp = old_bunch
        old_bunch = len(containing_color)
        for cc in containing_color[old_temp:]:
            for br in bags:
                if br["name"] == cc[1]:
                    for b in br["containing"]:
                        containing_color.append((int(cc[0]) * int(b[0]), b[2]))
            new_bunch = len(containing_color)

    print(sum([i[0] for i in containing_color]) - 1)


lvl_runner(sys.argv, where_is_my_bag, how_many_bugs_to_buy)
コード例 #7
0
            all_answers = ""
        else:
            all_answers += line
    print("Anyone:", total)


def every_one():
    lines = records_getter("d6_questionaire.txt")
    team_member_answers = []
    total = 0
    for line in lines:
        if line == "":
            if len(team_member_answers) == 1:
                answers_no = len(team_member_answers[0])
            else:
                left_answers = team_member_answers[0]
                for tma in team_member_answers[1:]:
                    left_answers = [i for i in left_answers if i in tma]
                answers_no = len(left_answers)

            total += answers_no
            # print(answers_no)
            team_member_answers = []
        else:
            team_member_answers.append(line)

    print("Everyone:", total)


lvl_runner(sys.argv, any_one, every_one)
コード例 #8
0
    seats = records_getter("d11_seating.txt")
    seats = [list(row) for row in seats]
    seats = np.array(seats)
    seats_padded = np.pad(seats, (1, 1), constant_values='.')

    col_number = len(seats_padded[0])
    row_number = len(seats_padded)

    previous_seats = seats_padded.copy()
    stabilized = False
    while not stabilized:
        stabilized = True
        for row in range(1, row_number-1):
            for col in range(1, col_number-1):
                if previous_seats[row][col] == "L":
                    if sight_checker(previous_seats, (row, col)) == 0:
                        seats_padded[row][col] = "#"
                        stabilized = False
                elif previous_seats[row][col] == "#":
                    if sight_checker(previous_seats, (row, col)) >= 5:
                        seats_padded[row][col] = "L"
                        stabilized = False
        previous_seats = seats_padded.copy()

    unique, counts = np.unique(seats_padded, return_counts=True)
    dict_coup_count = dict(zip(unique, counts))
    print("Occupied seats when 5 in sight range:", dict_coup_count['#'])


lvl_runner(sys.argv, lvl_first, lvl_second)
コード例 #9
0
pattern_len = rows[0].__len__()


def slop_counter(right, down=1):
    f = 0
    encounter = 0

    for row in rows[::down]:
        if row[f % pattern_len] == '#':
            encounter += 1
        f += right
    return encounter


def first_slop():
    print(f"Tobbogan encounter {slop_counter(3)} trees")


def couple_slides():
    traverses = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]]

    mult = 1
    for travers in traverses:
        mult *= slop_counter(*travers)

    print("Multiplication of slides:", mult)


lvl_runner(sys.argv, first_slop, couple_slides)
コード例 #10
0
    v = re.findall(r"^\#[0-9a-f]{6}$", passport_data_dict.get('hcl'))
    if not v:
        return False
    v = re.findall(r"^(amb|blu|brn|gry|grn|hzl|oth)$",
                   passport_data_dict.get('ecl'))
    if not v:
        return False
    v = re.findall(r"^(\d{9})$",
                   passport_data_dict.get('pid'))
    if not v:
        return False
    return True


def passport_checker():
    handler = ""
    counter = 0
    all = 0
    for line in lines:
        if line == "":
            if passport_veryfier(handler):
                counter += 1
            all += 1
            handler = ""
        else:
            handler += (" " + line)
    print("Valid passports:", counter, " out of", all)


lvl_runner(sys.argv, None, passport_checker)
コード例 #11
0
            else:
                m = -1
            line += m * int(commands[line][5:])
        commands[line_save] = "ed"
        if line == len(commands):
            return True, acc
        executed_line_iter += 1
    return False, acc


def fixing_loop():
    commands = records_getter("d8_gameboy.txt")
    save_commands = commands.copy()
    fixing_set = 0
    terminate = False
    acc = 0
    # Second star
    while not terminate:
        terminate, acc = prg_runner(fixing_set)
        # Reset and next command fixing
        commands = save_commands.copy()
        fixing_set += 1
    print("An accumultor-fixed value: ", acc)


def test_prg():
    print("An accumulator value, after execute: ", prg_runner()[1])


lvl_runner(sys.argv, test_prg, fixing_loop)