day1
This commit is contained in:
commit
8d6a6df572
7 changed files with 130 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.vscode
|
41
client/client.go
Normal file
41
client/client.go
Normal file
|
@ -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
|
||||||
|
}
|
22
d1/d1.go
Normal file
22
d1/d1.go
Normal file
|
@ -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)
|
||||||
|
}
|
25
d1/d1_test.go
Normal file
25
d1/d1_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
10
go.mod
Normal file
10
go.mod
Normal file
|
@ -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
|
||||||
|
)
|
10
go.sum
Normal file
10
go.sum
Normal file
|
@ -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=
|
21
main.go
Normal file
21
main.go
Normal file
|
@ -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))))
|
||||||
|
}
|
Loading…
Reference in a new issue