Skip to content

Commit 121a4f4

Browse files
committed
feat(scripts): add solution scaffolding script
1 parent ffc94a9 commit 121a4f4

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

create_solution.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import shutil
5+
import subprocess
6+
7+
def main():
8+
if len(sys.argv) != 3:
9+
print("Usage: python create_solution.py <platform> <problem_id>")
10+
print("Example: python create_solution.py uva 12166")
11+
print("platform: uva or codeforces")
12+
sys.exit(1)
13+
14+
platform = sys.argv[1]
15+
problem_id = sys.argv[2].strip()
16+
17+
if platform not in ['uva', 'codeforces']:
18+
print("Invalid platform. Use 'uva' or 'codeforces'.")
19+
sys.exit(1)
20+
21+
# Assume running from project root
22+
base_dir = os.getcwd()
23+
src_java = os.path.join(base_dir, 'src', 'main', 'java', 'com', 'lzw', 'solutions', 'sample', 'pjava_sample_buf', 'Main.java')
24+
25+
if not os.path.exists(src_java):
26+
print(f"Template not found: {src_java}")
27+
sys.exit(1)
28+
29+
target_java_dir = os.path.join(base_dir, 'src', 'main', 'java', 'com', 'lzw', 'solutions', platform, f'p{problem_id}')
30+
target_java = os.path.join(target_java_dir, 'Main.java')
31+
32+
os.makedirs(target_java_dir, exist_ok=True)
33+
34+
# Copy and update package
35+
shutil.copy2(src_java, target_java)
36+
37+
with open(target_java, 'r') as f:
38+
content = f.read()
39+
40+
old_package = 'com.lzw.solutions.sample.pjava_sample_buf'
41+
new_package = f'com.lzw.solutions.{platform}.p{problem_id}'
42+
content = content.replace(old_package, new_package)
43+
44+
with open(target_java, 'w') as f:
45+
f.write(content)
46+
47+
# Resources dir for 1.in
48+
resources_dir = os.path.join(base_dir, 'src', 'main', 'resources', platform, f'p{problem_id}')
49+
input_file = os.path.join(resources_dir, '1.in')
50+
os.makedirs(resources_dir, exist_ok=True)
51+
52+
# Read from macOS clipboard (pbpaste)
53+
try:
54+
clipboard_content = subprocess.check_output(['pbpaste']).decode('utf-8').strip()
55+
if clipboard_content:
56+
with open(input_file, 'w') as f:
57+
f.write(clipboard_content)
58+
print(f"✅ Created {target_java}")
59+
print(f"✅ Created {input_file} from clipboard")
60+
else:
61+
print(f"⚠️ Created {target_java}, but clipboard empty - add 1.in manually")
62+
except subprocess.CalledProcessError:
63+
print(f"❌ Failed to read clipboard. Created {target_java}, add {input_file} manually.")
64+
except Exception as e:
65+
print(f"❌ Error with clipboard: {e}. Created {target_java}, add {input_file} manually.")
66+
67+
if __name__ == '__main__':
68+
main()

0 commit comments

Comments
 (0)