-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (180 loc) · 4.95 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// get-puzzle gets the puzzle for the given day (e.g. 'day08').
//
// The environment variable `AOC_COOKIES` must be set before this
// command is called.
//
// Usage from the root directory (where `go.mod` lives):
// $ go run cmd/get-puzzle/main.go day08
package main
import (
"flag"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/must"
)
var (
puzzleOnly = flag.Bool("p", false, "Download puzzle input.txt only")
)
const (
aocCookies = "AOC_COOKIES"
urlFmt = "https://adventofcode.com/%v/day/%v"
)
func main() {
flag.Parse()
if flag.NArg() == 0 {
log.Fatal("must supply a day to process, e.g. day08")
}
Each(flag.Args(), process)
log.Printf("Done.")
}
func process(dayStr string) {
if !strings.HasPrefix(dayStr, "day") {
log.Fatalf("expected e.g. 'day08' but got %q", dayStr)
}
year := time.Now().Year()
day := must.Atoi(dayStr[3:])
log.Printf("Processing %v ...", dayStr)
cookiesStr := os.Getenv(aocCookies)
if cookiesStr == "" {
log.Fatalf("must set env var %q", aocCookies)
}
cookies := parseCookies(cookiesStr)
url := fmt.Sprintf(urlFmt, year, day)
if !*puzzleOnly {
getFile(url, dayStr, "example1.txt", cookies, findPreCode)
}
getFile(url+"/input", dayStr, "input.txt", cookies, nil)
if !*puzzleOnly {
buf, err := exec.Command("git", "add", dayStr).CombinedOutput()
if err != nil {
log.Printf("%s", buf)
log.Fatal(err)
}
log.Printf("%s", buf)
msg := fmt.Sprintf("Initial commit for %v", dayStr)
buf, err = exec.Command("git", "commit", "-am", msg).CombinedOutput()
if err != nil {
log.Printf("%s", buf)
log.Fatal(err)
}
log.Printf("%s", buf)
}
}
func getFile(url, dayStr, outFile string, cookies []*http.Cookie, findPreCode func(string) string) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
Each(cookies, req.AddCookie)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if findPreCode != nil {
b = []byte(findPreCode(string(b)))
if len(b) == 0 {
log.Fatal("no puzzle found")
}
src := fmt.Sprintf("%v`\n%s\n`\n", mainTestSource, b)
fn1 := filepath.Join(dayStr, "part1", "main_test.go")
fn2 := filepath.Join(dayStr, "part2", "main_test.go")
if _, err := os.Stat(fn1); err == nil {
log.Fatalf("%v already exists; aborting", fn1)
}
if _, err := os.Stat(fn2); err == nil {
log.Fatalf("%v already exists; aborting", fn2)
}
if err := ioutil.WriteFile(fn1, []byte(src), 0644); err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(fn2, []byte(src), 0644); err != nil {
log.Fatal(err)
}
} else if !*puzzleOnly {
fn1 := filepath.Join(dayStr, "part1", "main.go")
fn2 := filepath.Join(dayStr, "part2", "main.go")
if _, err := os.Stat(fn1); err == nil {
log.Fatalf("%v already exists; aborting", fn1)
}
if _, err := os.Stat(fn2); err == nil {
log.Fatalf("%v already exists; aborting", fn2)
}
if err := ioutil.WriteFile(fn1, []byte(mainSource), 0644); err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(fn2, []byte(mainSource), 0644); err != nil {
log.Fatal(err)
}
}
filename := filepath.Join(dayStr, outFile)
if err := ioutil.WriteFile(filename, b, 0644); err != nil {
log.Fatal(err)
}
}
var preCodeRE = regexp.MustCompile(`^(?ms).*?<pre><code>`)
func findPreCode(s string) string {
s = strings.ReplaceAll(s, "<em>", "")
s = strings.ReplaceAll(s, "</em>", "")
s = html.UnescapeString(s)
parts := strings.Split(s, "</code></pre>")
parts = Map(parts[:len(parts)-1], func(p string) string { return preCodeRE.ReplaceAllString(p, "") })
return strings.Join(parts, "\n\n")
}
func parseCookies(cookies string) (ret []*http.Cookie) {
return Reduce(strings.Split(cookies, "; "), ret, func(c string, acc []*http.Cookie) []*http.Cookie {
p := strings.Split(c, "=")
return append(acc, &http.Cookie{Name: p[0], Value: p[1]})
})
}
var mainSource = `// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/must"
)
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
buf := must.ReadFile(filename)
printf("Solution: %v\n", len(buf))
}
`
var mainTestSource = `package main
import (
"testing"
"github.com/gmlewis/advent-of-code-2021/test"
)
func TestExample(t *testing.T) {
want := "Solution: 0\n"
test.Runner(t, example1, want, process, &printf)
}
func BenchmarkExample(b *testing.B) {
test.Benchmark(b, "../example1.txt", process, &logf, &printf)
}
func BenchmarkInput(b *testing.B) {
test.Benchmark(b, "../input.txt", process, &logf, &printf)
}
var example1 = `