Started 2022 with go

This commit is contained in:
Karma Riuk
2023-08-02 11:50:29 +02:00
parent 567c8487b5
commit 7098b525f0
26 changed files with 6810 additions and 0 deletions

93
2022/go/01/answer.go Normal file
View File

@ -0,0 +1,93 @@
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
log "github.com/sirupsen/logrus"
)
type Input struct {
calories [][]int
}
func input(filename string) *Input {
file, err := os.Open(filename)
check(err, fmt.Sprintf("Couldn't open %q", filename))
defer file.Close()
input := &Input{}
input.calories = append(input.calories, make([]int, 0))
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
var line string = scanner.Text()
if len(line) == 0 {
input.calories = append(input.calories, make([]int, 0))
} else {
cal, err := strconv.Atoi(line)
check(err, fmt.Sprintf("Couldn't convert %q to int", line))
input.calories[len(input.calories)-1] = append(input.calories[len(input.calories)-1], cal)
}
}
return input
}
func map_slice[S, T any](ss []S, mapper func(S) T) []T {
var ret []T = make([]T, 0, len(ss))
for _, s := range ss {
ret = append(ret, mapper(s))
}
return ret
}
func max(arr []int) int {
var max int = 0
for _, e := range arr {
if e > max {
max = e
}
}
return max
}
func sum(arr []int) int {
res := 0
for _, e := range arr {
res += e
}
return res
}
func result(inp *Input, part int8) string {
var res string
if part == 1 {
var max int = max(
map_slice(inp.calories, func(s []int) int { return sum(s) }),
)
res = fmt.Sprint(max)
} else {
sums := map_slice(inp.calories, func(s []int) int { return sum(s) })
sort.Ints(sums)
res = fmt.Sprint(sum(sums[len(sums)-3:]))
}
return res
}
func check(e error, message string) {
if e != nil {
log.Error(message)
panic(e)
}
}
func main() {
fmt.Println(result(input("sample1"), 1))
}

44
2022/go/01/answer_test.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"reflect"
"testing"
)
func TestInputSample1(t *testing.T) {
filename := "sample1"
expected := &Input{
calories: [][]int{
{1000, 2000, 3000},
{4000},
{5000, 6000},
{7000, 8000, 9000},
{10000},
},
}
var got *Input = input(filename)
if !reflect.DeepEqual(*got, *expected) {
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
}
}
func TestResult(t *testing.T) {
tests := []struct {
part int8
filename string
expected string
}{
{1, "sample1", "24000"},
{1, "input", "71924"},
{2, "sample1", "45000"},
{2, "input", "210406"},
}
for _, test := range tests {
var got string = result(input(test.filename), test.part)
if got != test.expected {
t.Errorf("result = %q, want %q", got, test.expected)
}
}
}

7
2022/go/01/go.mod Normal file
View File

@ -0,0 +1,7 @@
module aoc/1
go 1.20
require github.com/sirupsen/logrus v1.9.3
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

15
2022/go/01/go.sum Normal file
View File

@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
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=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=

2251
2022/go/01/input Normal file

File diff suppressed because it is too large Load Diff

14
2022/go/01/sample1 Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000