Le matheux a lunettes de la classe vous regarde avec insistance puis s'approche de vous lentement, avant de vous donner une mission : essayer de remplir les challenges de sa création...
L'objectif ici est de résoudre les 7 problèmes de maths, et les scripter pour rester dans les temps de réponses attendus par le serveur.
import socket import re import numpy as np import json from scipy.integrate import solve_ivp ADRESSE = '127.0.0.1' PORT = 6790 def get_value_at_time(initial, decrement, multiply, time): value = initial for t in range(1, time+1): if value > 0: value -= decrement else: value *= multiply return value def solve_system(coefficients, constants): A = np.array(coefficients) B = np.array(constants) if np.linalg.det(A) == 0: return None solution = np.linalg.solve(A, B) solution = [round(num, 2) for num in solution] return solution def solve_knapstack(list_objects, capacity,dp): for poids, value in list_objects: for j in range(capacity, poids - 1, -1): dp[j] = max(dp[j], dp[j - poids] + value) # On prend le max entre la valeur actuelle et la valeur de l'objet ajouté def maze_to_list(mazetoparse): return [list(line) for line in mazetoparse.splitlines() if line.strip()] def shortestpath(labyrinth, positionjoueur, positionflag): curseurposition = list(positionflag) cpt = 0 entrance_row, entrance_col = positionjoueur hauteur = len(labyrinth) largeur = len(labyrinth[0]) if entrance_row == 0: side = 'n' elif entrance_row == hauteur - 1: side = 's' elif entrance_col == largeur - 1: side = 'e' elif entrance_col == 0: side = 'w' else: side = None if side == 'n' or side == 's': if side == 'n': target_row = 1 while curseurposition[0] > target_row: curseurposition[0] -= 1 cpt += 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' # on remonte du flag vers l'entrée elif side == 's': target_row = hauteur - 2 while curseurposition[0] < target_row: curseurposition[0] += 1 cpt += 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' # ou on redescend while curseurposition[1] != entrance_col: cpt += 1 if curseurposition[1] < entrance_col: curseurposition[1] += 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' elif curseurposition[1] > entrance_col: curseurposition[1] -= 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' else: return cpt elif side == 'e' or side == 'w': if side == 'e': target_col = largeur - 2 while curseurposition[1] < target_col: cpt += 1 curseurposition[1] += 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' # on va a droite si l'entrée est à droite elif side == 'w': target_col = 1 while curseurposition[1] > target_col: cpt += 1 curseurposition[1] -= 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' # sinon à gauche while curseurposition[0] != entrance_row: cpt += 1 if curseurposition[0] < entrance_row: # si le curseur est au dessus il descend curseurposition[0] += 1 labyrinth[curseurposition[0]][curseurposition[1]] = 'p' elif curseurposition[0] > entrance_row: curseurposition[0] -= 1 # sinon il monte labyrinth[curseurposition[0]][curseurposition[1]] = 'p' return cpt def solve_graph(labyrinth): i = 0 j = 0 for i in range(len(labyrinth)): for j in range(len(labyrinth[i])): if labyrinth[i][j] == "e": positionjoueur = (i,j) elif labyrinth[i][j] == "s": positionflag = (i,j) min_distance = shortestpath(labyrinth, positionjoueur, positionflag) return labyrinth,min_distance def main(): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ADRESSE, PORT)) data = client.recv(2048).decode() print(data) question_match = data.split(">>> Convex or Concave ? : ")[-1] if question_match: numbers = re.findall(r"-?\d+", question_match) numbers = [int(num) for num in numbers] if numbers[0] < 0: client.sendall(b"Concave\n") else: client.sendall(b"Convex\n") else: print("No valid question part found.") data = client.recv(2048).decode() print(data) question_match = data.split(">>> Solve the system:\n")[-1] if question_match: numbers = re.findall(r"-?\d+", question_match) numbers = [int(num) for num in numbers] coefficients = [numbers[0:3], numbers[4:7], numbers[8:11]] constants = [numbers[3], numbers[7], numbers[11]] correct_solution = solve_system(coefficients, constants) if correct_solution: rounded_solution = [round(float(num), 2) for num in correct_solution] print(rounded_solution) client.send(json.dumps(rounded_solution).encode()) else: client.send(b"System has no solution\n") data = client.recv(2048).decode() question_match = data.split(">>> Add the following matrices:\n")[-1] if question_match: matrices = re.findall(r"\[\[.*?\]\]", question_match) matrix1 = json.loads(matrices[0]) matrix2 = json.loads(matrices[1]) sum_matrix = np.add(matrix1, matrix2).tolist() client.send(json.dumps(sum_matrix).encode()) else: print("No valid question part found.") # graphalgo data = client.recv(4096).decode() print(data) mazetoparse = data.split("Please send back", 1)[0].strip() mazetoparse2 = mazetoparse.split("Maze>>", 1)[1].strip() mazeparsed = maze_to_list(mazetoparse2) _, min_distancesolved = solve_graph(mazeparsed) client.send(str(min_distancesolved).encode('utf-8')) data = client.recv(2048).decode() data = client.recv(2048).decode() print(data) # knapstack if ">>> List of objects (weight, value )" in data: objects_data = data[99:].split(">>> Capacity of the bag ")[0].strip() list_objects = eval(objects_data) capactiy_cutter = data.split(">>> Capacity of the bag")[1] capactiy_cutter = capactiy_cutter.split("\n")[0] cutted = int(capactiy_cutter) dp = [0] * (cutted + 1) solve_knapstack(list_objects, cutted, dp) client.send(str(dp[cutted]).encode()) # strangecounter data = client.recv(2048).decode() data = client.recv(2048).decode() print(data) lines = data.splitlines() print(lines) initial = int(lines[4].split(" ")[2].replace(',', '')) decrement_line = lines[4] decrement_str = decrement_line.split(" ")[5].replace(',', '') decrement = int(decrement_str) multiply_line = lines[5] # ligne qui parle du "multiply" multiply_str = multiply_line.split(" ")[5].replace(',', '') multiply = int(multiply_str) question_line = lines[6] # ligne qui pose la question client_time_str = question_line.split(" ")[7].replace('?', '') client_time = int(client_time_str) print(initial) print(decrement) print(multiply) print(client_time) correct_value = get_value_at_time(initial, decrement, multiply, client_time) client.send(str(correct_value).encode()) data = client.recv(2048).decode() print(data) # On extrait la ligne de l'ODE for line in data.splitlines(): if "y''" in line: eq_line = line.strip() break else: raise ValueError("Equation line not found") tokens = eq_line.split() a = int(tokens[2].split('*')[0]) b = int(tokens[4].split('*')[0]) c = int(tokens[6]) C0 = int(tokens[10].split('=')[1].rstrip(',')) C1 = int(tokens[11].split('=')[1]) def system(t, Y): y, dy = Y return [dy, -a * dy - b * y - c] sol = solve_ivp( system, [0, 1], [C0, C1], method='RK45', t_eval=[1] ) y1_end = sol.y[0, -1] y2_end = sol.y[1, -1] y1_py = round(float(y1_end), 2) y2_py = round(float(y2_end), 2) solution = [y1_py,y2_py] print("Solution calculée:", solution) client.sendall(str(solution).encode()) data = client.recv(2048).decode() print(data) if __name__ == '__main__': main()