-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.go
49 lines (42 loc) · 1009 Bytes
/
solve.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
package main
import (
"context"
"fmt"
"os"
"plugin"
"strconv"
"github.com/spf13/cobra"
)
func NewSolveCmd(ctx context.Context) *cobra.Command {
return &cobra.Command{
Use: "solve PROBLEM",
Short: "Solve a problem",
Run: func(cmd *cobra.Command, args []string) {
problemNumStr := args[0]
problemNum, err := strconv.Atoi(problemNumStr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
// load module
// 1. open the so file to load the symbols
problemFileNoExt := fmt.Sprintf("%03d", problemNum)
plug, err := plugin.Open(fmt.Sprintf("./pkg/problems/%s/main.so", problemFileNoExt))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
symSolution, err := plug.Lookup("Solution")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var solution Solution
solution, ok := symSolution.(Solution)
if !ok {
fmt.Fprintln(os.Stderr, "unexpected type from module symbol")
os.Exit(1)
}
solution.Solve(ctx)
},
}
}