@@ -13,23 +13,18 @@ enum OjApiCommand {
1313 return ( contest, problems)
1414 }
1515
16- static func submitCode( contestName: String , task: Character , ojApiPath: String ) throws -> URL {
16+ static func submitCode( contestName: String , task: String , ojApiPath: String ) throws -> URL {
1717 try precheck ( path: ojApiPath)
18- let result = run (
19- ojApiPath,
20- " submit-code " ,
21- " --file " ,
22- " Sources/ \( task. uppercased ( ) ) /main.swift " ,
23- " --language " ,
24- " 4055 " ,
25- " https://atcoder.jp/contests/ \( contestName) /tasks/ \( contestName) _ \( task. lowercased ( ) ) "
26- )
27- guard result. succeeded else {
28- throw result. stderror
18+ let contestURL = " https://atcoder.jp/contests/ \( contestName) "
19+ let contest = try getContest ( url: contestURL, ojApiPath: ojApiPath)
20+ guard let problem = contest. problems. first ( where: {
21+ $0. context. alphabet. uppercased ( ) == task. uppercased ( )
22+ } ) else {
23+ throw " The contest name or the task name is invalid. "
2924 }
30- print ( result . stdout )
31- let response = try JSONDecoder ( ) . decode ( SubmitCodeResponse . self , from : result . stdout . data ( using : . utf8 ) ! )
32- return response . result . url
25+ let filePath = " Sources/ \( task . uppercased ( ) ) /main.swift "
26+ let language = try guessLanguage ( url : problem . url , filePath : filePath , ojApiPath : ojApiPath )
27+ return try submitCode ( url : problem . url, filePath : filePath , language : language , ojApiPath : ojApiPath )
3328 }
3429}
3530
@@ -63,4 +58,33 @@ private extension OjApiCommand {
6358 let response = try JSONDecoder ( ) . decode ( GetProblemResponse . self, from: result. stdout. data ( using: . utf8) !)
6459 return response. result
6560 }
61+
62+ static func guessLanguage( url: URL , filePath: String , ojApiPath: String ) throws -> Language {
63+ try precheck ( path: ojApiPath)
64+ let result = run ( ojApiPath, " guess-language-id " , url. absoluteString, " --file= \( filePath) " )
65+ guard result. succeeded else {
66+ throw result. stderror
67+ }
68+ print ( result. stdout)
69+ let response = try JSONDecoder ( ) . decode ( GuessLanguageResponse . self, from: result. stdout. data ( using: . utf8) !)
70+ return response. result
71+ }
72+
73+ static func submitCode( url: URL , filePath: String , language: Language , ojApiPath: String ) throws -> URL {
74+ let result = run (
75+ ojApiPath,
76+ " submit-code " ,
77+ " --file " ,
78+ filePath,
79+ " --language " ,
80+ language. id,
81+ url. absoluteString
82+ )
83+ guard result. succeeded else {
84+ throw result. stderror
85+ }
86+ print ( result. stdout)
87+ let response = try JSONDecoder ( ) . decode ( SubmitCodeResponse . self, from: result. stdout. data ( using: . utf8) !)
88+ return response. result. url
89+ }
6690}
0 commit comments