-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_solve_resolve_02.py
More file actions
381 lines (338 loc) · 13.1 KB
/
Copy path_solve_resolve_02.py
File metadata and controls
381 lines (338 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
"""Brute-force solve resolve_02.json against FIPI API.
For each task in resolve_02.json:
- Generate candidate answers based on answer_type and existing agent hints
- POST to FIPI solve.php to find the one returning '3'
- Save with the verified correct answer
"""
import json
import itertools
import pathlib
import re
import time
import sys
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Optional
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
PROJ = "B24AFED7DE6AB5BC461219556CCA4F9B"
BASE = "https://oge.fipi.ru"
ROOT = pathlib.Path('/Users/nnn/oge 16may')
RESOLVE_DIR = ROOT / 'fipi_raw' / 'batches' / 'resolve'
IN_FILE = RESOLVE_DIR / 'resolve_02.json'
OUT_FILE = RESOLVE_DIR / 'resolve_02_verified.json'
# Reuse known data
WRONG_FILE = ROOT / 'fipi_raw' / 'wrong_answers.json'
ALL_TASKS_FILE = ROOT / 'fipi_raw' / 'all_tasks.json'
def make_session():
s = requests.Session()
s.headers.update({
"User-Agent": "Mozilla/5.0",
"Referer": f"{BASE}/bank/index.php?proj={PROJ}",
})
s.verify = False
s.get(f"{BASE}/bank/index.php?proj={PROJ}", timeout=15)
return s
def post_one(session, guid, answer, retries=2):
for _ in range(retries + 1):
try:
r = session.post(
f"{BASE}/bank/solve.php",
data={"guid": guid, "answer": answer, "ajax": "1", "proj": PROJ},
headers={"X-Requested-With": "XMLHttpRequest"},
timeout=15,
)
return r.text.strip()
except Exception:
time.sleep(0.3)
return ""
def brute_selectone(session, guid):
"""Brute SelectOne: try 1,2,3,4 (maybe 5)."""
for d in '1234':
r = post_one(session, guid, d)
if r in ('1', '3'):
return d
time.sleep(0.15)
# try 5
r = post_one(session, guid, '5')
if r in ('1', '3'):
return '5'
return None
def brute_selectn(session, guid, n_opts=5, pick=2):
"""SelectN: pick 2 of 5 typically. Bitmask format."""
found = []
for combo in itertools.combinations(range(1, n_opts + 1), pick):
mask = ['0'] * n_opts
for d in combo:
mask[d - 1] = '1'
bitmask = ''.join(mask)
r = post_one(session, guid, bitmask)
if r in ('1', '3'):
# Return as digit string (e.g. '13')
return ''.join(str(d) for d in combo)
time.sleep(0.15)
return None
def brute_accord(session, guid, n_choices=5, n_positions=3):
"""Accord: typically 3 positions × 5 choices → 125 combos.
Format on FIPI: '123' (or '1234')."""
# Try permutations first (positions usually all distinct)
# But could be repeats - try all combinations with possible repeats
# Actually for OGE-physics matching, choices can repeat or not. Try permutations first (no repeats), then with repeats.
# First: permutations (most common)
for perm in itertools.permutations(range(1, n_choices + 1), n_positions):
ans = ''.join(str(d) for d in perm)
r = post_one(session, guid, ans)
if r in ('1', '3'):
return ans
time.sleep(0.1)
# Then: with repeats
for combo in itertools.product(range(1, n_choices + 1), repeat=n_positions):
ans = ''.join(str(d) for d in combo)
# Skip ones we tried (no-repeat permutations)
if len(set(combo)) == n_positions:
continue
r = post_one(session, guid, ans)
if r in ('1', '3'):
return ans
time.sleep(0.1)
return None
def brute_txtpc(session, guid, n_choices=5, n_positions=3):
"""TxtPartChoice — similar to accord."""
return brute_accord(session, guid, n_choices, n_positions)
def extract_first_num(s):
"""Extract first number from string."""
m = re.search(r"-?\d+(?:[.,]\d+)?", s or "")
return m.group(0) if m else None
def short_candidates_from_prev(prev_raw, agent_raw):
"""For Краткий ответ: derive numeric candidates."""
cands = []
for src in [agent_raw, prev_raw]:
if not src:
continue
n = extract_first_num(src)
if n:
for v in {n, n.replace(",", "."), n.replace(".", ","), n.lstrip("-")}:
if v and v not in cands:
cands.append(v)
return cands
def try_short(session, guid, agent_raw, prev_raw):
"""Try original short answer + nearby variations."""
cands = short_candidates_from_prev(prev_raw, agent_raw)
for c in cands:
r = post_one(session, guid, c)
if r in ('1', '3'):
return c
time.sleep(0.1)
return None
# Numeric scan for shorts when we have no clue
def numeric_scan(session, guid, hint_value=None):
"""Try common physics answers (integer or 1-decimal) around hint."""
tried = set()
# If hint, try ± nearby
candidates = []
if hint_value is not None:
try:
base = float(str(hint_value).replace(',', '.'))
for delta in [0, 1, -1, 2, -2, 5, -5, 10, -10, 0.5, -0.5, 0.1, -0.1]:
v = base + delta
# Integer if base is integer-like
if v.is_integer():
candidates.append(str(int(v)))
else:
candidates.append(f"{v:.1f}".replace('.', ','))
candidates.append(f"{v:.1f}")
except (ValueError, TypeError):
pass
# Common round answers
for n in [0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 25, 30, 40, 50, 60, 75, 80, 90, 100, 120, 150, 180, 200, 240, 250, 300, 360, 400, 500, 600, 720, 750, 800, 900, 1000, 1200, 1500, 2000, 2500, 3000, 5000, 10000]:
if str(n) not in tried:
candidates.append(str(n))
for c in candidates:
if c in tried:
continue
tried.add(c)
r = post_one(session, guid, c)
if r in ('1', '3'):
return c
time.sleep(0.08)
return None
def solve_task(task, wrong_info, session=None):
"""Solve one task; returns (correct_answer or None, info)."""
if session is None:
session = make_session()
guid = task['guid']
answer_type = task['answer_type']
sid = task['short_id']
agent_raw = wrong_info.get('agent_answer_raw', '') if wrong_info else ''
prev_raw = task.get('previous_agent_answer', '')
if answer_type == "Выбор ответа из предложенных вариантов":
# SelectOne, try original first
first_digit = None
for src in (agent_raw, prev_raw):
m = re.search(r"[1-5]", src or "")
if m:
first_digit = m.group(0)
break
if first_digit:
r = post_one(session, guid, first_digit)
if r in ('1', '3'):
return first_digit, 'original_correct'
# Try all
for d in '1234':
r = post_one(session, guid, d)
if r in ('1', '3'):
return d, 'brute'
time.sleep(0.12)
return None, 'failed_selectone'
elif answer_type == "Выбор ответов из предложенных вариантов":
# SelectN — typically pick 2 from 5
# Try previous agent's digits if available
agent_digits = sorted({d for d in re.findall(r'[1-5]', agent_raw or '')})
if len(agent_digits) == 2:
mask = ['0'] * 5
for d in agent_digits:
mask[int(d) - 1] = '1'
bitmask = ''.join(mask)
r = post_one(session, guid, bitmask)
if r in ('1', '3'):
return ''.join(agent_digits), 'original_correct'
# Brute force all C(5,2) combos
for combo in itertools.combinations('12345', 2):
mask = ['0'] * 5
for d in combo:
mask[int(d) - 1] = '1'
bitmask = ''.join(mask)
r = post_one(session, guid, bitmask)
if r in ('1', '3'):
return ''.join(combo), 'brute'
time.sleep(0.12)
return None, 'failed_selectn'
elif answer_type == "Краткий ответ":
# Short — try agent's number, then nearby
cands = []
for src in (agent_raw, prev_raw):
if src:
n = extract_first_num(src)
if n:
cands.extend([n, n.replace(',', '.'), n.replace('.', ',')])
# dedup
seen = set()
cands = [c for c in cands if not (c in seen or seen.add(c))]
for c in cands:
r = post_one(session, guid, c)
if r in ('1', '3'):
return c, 'original_correct'
time.sleep(0.1)
# No clean candidates - try numeric scan based on hint
hint = cands[0] if cands else None
result = numeric_scan(session, guid, hint)
if result:
return result, 'numeric_scan'
return None, 'failed_short'
elif answer_type == "Установление соответствия":
# Try agent's accord first
agent_digits = re.sub(r'\D', '', agent_raw or '')
if 2 <= len(agent_digits) <= 4:
r = post_one(session, guid, agent_digits)
if r in ('1', '3'):
return agent_digits, 'original_correct'
# Brute force permutations
# Determine n_positions from agent answer or default 3
n_pos = len(agent_digits) if 2 <= len(agent_digits) <= 4 else 3
for perm in itertools.permutations(range(1, 6), n_pos):
ans = ''.join(str(d) for d in perm)
if ans == agent_digits:
continue
r = post_one(session, guid, ans)
if r in ('1', '3'):
return ans, 'brute_accord_perm'
time.sleep(0.08)
# Also try size-2 if size-3 failed
if n_pos == 3:
for perm in itertools.permutations(range(1, 6), 2):
ans = ''.join(str(d) for d in perm)
r = post_one(session, guid, ans)
if r in ('1', '3'):
return ans, 'brute_accord_perm2'
time.sleep(0.08)
return None, 'failed_accord'
elif answer_type == "Расстановка терминов":
# Similar to accord
agent_digits = re.sub(r'\D', '', agent_raw or '')
if 2 <= len(agent_digits) <= 4:
r = post_one(session, guid, agent_digits)
if r in ('1', '3'):
return agent_digits, 'original_correct'
n_pos = len(agent_digits) if 2 <= len(agent_digits) <= 4 else 3
for perm in itertools.permutations(range(1, 6), n_pos):
ans = ''.join(str(d) for d in perm)
if ans == agent_digits:
continue
r = post_one(session, guid, ans)
if r in ('1', '3'):
return ans, 'brute_txtpc'
time.sleep(0.08)
return None, 'failed_txtpc'
return None, 'unknown_type'
def main():
with open(IN_FILE) as f:
batch = json.load(f)
with open(WRONG_FILE) as f:
wrong = json.load(f)
wrong_by_id = {w['short_id']: w for w in wrong}
# Allow resume
done_map = {}
if OUT_FILE.exists():
with open(OUT_FILE) as f:
existing = json.load(f)
done_map = {x['short_id']: x for x in existing}
print(f'Total tasks: {len(batch)}, already done: {len(done_map)}')
# Process in parallel with 6 threads (each has its own session)
lock = threading.Lock()
results = list(done_map.values())
def worker(task):
sid = task['short_id']
if sid in done_map:
return None
info = wrong_by_id.get(sid, {})
sess = make_session()
try:
ans, status = solve_task(task, info, sess)
except Exception as e:
ans, status = None, f'exception:{e}'
return {
'short_id': sid,
'guid': task['guid'],
'answer_type': task['answer_type'],
'subtopic_kes': task.get('subtopic_kes'),
'subtopic_name': task.get('subtopic_name'),
'agent_answer': info.get('agent_answer_raw', ''),
'verified_answer': ans,
'status': status,
}
pending = [t for t in batch if t['short_id'] not in done_map]
print(f'Pending: {len(pending)}')
with ThreadPoolExecutor(max_workers=6) as ex:
futs = {ex.submit(worker, t): t for t in pending}
i = 0
for fut in as_completed(futs):
res = fut.result()
if res is None:
continue
i += 1
with lock:
results.append(res)
# Save every 5
if i % 5 == 0:
with open(OUT_FILE, 'w') as f:
json.dump(results, f, ensure_ascii=False, indent=2)
print(f'[{i}] {res["short_id"]} ({res["answer_type"][:20]}): {res["verified_answer"]!r} ({res["status"]})')
with open(OUT_FILE, 'w') as f:
json.dump(results, f, ensure_ascii=False, indent=2)
# Stats
ok = sum(1 for r in results if r['verified_answer'])
print(f'\nDone. Total={len(results)}, verified={ok}, failed={len(results)-ok}')
if __name__ == '__main__':
main()