commit 8d6a6df57230957c5d64920dc6c241c979ae1e69 Author: Gregory Date: Mon Feb 7 21:59:19 2022 +0200 day1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..c28f682 --- /dev/null +++ b/client/client.go @@ -0,0 +1,41 @@ +package client + +import ( + "fmt" + "io" + "log" + "net/http" +) + +type client struct { + session string +} + +func NewClient(session string) client { + return client{session} +} + +func (c *client) Day(day int) []byte { + client := http.Client{} + req, err := http.NewRequest("GET", fmt.Sprintf("https://adventofcode.com/2021/day/%v/input", day), nil) + + if err != nil { + log.Fatalln(err) + } + + req.Header.Set("Cookie", fmt.Sprintf("session=%s", c.session)) + resp, err := client.Do(req) + + if err != nil { + log.Fatalln(err) + } + + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + + if err != nil { + log.Fatalln(err) + } + + return body +} diff --git a/d1/d1.go b/d1/d1.go new file mode 100644 index 0000000..7f7c853 --- /dev/null +++ b/d1/d1.go @@ -0,0 +1,22 @@ +package d1 + +import ( + "strconv" + "strings" +) + +func D1(scans string) int64 { + depths := strings.Split(strings.TrimSpace(scans), "\n") + downs := 0 + + for i := 1; i < len(depths); i++ { + next, _ := strconv.Atoi(strings.TrimSpace(depths[i])) + prev, _ := strconv.Atoi(strings.TrimSpace(depths[i-1])) + + if next > prev { + downs++ + } + } + + return int64(downs) +} diff --git a/d1/d1_test.go b/d1/d1_test.go new file mode 100644 index 0000000..c82a894 --- /dev/null +++ b/d1/d1_test.go @@ -0,0 +1,25 @@ +package d1 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestD1(t *testing.T) { + input := ` + 199 + 200 + 208 + 210 + 200 + 207 + 240 + 269 + 260 + 263 + ` + actual := D1(input) + + assert.EqualValues(t, 7, actual) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3a61fd5 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/fotonmoton/aoc2021 + +go 1.17 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.7.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b380ae4 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..ede3869 --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/fotonmoton/aoc2021/client" + "github.com/fotonmoton/aoc2021/d1" +) + +func main() { + + reader := bufio.NewReader(os.Stdin) + fmt.Print("Enter your session: ") + session, _ := reader.ReadString('\n') + client := client.NewClient(strings.TrimSpace(session)) + + fmt.Printf("day1: %v\n", d1.D1(string(client.Day(1)))) +}