プログラミング雑記帳

VSCode + WSL + online-judge-tools で競プロ環境構築

自分用のメモです。 とりあえずAtCoder用です。

WSLでg++が使えるようになっていることを前提としています。

ディレクトリ構成

とりあえず以下のような構成でファイルを作成します。

contest
├── .vscode
│   └── tasks.json
├── Pipfile
├── Pipfile.lock
└── atcoder
    ├── abc
    │   ├── abc001
    │   ├── abc002
    │   ・・・
    │   ├── abc236
    │   │   ├── a.cpp
    │   │   ├── b.cpp
    │   │   ├── c.cpp
    │   │   ├── d.cpp
    │   │   ├── e.cpp
    │   │   ├── f.cpp
    │   │   ├── g.cpp
    │   │   └── h.cpp
    │   ・・・
    │
    ├── agc
    ├── arc
    ├── dltest.sh
    └── othercontests

online-judge-tools のインストールと設定

pipenv を使ってインストールするので、そうでない人は適宜読み替えてください。

pipenv install online-judge-tools

インストールが終わったらAtCoderにログインします。 ユーザー名とパスワードが求められるので入れます。

pipenv run oj login https://atcoder.jp/

ログインできない場合(2022/03/20 追記)

うまくいかず止まることがあります。 そのときは、Seleniumをインストールするとうまくいくかもしれません。

pipenv install selenium
pipenv run oj login https://atcoder.jp/

サンプルケース取得用のシェルスクリプト作成

dltest.sh というファイル名で適当な場所(contest/atcoder/dltest.sh など)に作成します。

#!/bin/bash

contest_dir=$1
contest_id=${contest_dir##*/}
problem_id=$2
test_dir=${contest_dir}/test/${contest_id}_${problem_id}


if [ ! -e ${test_dir} ]; then
    pipenv run oj d -d ${test_dir} https://atcoder.jp/contests/${contest_id}/tasks/${contest_id}_${problem_id}
fi

作成したら、パーミッションがきちんと設定されているかを確認します。

tasks.json の設定

.vscode ディレクトリに tasks.json を作成して以下のように書きます。 pipenvoj をインストールしていない場合は commandargs の部分を自分の環境に合うように書き換えてください(いちいち pipenv run をせずに済む方法をご存知の方がいたらコメントください)。

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "build",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-O2",
                "-pedantic-errors",
                "-Wall",
                "-D_GLIBCXX_DEBUG",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/a.out"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /usr/bin/g++",
            "presentation": {
                "focus": true
            }
        },
        {
            "type": "shell",
            "label": "download test cases",
            "command": "${workspaceFolder}/atcoder/dltest.sh",
            "args": [
                "${fileDirname}",
                "${fileBasenameNoExtension}"
            ]
        },
        {
            "type": "shell",
            "label": "do oj test",
            "command": "pipenv",
            "args": [
                "run",
                "oj",
                "t",
                "-c",
                "${fileDirname}/a.out",
                "-d",
                "${fileDirname}/test/${fileDirnameBasename}_${fileBasenameNoExtension}"
            ],
            "dependsOn": [
                "build",
                "download test cases"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "presentation": {
                "focus": true
            }
        },
        {
            "type": "shell",
            "label": "submit",
            "command": "pipenv",
            "args": [
                "run",
                "oj",
                "s",
                "-y",
                "https://atcoder.jp/contests/${fileDirnameBasename}/tasks/${fileDirnameBasename}_${fileBasenameNoExtension}",
                "${file}"
            ]
        }
    ]
}

ショートカットキーの設定

デフォルトで Ctrl+Shift+B でbuildはできるようになっていますが、 testにはショートカットが設定されていません。 Ctrl+Shift+T を割り当てることにしますが、 View: Reopen Closed EditorCtrl+Shift+T が既に割り当てられているので、キーバインドを削除してから workbench.action.tasks.testCtrl+Shift+T を割り当てます。

tasks.json で設定した他のものは Ctrl+Shift+P から Tasks: Run Task から実行できます。

参考

qiita.com

qiita.com