-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
82 lines (72 loc) · 1.96 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
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"regexp"
"strings"
)
const head = `
# daily-problem-of-leetcode
![ranking](https://leetcode-badge.haozibi.dev/v1cn/ranking/xhofe.svg?logo=leetcode&color=4299E1)
![solved](https://leetcode-badge.haozibi.dev/v1cn/solved/xhofe.svg)
![accepted-rate](https://leetcode-badge.haozibi.dev/v1cn/accepted-rate/xhofe.svg?color=9F7AEA)
![records](https://leetcode-badge.haozibi.dev/v1cn/chart/submission-calendar/xhofe.svg)
`
func problemStr(year, month, filename string) string {
log.Printf("generate problem: %s\n", filename)
day := filename[:2]
name := filename[3:]
name = name[:strings.LastIndex(name, ".")]
return fmt.Sprintf(`- %s: [%s](./%s/%s/%s) [![leetcode](https://img.shields.io/badge/-link-38B2AC?logo=leetcode)](https://leetcode.cn/problems/%s/)
`,
day, name, year, month, filename, name)
}
func monthStr(year, month string) string {
log.Printf("generate month: %s\n", month)
str := "### " + month + "\n"
problems, err := ioutil.ReadDir(filepath.Join(year, month))
if err != nil {
log.Fatal(err)
}
for _, problem := range reverse(problems) {
str += problemStr(year, month, problem.Name())
}
return str
}
func yearStr(year string) string {
log.Printf("generate year: %s\n", year)
str := "## " + year + "\n"
months, err := ioutil.ReadDir(year)
if err != nil {
log.Fatal(err)
}
for _, month := range reverse(months) {
str += monthStr(year, month.Name())
}
return str
}
func reverse[T any](arr []T) []T {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
return arr
}
func main() {
readme := head
years, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
yearRegexp := regexp.MustCompile("^\\d{4}$")
for _, year := range reverse(years) {
if year.IsDir() && yearRegexp.Match([]byte(year.Name())) {
readme += yearStr(year.Name())
}
}
err = ioutil.WriteFile("README.md", []byte(readme), 0644)
if err != nil {
log.Fatal(err)
}
}