From 0efa1a46733c47af44856c4752a945ec43701c40 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Sun, 14 Jun 2026 13:28:25 +0530 Subject: [PATCH] feat: enhance Codeforces URL handling with contest and problem set extraction --- src/platform/codeforces/codeforces.go | 57 ++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/src/platform/codeforces/codeforces.go b/src/platform/codeforces/codeforces.go index 5e275ab..2664c56 100644 --- a/src/platform/codeforces/codeforces.go +++ b/src/platform/codeforces/codeforces.go @@ -10,6 +10,13 @@ import ( const urlFormat string = "https://codeforces.com/problemset/problem/%s/%s" const codeNameDelimiter string = "-" +const ( + ProblemSet = "problemset" + Contest = "contest" +) + +var ErrInvaliidCodeforcesUrl = errors.New("invalid url") + type CodeforcesInstance struct { name types.PlatformName codeName string @@ -25,12 +32,27 @@ func (cf CodeforcesInstance) GetCodeName() string { // This is the main method func GetInstance(url string) (types.Instance, error) { + // For codeforces either the problem could be from + // problem set or contest like below + + url = strings.ToLower(url) + // extract code name from url - // example: https://codeforces.com/problemset/problem/1234/A + // example1 [problemset]: https://codeforces.com/problemset/problem/1234/A + // example2 [contest]: https://codeforces.com/contest/2236/problem/D // if delimiter is "-" - // code name is 1234-A - extractedCodeName, err := extractCodeName(url) + // code name is 1234-A [example1] + // or name is 2236-D [example2] + + var extractedCodeName string = "" + var err error + if strings.Contains(url, ProblemSet) { + extractedCodeName, err = extractCodeNameProblemSet(url) + } else { + extractedCodeName, err = extractCodeNameContest(url) + } + if err != nil { return nil, err } @@ -41,10 +63,35 @@ func GetInstance(url string) (types.Instance, error) { }, nil } -func extractCodeName(url string) (string, error) { +func extractCodeNameContest(url string) (string, error) { + // https://codeforces.com/contest/2236/problem/D + // first %s comes at index 42 + if len("codeforces.com/contest/?/problem/?") > len(url) { + return "", ErrInvaliidCodeforcesUrl + } + + index := strings.Index(url, Contest) + if index < 0 { + return "", ErrInvaliidCodeforcesUrl + } + + index += len(Contest) + len("/") + + // now the index is at contest Id + substring := url[index:] + + split := strings.Split(substring, "/") + + contestCode := string(split[0]) + problem := strings.ToUpper(string(split[2])) + + return contestCode + codeNameDelimiter + problem, nil +} + +func extractCodeNameProblemSet(url string) (string, error) { // first %s comes at index 42 if len(url) < 43 { - return "", errors.New("invalid url") + return "", ErrInvaliidCodeforcesUrl } prefixRemovedString := url[42:]