from itertools import permutations
from .randutils import get_matrices

def generate_all_permutations(input_list):
    return list(permutations(input_list))

def run_permutation(permutation, M, N, K):
    A,B,C = get_matrices(M, N, K)

    #GENERATED by ChatGPT, seems correct to my eyes
    # Map from number to loop variable name and range
    loop_vars = {
        1: ('i', range(M)),
        2: ('j', range(N)),
        3: ('k', range(K))
    }

    var_names = []
    ranges = []
    for x in permutation:
        name, rng = loop_vars[x]
        var_names.append(name)
        ranges.append(rng)

    for a in ranges[0]:
        for b in ranges[1]:
            for c in ranges[2]:
                # Map loop variables to actual names
                loop_vals = {var_names[0]: a, var_names[1]: b, var_names[2]: c}
                i = loop_vals['i']
                j = loop_vals['j']
                k = loop_vals['k']
                C[i, j] += A[i, k] * B[k, j]

    return C