Skip to content

Commit d7b289a

Browse files
committed
cmd: Implement cli to run puzzles
1 parent da9a7d7 commit d7b289a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

cmd/main.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"strconv"
10+
11+
"github.com/pkg/errors"
12+
13+
"github.com/oleg-balunenko/advent-of-code/internal/puzzle"
14+
_ "github.com/oleg-balunenko/advent-of-code/internal/solutions/day01"
15+
)
16+
17+
var (
18+
inputDir = flag.String(
19+
"input_dir",
20+
filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "oleg-balunenko", "advent-of-code", "input"),
21+
"Path to directory with puzzles input files")
22+
)
23+
24+
func main() {
25+
flag.Parse()
26+
27+
menu()
28+
}
29+
30+
func menu() {
31+
fmt.Println("Menu:")
32+
33+
solvers := puzzle.Solvers()
34+
35+
var choices = make(map[string]string, len(solvers))
36+
37+
for i, s := range solvers {
38+
choices[strconv.Itoa(i+1)] = s
39+
fmt.Printf("%d. %s \n", i+1, s)
40+
}
41+
42+
var text string
43+
44+
for {
45+
_, err := fmt.Scanln(&text)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
if pname, ok := choices[text]; !ok {
51+
fmt.Println("wrong choice, try again")
52+
} else {
53+
run(pname)
54+
return
55+
}
56+
}
57+
}
58+
59+
func run(name string) {
60+
s, err := puzzle.GetSolver(name)
61+
if err != nil {
62+
log.Fatal(errors.Wrap(err, "failed to get solver"))
63+
}
64+
65+
input := filepath.Join(*inputDir, fmt.Sprintf("%s.txt", name))
66+
67+
if err := puzzle.Run(s, input); err != nil {
68+
log.Fatal(errors.Wrap(err, "failed to run puzzle solver"))
69+
}
70+
}

0 commit comments

Comments
 (0)