add USAMO 2025
Browse files
USAMO/download_script/download.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -----------------------------------------------------------------------------
|
| 2 |
+
# Author: Jiawei Liu
|
| 3 |
+
# Date: 2025-10-29
|
| 4 |
+
# -----------------------------------------------------------------------------
|
| 5 |
+
'''
|
| 6 |
+
Download script for APMO
|
| 7 |
+
To run:
|
| 8 |
+
`python USAMO/download_script/download.py`
|
| 9 |
+
'''
|
| 10 |
+
|
| 11 |
+
import re
|
| 12 |
+
import requests
|
| 13 |
+
from bs4 import BeautifulSoup
|
| 14 |
+
from tqdm import tqdm
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
from requests.adapters import HTTPAdapter
|
| 17 |
+
from urllib3.util.retry import Retry
|
| 18 |
+
from urllib.parse import urljoin
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def build_session(
|
| 22 |
+
max_retries: int = 3,
|
| 23 |
+
backoff_factor: int = 2,
|
| 24 |
+
session: requests.Session = None
|
| 25 |
+
) -> requests.Session:
|
| 26 |
+
"""
|
| 27 |
+
Build a requests session with retries
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
max_retries (int, optional): Number of retries. Defaults to 3.
|
| 31 |
+
backoff_factor (int, optional): Backoff factor. Defaults to 2.
|
| 32 |
+
session (requests.Session, optional): Session object. Defaults to None.
|
| 33 |
+
"""
|
| 34 |
+
session = session or requests.Session()
|
| 35 |
+
adapter = HTTPAdapter(max_retries=Retry(total=max_retries, backoff_factor=backoff_factor))
|
| 36 |
+
session.mount("http://", adapter)
|
| 37 |
+
session.mount("https://", adapter)
|
| 38 |
+
session.headers.update({
|
| 39 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
|
| 40 |
+
})
|
| 41 |
+
|
| 42 |
+
return session
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
base_url = "https://web.evanchen.cc/problems.html"
|
| 47 |
+
req_session = build_session()
|
| 48 |
+
|
| 49 |
+
output_dir = Path(__file__).parent.parent / "raw"
|
| 50 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 51 |
+
|
| 52 |
+
resp = req_session.get(base_url)
|
| 53 |
+
soup = BeautifulSoup(resp.text, 'html.parser')
|
| 54 |
+
|
| 55 |
+
imo_exams = soup.find_all("a", href=lambda h: h and re.search(r"USAMO\-\d{4}\-notes.pdf$", h))
|
| 56 |
+
imo_pdf_links = [urljoin(base_url, link["href"]) for link in imo_exams]
|
| 57 |
+
|
| 58 |
+
for link in tqdm(imo_pdf_links):
|
| 59 |
+
output_file = output_dir / f"en-{Path(link).name}"
|
| 60 |
+
|
| 61 |
+
# Check if the file already exists
|
| 62 |
+
if output_file.exists():
|
| 63 |
+
continue
|
| 64 |
+
|
| 65 |
+
pdf_resp = req_session.get(link)
|
| 66 |
+
|
| 67 |
+
if pdf_resp.status_code != 200:
|
| 68 |
+
print(f"Failed to download {link}")
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
output_file.write_bytes(pdf_resp.content)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|
USAMO/md/en-USAMO-2025-notes.md
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
USAMO/raw/en-USAMO-2025-notes.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2cef35876f2d5ed088544a76d42656559c4c2759caa711337016bf6075a13cd5
|
| 3 |
+
size 345456
|
USAMO/segment_script/segment_2025.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -----------------------------------------------------------------------------
|
| 2 |
+
# Author: Jiawei Liu
|
| 3 |
+
# Date: 2025-10-29
|
| 4 |
+
# -----------------------------------------------------------------------------
|
| 5 |
+
import json
|
| 6 |
+
import re
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
from rapidfuzz import fuzz
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def clean_text(text: str):
|
| 13 |
+
text = re.sub(
|
| 14 |
+
r"\nAvailable online at.+?\.\s\s$", "", text, flags=re.DOTALL | re.MULTILINE
|
| 15 |
+
)
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def extract_problems(markdown_text: str):
|
| 20 |
+
problems = {}
|
| 21 |
+
|
| 22 |
+
# 1. Find the block of text containing the problems
|
| 23 |
+
# Block starts with '## Problems'
|
| 24 |
+
# and ends before '## \(S 1\) Solutions to Day 1'
|
| 25 |
+
problems_section = re.search(
|
| 26 |
+
r"^## Problems\s*$(.*?)^##.*Solutions to Day 1",
|
| 27 |
+
markdown_text,
|
| 28 |
+
re.DOTALL | re.MULTILINE,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if not problems_section:
|
| 32 |
+
print(" - Not found '## Problems' section.")
|
| 33 |
+
return problems
|
| 34 |
+
|
| 35 |
+
problems_block = problems_section.group(1)
|
| 36 |
+
|
| 37 |
+
# 2. Split the block into individual problems
|
| 38 |
+
# Each problem starts with a number followed by a dot and space (e.g., '1. ')
|
| 39 |
+
matchs = list(re.finditer(r"^(\d+)\.\s+", problems_block, flags=re.MULTILINE))
|
| 40 |
+
|
| 41 |
+
# Split the text into parts using the problem numbers as delimiters
|
| 42 |
+
for i, m in enumerate(matchs):
|
| 43 |
+
problem_label = m.group(1)
|
| 44 |
+
problem = problems_block[
|
| 45 |
+
m.end() : matchs[i + 1].start()
|
| 46 |
+
if i + 1 < len(matchs)
|
| 47 |
+
else len(problems_block)
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
problems[problem_label] = (problem.strip(), m.group())
|
| 51 |
+
|
| 52 |
+
print(f" - Extracted {len(problems)} problems.")
|
| 53 |
+
|
| 54 |
+
return problems
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def extract_solutions(markdown_text):
|
| 58 |
+
solutions = {}
|
| 59 |
+
|
| 60 |
+
# 1. Find the block of text containing the solutions
|
| 61 |
+
# Block starts with '## \(S 1\) Solutions to Day 1' and end of document
|
| 62 |
+
solutions_section = re.search(
|
| 63 |
+
r"^##.*?Solutions to Day 1\s*$(.*)",
|
| 64 |
+
markdown_text,
|
| 65 |
+
re.DOTALL | re.MULTILINE,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if not solutions_section:
|
| 69 |
+
print(" - Not found Solutions section.")
|
| 70 |
+
return solutions
|
| 71 |
+
|
| 72 |
+
solutions_block = solutions_section.group(1)
|
| 73 |
+
|
| 74 |
+
## \(\S 1.1\) USAMO 2025/1, proposed by John Berman
|
| 75 |
+
# 2. Split the block into individual solutions
|
| 76 |
+
matchs = list(
|
| 77 |
+
re.finditer(r"^##.*?USAMO\s*?\d{4}\/(\d+).*?\n$", solutions_block, flags=re.MULTILINE)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Split the text into parts using the solution numbers as delimiters
|
| 81 |
+
for i, m in enumerate(matchs):
|
| 82 |
+
problem_label = m.group(1)
|
| 83 |
+
solution = solutions_block[
|
| 84 |
+
m.end() : matchs[i + 1].start()
|
| 85 |
+
if i + 1 < len(matchs)
|
| 86 |
+
else len(solutions_block)
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
solutions[problem_label] = (solution.strip(), m.group())
|
| 90 |
+
|
| 91 |
+
print(f" - Extracted {len(solutions)} solutions.")
|
| 92 |
+
|
| 93 |
+
return solutions
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def join(problems: dict, solutions: dict):
|
| 97 |
+
pairs = []
|
| 98 |
+
|
| 99 |
+
for problem_label, (problem, p_match) in problems.items():
|
| 100 |
+
solution, s_match = solutions.get(problem_label)
|
| 101 |
+
|
| 102 |
+
# Clean solution by removing the part that overlaps with the problem statement
|
| 103 |
+
problem_align = fuzz.partial_ratio_alignment(solution, problem)
|
| 104 |
+
solution = solution.replace(
|
| 105 |
+
solution[problem_align.src_start : problem_align.src_end], ""
|
| 106 |
+
)
|
| 107 |
+
solution = re.sub(
|
| 108 |
+
r"^\s*## Problem statement", "", solution, flags=re.IGNORECASE
|
| 109 |
+
).strip()
|
| 110 |
+
|
| 111 |
+
if not solution:
|
| 112 |
+
print(f" - Warning: No solution found for problem {problem_label}.")
|
| 113 |
+
pairs.append((problem, solution, problem_label, p_match, s_match))
|
| 114 |
+
|
| 115 |
+
return pairs
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def write_pairs(output_file: Path, pairs: list, year: str, project_root: Path):
|
| 119 |
+
output_jsonl_text = ""
|
| 120 |
+
for problem, solution, problem_label, p_match, s_match in pairs:
|
| 121 |
+
output_jsonl_text += (
|
| 122 |
+
json.dumps(
|
| 123 |
+
{
|
| 124 |
+
"year": year,
|
| 125 |
+
"tier": "T1",
|
| 126 |
+
"problem_label": problem_label,
|
| 127 |
+
"problem_type": None,
|
| 128 |
+
"exam": "USAMO",
|
| 129 |
+
"problem": problem,
|
| 130 |
+
"solution": solution,
|
| 131 |
+
"metadata": {
|
| 132 |
+
"resource_path": output_file.relative_to(
|
| 133 |
+
project_root
|
| 134 |
+
).as_posix(),
|
| 135 |
+
"problem_match": p_match,
|
| 136 |
+
"solution_match": s_match,
|
| 137 |
+
},
|
| 138 |
+
},
|
| 139 |
+
ensure_ascii=False,
|
| 140 |
+
)
|
| 141 |
+
+ "\n"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
output_file.write_text(output_jsonl_text, encoding="utf-8")
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
compet_base_path = Path(__file__).resolve().parent.parent
|
| 149 |
+
compet_md_path = compet_base_path / "md"
|
| 150 |
+
seg_output_path = compet_base_path / "segmented"
|
| 151 |
+
project_root = compet_base_path.parent
|
| 152 |
+
|
| 153 |
+
num_problems = 0
|
| 154 |
+
num_solutions = 0
|
| 155 |
+
|
| 156 |
+
md_files = [
|
| 157 |
+
_
|
| 158 |
+
for _ in compet_md_path.glob("**/*.md")
|
| 159 |
+
if int(re.search(r"\d{4}", _.as_posix()).group()) in [2025]
|
| 160 |
+
]
|
| 161 |
+
for md_file in md_files:
|
| 162 |
+
print(f"Processing {md_file}...")
|
| 163 |
+
output_file = seg_output_path / md_file.relative_to(compet_md_path).with_suffix(
|
| 164 |
+
".jsonl"
|
| 165 |
+
)
|
| 166 |
+
output_file.parent.mkdir(parents=True, exist_ok=True)
|
| 167 |
+
|
| 168 |
+
# Read the markdown file
|
| 169 |
+
markdown_text = "\n" + md_file.read_text(encoding="utf-8")
|
| 170 |
+
markdown_text = clean_text(markdown_text)
|
| 171 |
+
|
| 172 |
+
problems = extract_problems(markdown_text)
|
| 173 |
+
solutions = extract_solutions(markdown_text)
|
| 174 |
+
|
| 175 |
+
num_problems += len(problems)
|
| 176 |
+
num_solutions += len(solutions)
|
| 177 |
+
|
| 178 |
+
pairs = join(problems, solutions)
|
| 179 |
+
|
| 180 |
+
year = re.search(r"\d{4}", output_file.stem).group()
|
| 181 |
+
|
| 182 |
+
write_pairs(output_file, pairs, year, project_root)
|
| 183 |
+
|
| 184 |
+
print()
|
| 185 |
+
|
| 186 |
+
print(f"Total problems extracted: {num_problems}")
|
| 187 |
+
print(f"Total solutions extracted: {num_solutions}")
|
USAMO/segmented/en-USAMO-2025-notes.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|