-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·116 lines (89 loc) · 3.21 KB
/
generate.py
File metadata and controls
executable file
·116 lines (89 loc) · 3.21 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
#!/usr/bin/env python3
"""Generate standalone solution file for Codeforces submission."""
import sys
import os
from pathlib import Path
def read_file(filepath: str) -> str:
"""Read file contents."""
with open(filepath, 'r') as f:
return f.read()
def extract_problem_code(problem_content: str) -> str:
"""Extract only the implementation from problem file."""
lines = problem_content.split('\n')
result: list[str] = []
skip_block_comment = False
for line in lines:
# Skip problem comment and includes already in template
if line.startswith('// Problem:'):
continue
# Skip copyright/license block comment (/* ... */)
stripped = line.strip()
if stripped.startswith('/*'):
skip_block_comment = True
if skip_block_comment:
if '*/' in line:
skip_block_comment = False
continue
result.append(line)
return '\n'.join(result).strip()
def generate_solution(template_content: str, problem_code: str) -> str:
"""Combine template and problem code, removing LOCAL blocks."""
lines = template_content.split('\n')
result: list[str] = []
skip_local = False
skip_problem_include = False
for line in lines:
# Handle LOCAL blocks
if '#ifdef LOCAL' in line:
skip_local = True
continue
if skip_local and '#endif' in line:
skip_local = False
continue
if skip_local:
continue
# Handle PROBLEM_FILE include section
if '#ifdef PROBLEM_FILE' in line:
skip_problem_include = True
# Insert problem code here instead
result.append('\n' + problem_code)
continue
if skip_problem_include and '#endif' in line:
skip_problem_include = False
continue
if skip_problem_include:
continue
result.append(line)
return '\n'.join(result)
def main() -> None:
"""Main entry point."""
if len(sys.argv) != 2:
print("Usage: python3 generate.py <problem_file.cpp>")
print("Example: python3 generate.py 1903A.cpp")
sys.exit(1)
problem_file = sys.argv[1]
# Validate files exist
if not os.path.exists(problem_file):
print(f"Error: Problem file '{problem_file}' not found")
sys.exit(1)
if not os.path.exists('template.cpp'):
print("Error: template.cpp not found")
sys.exit(1)
# Read files
template_content = read_file('template.cpp')
problem_content = read_file(problem_file)
# Extract problem code
problem_code = extract_problem_code(problem_content)
# Generate combined solution
solution = generate_solution(template_content, problem_code)
# Create solutions directory
solutions_dir = Path('solutions')
_ = solutions_dir.mkdir(exist_ok=True)
# Write output file
problem_name = Path(problem_file).stem
output_file = solutions_dir / f"{problem_name}.cpp"
with open(output_file, 'w') as f:
_ = f.write(solution)
print(f"✓ Generated: {output_file}")
if __name__ == '__main__':
main()