AtcoderをGoで解答する環境をDevcontainerで作った話

はじめに

2024年の目標の一つでAtCoderの緑色になることを掲げています

まずは形から入るために、AtCoderの環境を作ってみました
今回はコードの一部を紹介します
今回はCodeSpacesでも動けば良いなと思ってDev Containerを使っています

devcontainer.json

コンテナの設定ファイルは.devcontainer/devcontainer.jsonだけ用意しています

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "name": "Debian",
  "image": "mcr.microsoft.com/devcontainers/base:bullseye",
  "features": {
    "ghcr.io/devcontainers/features/go:1": {
      "version": "1.20.6"
    },
    "ghcr.io/devcontainers/features/node:1": {},
    "ghcr.io/devcontainers/features/python:1": {},
    "ghcr.io/devcontainers-contrib/features/npm-package:1": {
      "package": "atcoder-cli"
    },
    "ghcr.io/akhildevelops/devcontainer-features/pip:0": {
      "PACKAGES": "online-judge-tools"
    }
  },

  "postCreateCommand": "cp -r atcoder-cli-nodejs ~/.config"
}

devcontainer features

devcontainerに追加の機能を簡単に追加できる機能です

今回はdevcontainer featuresを使って必要なパッケージを追加しています
Go言語のインストールの部分を他の言語に変えればそのまま使えるんじゃないかなと思っています

tasks.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "online-judge-tools test",
      "type": "shell",
      "command": "oj t -c \"go run ${fileDirname}/main.go\" -d ${fileDirname}/test/",
      "group": {
        "kind": "test",
        "isDefault": true
      }
    },
    {
      "label": "acc submit",
      "type": "shell",
      "command": "cd ${fileDirname} && acc s",
      "problemMatcher": [],
    },
    {
      "label": "acc new",
      "type": "shell",
      "command": "cd ${workspaceFolder}/contest && acc new ${input:contest-id}"
    }
  ],
  "inputs": [
    {
      "id": "contest-id",
      "type": "promptString",
      "description": "Contest ID"
    }
  ]
}

vscodeのタスクランナーの設定ファイルです

online-judge-toolsを使ってテストを実行とatcoder-cliを使ってコンテストを作成、提出できるようにしています

goのテンプレート

よく使うテンプレートを用意しています
ここはまだ競技プログラミングの経験が浅く、何が必要かわかっていないので最適化できていないです
とりあえず最低限必要そうなものだけ用意しています

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

func main() {
	solve(os.Stdin, os.Stdout)
}

func solve(r io.Reader, w io.Writer) {
	var sc = bufio.NewScanner(r)
	var out = bufio.NewWriter(w)

	defer out.Flush()

	sc.Split(bufio.ScanWords) // スペース区切りの設定
	s := nextString(sc)
	fmt.Fprintln(out, s)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func nextString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

func min(values ...int) int {
	ret := values[0]
	for _, v := range values {
		if ret > v {
			ret = v
		}
	}
	return ret
}

func max(values ...int) int {
	ret := values[0]
	for _, v := range values {
		if ret < v {
			ret = v
		}
	}
	return ret
}

./testにテスト用のデータを作成してくれるので、それを使ってテストできるようにしています
delveは標準入力を受け付けないみたいなので、VSCodeのデバッグを使うときはこのテストファイルを使ってmain.goを実行しています

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
	"bytes"
	"os"
	"strings"
	"testing"
)

func Test_solve(t *testing.T) {
	files, err := os.ReadDir("./test")
	if err != nil {
		t.Fatal(err)
	}

	for _, file := range files {
		if strings.HasSuffix(file.Name(), ".in") {
			testName := strings.TrimSuffix(file.Name(), ".in")
			inFile := "./test/" + file.Name()
			outFile := "./test/" + testName + ".out"

			t.Run(testName, func(t *testing.T) {
				w := &bytes.Buffer{}
				in, err := os.Open(inFile)
				if err != nil {
					t.Fatal(err)
				}
				defer in.Close()

				out, err := os.ReadFile(outFile)
				if err != nil {
					t.Fatal(err)
				}

				solve(in, w)
				wantW := string(out)
				if gotW := w.String(); gotW != wantW {
					t.Errorf("solve() = %v, want %v", gotW, wantW)
				}
			})
		}
	}
}

このファイルはatcoder-cli-nodejsディレクトリに作ってあり、postCreateCommandで~/.configにコピーしています

おわりに

これでAtCoderの環境ができました
これまでは競プロにチャレンジするもののすぐ飽きてしまっていましたが、これで少しは続くかなと思っています

最終的にできたコードはこちらにあります

yhorikawa/atcoder-go

Built with Hugo
テーマ StackJimmy によって設計されています。