@@ -22,6 +22,46 @@ const safeReadFile = (path: string): string | null => {
2222 }
2323} ;
2424
25+ const extractReferenceSolution = ( pythonCode : string ) : string | null => {
26+ if ( ! pythonCode ) return null ;
27+
28+ const lines = pythonCode . split ( "\n" ) ;
29+ let inMethod = false ;
30+ let methodLines : string [ ] = [ ] ;
31+ let methodIndent = 0 ;
32+
33+ for ( let i = 0 ; i < lines . length ; i ++ ) {
34+ const line = lines [ i ] ;
35+
36+ if ( ! inMethod && line . trim ( ) . startsWith ( "def reference_solution(" ) ) {
37+ inMethod = true ;
38+ methodIndent = line . search ( / \S / ) ; // Get the indentation level
39+ methodLines . push ( line ) ;
40+ continue ;
41+ }
42+
43+ if ( inMethod ) {
44+ const currentIndent = line . search ( / \S / ) ;
45+ const isEmpty = line . trim ( ) === "" ;
46+
47+ if ( ! isEmpty && currentIndent <= methodIndent && currentIndent >= 0 ) {
48+ break ;
49+ }
50+
51+ methodLines . push ( line ) ;
52+ }
53+ }
54+
55+ if ( methodLines . length === 0 ) return null ;
56+
57+ const dedentedLines = methodLines . map ( ( line ) => {
58+ if ( line . trim ( ) === "" ) return line ;
59+ return line . slice ( methodIndent ) ;
60+ } ) ;
61+
62+ return dedentedLines . join ( "\n" ) ;
63+ } ;
64+
2565async function main ( ) {
2666 const problemsDir = getProblemsDir ( ) ;
2767 const problemSlugs = readdirSync ( problemsDir ) . filter (
@@ -51,6 +91,9 @@ async function main() {
5191 }
5292
5393 const definition = safeReadFile ( getDefinitionPath ( slug ) ) ;
94+ const referenceSolution = definition
95+ ? extractReferenceSolution ( definition )
96+ : null ;
5497
5598 // Upsert problem in database
5699 const problem = await prisma . problem . upsert ( {
@@ -61,6 +104,7 @@ async function main() {
61104 difficulty : frontmatter . difficulty ,
62105 author : frontmatter . author ,
63106 definition : definition ,
107+ referenceSolution : referenceSolution ,
64108 parameters : frontmatter . parameters ,
65109 tags : frontmatter . tags ,
66110 } ,
@@ -71,6 +115,7 @@ async function main() {
71115 difficulty : frontmatter . difficulty ,
72116 author : frontmatter . author ,
73117 definition : definition ,
118+ referenceSolution : referenceSolution ,
74119 parameters : frontmatter . parameters ,
75120 tags : frontmatter . tags ,
76121 } ,
@@ -81,6 +126,7 @@ async function main() {
81126 console . log ( ` - Difficulty: ${ frontmatter . difficulty ? "✓" : "✗" } ` ) ;
82127 console . log ( ` - Parameters: ${ frontmatter . parameters ? "✓" : "✗" } ` ) ;
83128 console . log ( ` - Definition: ${ definition ? "✓" : "✗" } ` ) ;
129+ console . log ( ` - Reference Solution: ${ referenceSolution ? "✓" : "✗" } ` ) ;
84130 console . log ( ` - Tags: ${ frontmatter . tags ? "✓" : "✗" } ` ) ;
85131 }
86132}
0 commit comments