import random from multiprocessing import Process import os from typing import List all_conds = [] skip = [] filenames = os.listdir("./solves") nums_to_run = [] with open('conds.js') as f: for i,l in enumerate(f.readlines()): all_conds.append(l) if f"solved_sum_{i}.txt" not in filenames and f"unknown_{i}.txt" not in filenames and f"solved_sub_{i}.txt" not in filenames: nums_to_run.append(i) pass else: skip.append(i) def run_thread(cond_nums): for n in (cond_nums): expr = all_conds[n] if n in skip: continue def check_tri_sum(test): if len(test) != 4: return False for i in range(10): a = random.randrange(256) if test[3] - a < 300: b = 20 else: b = random.randrange(256) c = test[3] - a - b inp = [0 for i in range(1950)] inp[test[0]], inp[test[1]] , inp[test[2]] = a, b, c check = eval(expr, globals(), {'inp': inp}) if not check: return False return True i = 0 broken = False def random_weighted_array(): nums = [i for i in range(1950)] out = [0 for _ in range(1950)] for i in range(1950 // 3): j = random.choice(nums) nums.remove(j) out[j] = random.randrange(1, 16) for i in range(1950 - 1950 // 3): j = random.choice(nums) nums.remove(j) out[j] = random.randrange(230, 255) return out def random_normal_array(): return [random.randrange(0, 256) for i in range(1949)] + [431] while True: i += 1 inp = random_normal_array() # inp = random_weighted_array() check = eval(expr, globals(), {'inp': inp}) if(check): break if i > 100000: broken = True break if broken: print("skipping", n) continue results = [] saved_inp = inp[:] for i in range(1950): if(hex(i) not in expr): continue for j in range(2): inp[i] = j check = eval(expr, globals(), {'inp': inp}) inp = saved_inp[:] if not check: results.append(i) break s = sum([saved_inp[results[i]] for i in range(len(results))]) results.append(s) if check_tri_sum(results): results.append("SUM") results.append(n) with open(f"./solves/solved_sum_{n}.txt", 'w') as f: f.write(str(results)) else: results = results[:-1] for j in [saved_inp[results[i]] for i in range(len(results))]: results.append(j) results.append("UNKNOWN") results.append(n) with open(f"./solves/unknown_{n}.txt", 'w') as f: f.write(str(results)) print(results) NUM_THREADS = 16 threads: List[Process] = [] for i in range(NUM_THREADS): threads.append(Process(target=run_thread, args=(nums_to_run[i::NUM_THREADS],))) for t in threads: t.start() for t in threads: t.join()