Skip to content

Commit d6ca54c

Browse files
Merge pull request #3 from subdiox/feature/atcoder-submission
Add support for submission to the latest AtCoder contests
2 parents 2a736e7 + f7f0b04 commit d6ca54c

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

Sources/AtCoderLibrary/API/OjApiCommand.swift

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/AtCoderLibrary/API/Response.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ extension OjApiCommand {
99
let result: Problem
1010
}
1111

12+
struct GuessLanguageResponse: Decodable {
13+
let result: Language
14+
}
15+
1216
struct SubmitCodeResponse: Decodable {
1317
let result: Result
1418

@@ -57,3 +61,8 @@ struct Context: Decodable {
5761
let url: URL
5862
}
5963
}
64+
65+
struct Language: Decodable {
66+
let id: String
67+
let description: String
68+
}

Sources/AtCoderLibrary/Command/Submit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public struct Submit: ParsableCommand {
88
abstract: "Submit a your code."
99
)
1010

11-
@Argument(help: "Alphabet of the problem to be submitted.", transform: Character.init)
12-
var task: Character
11+
@Argument(help: "Alphabet of the problem to be submitted.")
12+
var task: String
1313

1414
@Flag(name: .shortAndLong, help: "Run a UnitTest before submitting.")
1515
var runTest: Bool = false

Sources/AtCoderLibrary/Command/Submit/RunTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import SwiftShell
33

44
enum RunTest {
5-
static func sampleCase(task: Character) throws {
5+
static func sampleCase(task: String) throws {
66
try runAndPrint("swift", "test", "--filter", "\(task.uppercased())Tests/testExample")
77
}
88
}

0 commit comments

Comments
 (0)