import time


# Global variable to track the highest found
max_solution = (0.0, "", 0)
# benchmarking code
total = 0
c = 0
start = time.time()
t = time.time()
# end benchmarking code


def process_solution(s):
    # abuse of globals
    global max_solution
    # benchmarking code
    global total
    global t
    global start
    # end benchmarking code
    for i in range(14):
        try:
            val = apply_op(s, i)
        except ZeroDivisionError:
            continue
        if val > max_solution[0]:
            max_solution = (val, s, i)
            print(f"Found new best {max_solution}")
    # benchmarking code
    if c % 1024 == 0:
        end = time.time()
        runtime = end - start
        # print rate about every 5 seconds
        if end - t > 5.0:
            print(f"Rate: {float(total) / runtime}")
            t = time.time()
    total += 24
    # end benchmarking code


def apply_op(s, i):
    [a, b, c, d, e] = s
    if i == 0:
        return (((a * b) / c) + d) - e
        # return (((a * b) / c) - d) + e
        # return (((a / b) * c) + d) - e
        # return (((a / b) * c) - d) + e
    elif i == 1:
        return (((a + b) - c) * d) / e
        # return (((a + b) - c) / d) * e
        # return (((a - b) + c) * d) / e
        # return (((a - b) + c) / d) * e
    elif i == 2:
        return (((a * b) + c) - d) / e
        # return (((a * b) - c) + d) / e
    elif i == 3:
        return (((a - b) * c) / d) + e
        # return (((a - b) / c) * d) + e
    elif i == 4:
        return (((a / b) + c) - d) * e
        # return (((a / b) - c) + d) * e
    elif i == 5:
        return (((a + b) * c) / d) - e
        # return (((a + b) / c) * d) - e
    elif i == 6:
        return (((a * b) + c) / d) - e
    elif i == 7:
        return (((a * b) - c) / d) + e
    elif i == 8:
        return (((a + b) * c) - d) / e
    elif i == 9:
        return (((a + b) / c) - d) * e
    elif i == 10:
        return (((a - b) * c) + d) / e
    elif i == 11:
        return (((a - b) / c) + d) * e
    elif i == 12:
        return (((a / b) + c) * d) - e
    elif i == 13:
        return (((a / b) - c) * d) + e


def is_bit_set(n, bit):
    return ((n >> bit) & 1) == 1


def set_bit(n, bit):
    return n | (1 << bit)


def find_all(s=[], digits_used=0, rem_digits=10, rem_groups=5):
    if digits_used == 0b1111111111:
        # base condition
        assert rem_digits == 0
        assert rem_groups == 0
        assert len(s) == 5, s
        process_solution(s)
    else:
        if rem_digits > rem_groups and s:
            # Choose to add a digit to end
            for digit in range(10):
                if not is_bit_set(digits_used, digit):
                    # recurse
                    n_dig_set = set_bit(digits_used, digit)
                    n_s = s[:]
                    last = n_s.pop()
                    n_s.append(last * 10.0 + float(digit))

                    find_all(n_s, n_dig_set, rem_digits - 1, rem_groups)

        # Choose to add append number group
        if rem_groups > 0:
            for digit in range(10):
                if not is_bit_set(digits_used, digit):
                    # recurse
                    n_s = s[:]
                    n_s.append(float(digit))
                    n_dig_set = set_bit(digits_used, digit)
                    find_all(n_s, n_dig_set, rem_digits - 1, rem_groups - 1)


find_all()
