Skip to content

Commit ced7c13

Browse files
committed
[2024] Try out Go generics
1 parent 35e7599 commit ced7c13

File tree

7 files changed

+36
-25
lines changed

7 files changed

+36
-25
lines changed

2024/day01/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ func part1(input string) int64 {
1717
log.Fatal(err)
1818
}
1919

20-
lib.SortSliceInt64(first)
21-
lib.SortSliceInt64(second)
20+
lib.SortSliceAscending(first)
21+
lib.SortSliceAscending(second)
2222

2323
sum := int64(0)
2424
for i := 0; i < len(first); i++ {
25-
sum += lib.AbsInt64(first[i] - second[i])
25+
sum += lib.Abs(first[i] - second[i])
2626
}
2727

2828
return sum
@@ -59,12 +59,12 @@ func parseInput(input string) ([]int64, []int64, error) {
5959
continue
6060
}
6161

62-
firstValue, err := lib.ParseInt64(matches[1])
62+
firstValue, err := lib.ParseInt[int64](matches[1])
6363
if err != nil {
6464
return nil, nil, err
6565
}
6666

67-
secondValue, err := lib.ParseInt64(matches[2])
67+
secondValue, err := lib.ParseInt[int64](matches[2])
6868
if err != nil {
6969
return nil, nil, err
7070
}

2024/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/kfarnung/advent-of-code/2024
22

33
go 1.23
44

5-
require github.com/stretchr/testify v1.10.0
5+
require (
6+
github.com/stretchr/testify v1.10.0
7+
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.1 // indirect

2024/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
66
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
8+
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
79
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
810
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
911
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

2024/lib/intmath.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package lib
22

3-
// AbsInt64 calculates the absolute value of a given int
4-
func AbsInt64(n int64) int64 {
3+
import "golang.org/x/exp/constraints"
4+
5+
// Abs calculates the absolute value of a given int
6+
func Abs[T constraints.Signed](n T) T {
57
if n < 0 {
68
return -n
79
}
810

911
return n
1012
}
1113

12-
// ModInt64 calculates the modulus value (as opposed to the remainder)
14+
// Mod calculates the modulus value (as opposed to the remainder)
1315
// https://github.com/golang/go/issues/448
14-
func ModInt64(x, y int64) int64 {
16+
func Mod[T constraints.Signed](x, y T) T {
1517
return ((x % y) + y) % y
1618
}

2024/lib/parse.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package lib
22

3-
import "strconv"
3+
import (
4+
"strconv"
5+
"unsafe"
46

5-
// ParseInt32 parses the string as a base-10 int
6-
func ParseInt32(text string) (int32, error) {
7-
value, err := strconv.ParseInt(text, 10, 32)
7+
"golang.org/x/exp/constraints"
8+
)
9+
10+
// ParseInt64 parses the string as a base-10 int64
11+
func ParseInt[T constraints.Signed](text string) (T, error) {
12+
size := unsafe.Sizeof(T(0))
13+
value, err := strconv.ParseInt(text, 10, int(size*8))
814
if err != nil {
915
return 0, err
1016
}
1117

12-
return int32(value), nil
13-
}
14-
15-
// ParseInt64 parses the string as a base-10 int64
16-
func ParseInt64(text string) (int64, error) {
17-
return strconv.ParseInt(text, 10, 64)
18+
return T(value), nil
1819
}

2024/lib/point2d.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (p *Point2D) Rotate90DegreesCounterClockwise(count int) {
4141

4242
// ManhattanDistance calculates the manhattan distance between two points
4343
func (p Point2D) ManhattanDistance(other Point2D) int64 {
44-
return AbsInt64(other.X-p.X) + AbsInt64(other.Y-p.Y)
44+
return Abs(other.X-p.X) + Abs(other.Y-p.Y)
4545
}
4646

4747
func (p Point2D) String() string {

2024/lib/slice.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package lib
22

3-
import "sort"
3+
import (
4+
"cmp"
5+
"sort"
6+
)
47

58
// StringSliceToInt64 converts the slice of strings to a slice of int64 values.
69
func StringSliceToInt64(input []string) ([]int64, error) {
710
result := make([]int64, len(input))
811
for i, val := range input {
9-
num, err := ParseInt64(val)
12+
num, err := ParseInt[int64](val)
1013
if err != nil {
1114
return nil, err
1215
}
@@ -21,7 +24,7 @@ func StringSliceToInt64(input []string) ([]int64, error) {
2124
func StringSliceToInt32(input []string) ([]int32, error) {
2225
result := make([]int32, len(input))
2326
for i, val := range input {
24-
num, err := ParseInt32(val)
27+
num, err := ParseInt[int32](val)
2528
if err != nil {
2629
return nil, err
2730
}
@@ -32,7 +35,7 @@ func StringSliceToInt32(input []string) ([]int32, error) {
3235
return result, nil
3336
}
3437

35-
func SortSliceInt64(input []int64) {
38+
func SortSliceAscending[T cmp.Ordered](input []T) {
3639
sort.Slice(input, func(i, j int) bool {
3740
return input[i] < input[j]
3841
})

0 commit comments

Comments
 (0)