-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (79 loc) · 1.89 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/saimanwong/go-aoc/problem"
"github.com/saimanwong/go-aoc/problem/year2020"
"github.com/saimanwong/go-aoc/problem/year2021"
"github.com/saimanwong/go-aoc/problem/year2022"
"github.com/saimanwong/go-aoc/problem/year2024"
)
var problems map[problem.Year]problem.Problems = map[problem.Year]problem.Problems{
"2020": year2020.GetAllProblems(),
"2021": year2021.GetAllProblems(),
"2022": year2022.GetAllProblems(),
"2024": year2024.GetAllProblems(),
}
func main() {
if len(os.Args) != 4 {
fmt.Println("exactly 3 arguments required")
os.Exit(1)
}
year := os.Args[1]
re := regexp.MustCompile("^[0-9]{4}$")
if !re.MatchString(year) {
fmt.Println("first argument must be a valid year")
os.Exit(2)
}
day := os.Args[2]
re = regexp.MustCompile("^[0-9]{2}$")
if !re.MatchString(day) {
fmt.Println("second argument must be a valid day")
os.Exit(3)
}
inputFile := os.Args[3]
re = regexp.MustCompile("^[a-z0-9]+$")
if !re.MatchString(inputFile) {
fmt.Println("third argument must only contain alphanumeric characters")
os.Exit(4)
}
probY, ok := problems[problem.Year(year)]
if !ok {
fmt.Println("year", year, "does not exist")
os.Exit(1)
}
prob, ok := probY[problem.Day(day)]
if !ok {
fmt.Println("day", day, "does not exist")
os.Exit(1)
}
path := filepath.Join(
"inputs",
fmt.Sprintf("year%s", year),
fmt.Sprintf("day%s", day),
inputFile,
)
if err := parse(path, prob); err != nil {
fmt.Printf("parse fail: %v\n", err)
os.Exit(1)
}
fmt.Printf("===== %s-12-%s %s =====\n", year, day, inputFile)
prob.Run()
}
func parse(path string, prob problem.Problemer) error {
var lines []string
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
prob.SetInput(lines)
return nil
}