Skip to content

Commit a0710f4

Browse files
committed
feat: Add script to automate task solution setup
1 parent c8d56e1 commit a0710f4

File tree

6 files changed

+158
-1
lines changed

6 files changed

+158
-1
lines changed

‎.env.example‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AOC_SESSION=<session cookie from website>

‎README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# ☃️ Advent of Code 2024
22

3-
My solutions for my second Advent of Code in Go!
3+
My solutions for my second Advent of Code in Go!
4+
5+
## Usage
6+
```bash
7+
# Create template solution file and download input.txt from AoC website
8+
go run tools/aoc.go
9+
10+
# Run solution
11+
go run day_<day>/main.go
12+
```

‎go.mod‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/Norbiros/AoC2024
22

33
go 1.23.1
4+
5+
require github.com/joho/godotenv v1.5.1 // indirect

‎go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
2+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

‎tools/aoc.go‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"flag"
6+
"fmt"
7+
"github.com/joho/godotenv"
8+
"io"
9+
"log"
10+
"net/http"
11+
"os"
12+
"path/filepath"
13+
"strings"
14+
"time"
15+
)
16+
17+
//go:embed template.go
18+
var template string
19+
20+
func main() {
21+
err := godotenv.Load()
22+
if err != nil {
23+
log.Fatal("Error loading .env file")
24+
}
25+
26+
var day, year int
27+
var cookie string
28+
29+
today := time.Now()
30+
flag.IntVar(&day, "day", today.Day(), "day number to fetch, 1-25")
31+
flag.IntVar(&year, "year", today.Year(), "day of year to fetch")
32+
flag.StringVar(&cookie, "cookie", os.Getenv("AOC_SESSION"), "session cookie from website")
33+
flag.Parse()
34+
35+
url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", year, day)
36+
prefixedDay := fmt.Sprintf("day_%02d", day)
37+
38+
fmt.Printf("Sending request to %s...\n", url)
39+
response, err := sendGetRequest(url, cookie)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
stringResponse := string(response)
44+
stringResponse = strings.TrimRight(stringResponse, "\n")
45+
fmt.Println("Received response!")
46+
47+
err = writeToFile(filepath.Join(prefixedDay, "input.txt"), stringResponse)
48+
if err != nil {
49+
log.Fatal("Error writing to file:", err)
50+
}
51+
52+
err = writeToFile(filepath.Join(prefixedDay, "main.go"), template)
53+
if err != nil {
54+
log.Fatal("Error writing to file:", err)
55+
}
56+
57+
fmt.Println()
58+
fmt.Printf("Successfully created all template files for day %d of Advent of Code %d\n", day, year)
59+
fmt.Printf("You can view task description here: https://adventofcode.com/%d/day/%d\n", year, day)
60+
61+
}
62+
63+
func sendGetRequest(url, cookie string) ([]byte, error) {
64+
client := &http.Client{}
65+
req, err := http.NewRequest("GET", url, nil)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
req.AddCookie(&http.Cookie{
71+
Name: "session",
72+
Value: cookie,
73+
})
74+
75+
response, err := client.Do(req)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
defer func() {
81+
_ = response.Body.Close()
82+
}()
83+
84+
body, err := io.ReadAll(response.Body)
85+
if err != nil {
86+
return nil, err
87+
}
88+
89+
return body, nil
90+
}
91+
92+
func writeToFile(filePath, content string) error {
93+
if _, err := os.Stat(filePath); err == nil {
94+
fmt.Println("file already exists:", filePath)
95+
return nil
96+
}
97+
98+
dir := filepath.Dir(filePath)
99+
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
100+
return fmt.Errorf("creating directory: %w", err)
101+
}
102+
103+
file, err := os.Create(filePath)
104+
if err != nil {
105+
return err
106+
}
107+
defer func() {
108+
_ = file.Close()
109+
}()
110+
111+
if _, err := file.WriteString(content); err != nil {
112+
return fmt.Errorf("writing to file: %w", err)
113+
}
114+
return nil
115+
}

‎tools/template.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build ignore
2+
3+
package main
4+
5+
import (
6+
_ "embed"
7+
"fmt"
8+
"log"
9+
)
10+
11+
//go:embed input.txt
12+
var input string
13+
14+
func main() {
15+
fmt.Println("Solving \"${TASK_TITLE}\"...")
16+
fmt.Println("Part 1 result", partOne(input))
17+
fmt.Println("Part 2 result", partTwo(input))
18+
}
19+
20+
func partOne(input string) int {
21+
log.Panic("Solution for Part 1 not implemented!")
22+
return -1
23+
}
24+
25+
func partTwo(input string) int {
26+
log.Panic("Solution for Part 2 not implemented!")
27+
return -1
28+
}

0 commit comments

Comments
 (0)