Day 01
This commit is contained in:
commit
ad70f3e86b
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,47 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/thoas/go-funk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
inputBytes, _ := os.ReadFile("input.txt")
|
||||||
|
inputs := strings.Split(string(inputBytes), "\n")
|
||||||
|
|
||||||
|
log.Println("Puzzle 1:", puzzle1(inputs))
|
||||||
|
log.Println("Puzzle 2:", puzzle2(inputs))
|
||||||
|
}
|
||||||
|
|
||||||
|
func puzzle1(inputs []string) int {
|
||||||
|
increased := 0
|
||||||
|
last := 0
|
||||||
|
for _, input := range inputs {
|
||||||
|
inputNo, _ := strconv.Atoi(input)
|
||||||
|
if last > 0 && inputNo > last {
|
||||||
|
increased++
|
||||||
|
}
|
||||||
|
last = inputNo
|
||||||
|
}
|
||||||
|
return increased
|
||||||
|
}
|
||||||
|
|
||||||
|
func puzzle2(inputs []string) int {
|
||||||
|
increased := 0
|
||||||
|
last3 := []int{0, 0, 0}
|
||||||
|
for _, input := range inputs {
|
||||||
|
inputNo, _ := strconv.Atoi(input)
|
||||||
|
new3 := append(last3[1:], inputNo)
|
||||||
|
newSum := funk.SumInt(new3)
|
||||||
|
lastSum := funk.SumInt(last3)
|
||||||
|
if !funk.ContainsInt(last3, 0) && !funk.ContainsInt(new3, 0) && newSum > lastSum {
|
||||||
|
increased++
|
||||||
|
}
|
||||||
|
last3 = new3
|
||||||
|
}
|
||||||
|
return increased
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test(t *testing.T) {
|
||||||
|
input := `199,200,208,210,200,207,240,269,260,263`
|
||||||
|
assert.Equal(t, 7, puzzle1(strings.Split(input, ",")))
|
||||||
|
assert.Equal(t, 5, puzzle2(strings.Split(input, ",")))
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
module git.jlel.se/jlelse/aoc21
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/stretchr/testify v1.7.0
|
||||||
|
github.com/thoas/go-funk v0.9.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||||
|
)
|
|
@ -0,0 +1,15 @@
|
||||||
|
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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
|
||||||
|
github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
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=
|
Reference in New Issue