''' This is a helper script to run and test your code. Put this script in the same folder as your solution and sample_grader.cpp. USAGE: python3 testing_tool.py sol.cpp test.txt It compiles and runs sol.cpp alongside sample_grader.cpp. test.txt contains: On the first line, the integer N. On second line, the integer K representing the number of dishes Acid likes. On the third line, K integers representing the dishes Acid likes. On the fourth line, an arbitrary length string containing A's and B's, the order in which the Waiter asks Acid or Base. Do not presume the Waiter has a predetermined order in which it asks Acid or Base for orders. The script then simulates the interaction between the Waiter and Acid, Base and the Chef and prints to stdout their interaction. ''' import os import shutil import subprocess import sys source = sys.argv[1] if len(sys.argv) > 1 else "sol.cpp" test_file = sys.argv[2] if len(sys.argv) > 2 else "test.txt" temp_dir = os.path.join(os.getcwd(), "temp") shutil.rmtree(temp_dir, ignore_errors=True) os.makedirs(temp_dir) binary = os.path.join(temp_dir, "player") compiled = subprocess.run(["g++", "-O2", "-std=c++17", "-I.", "sample_grader.cpp", source, "-o", binary]) if compiled.returncode != 0: sys.exit("Compilation failed") def create_fifo(p, q): p_to_q = os.path.join(temp_dir, f"{p}_to_{q}") q_to_p = os.path.join(temp_dir, f"{q}_to_{p}") os.mkfifo(p_to_q) os.mkfifo(q_to_p) return p_to_q, q_to_p class Person: def __init__(self, name): self.name = name p_to_m, m_to_p = create_fifo(name, "m") self.process = subprocess.Popen([binary, m_to_p, p_to_m]) self.read = open(p_to_m, "r") self.write = open(m_to_p, "w") def send_message(self, text): self.write.write(text + "\n") self.write.flush() def receive_line(self): line = self.read.readline() if line == "": print(f"RE: {self.name} crashed or closed its pipe.") exit(0) return line.strip() def close(self): self.write.close() self.read.close() self.process.wait() test_input = open(test_file, "r") n = int(test_input.readline()) k = int(test_input.readline()) s = list(map(int, test_input.readline().split())) n_minus_s = [i + 1 for i in range(n) if i + 1 not in s] question_order = test_input.readline().strip() test_input.close() acid = Person("Acid") base = Person("Base") chef = Person("Chef") customers = {"A": acid, "B": base} acid.send_message("customer") acid.send_message(f"{n} {k}") acid.send_message(" ".join(map(str, s))) base.send_message("customer") base.send_message(f"{n} {n - k}") base.send_message(" ".join(map(str, n_minus_s))) chef.send_message("chef") chef.send_message(str(n)) verdict = None orders = 0 for turn in question_order: current = customers[turn] other = base if current is acid else acid current.send_message("!") x = int(current.receive_line()) other.send_message(f". {x}") orders += 1 print(f"Waiter gets order {x} from {current.name}") chef.send_message(str(x)) reply = chef.receive_line() print(f"Chef says: \"{reply}\"") if reply == "YEAH, SCIENCE!": acid.send_message('x') base.send_message('x') size = int(chef.receive_line()) dishes = list(map(int, chef.receive_line().split())) print(f"Chef guesses: {dishes}") if(size != len(dishes)): print("WA: What are you smoking, Jesse?") exit(0) if(not (sorted(dishes) == sorted(s) or sorted(dishes) == sorted(n_minus_s))): print("WA: Honestly, Jesse, it is a miracle you even managed to tie your shoes this morning.") exit(0) print("Correct: Well. Miracles do happen.") exit(0) print("WA: Any day now, Jesse. Take your time. It’s not like we’re in a rush or anything.")