Skip to content

Commit

Permalink
[2024] Try out Go generics
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarnung committed Dec 2, 2024
1 parent 35e7599 commit ced7c13
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
10 changes: 5 additions & 5 deletions 2024/day01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func part1(input string) int64 {
log.Fatal(err)
}

lib.SortSliceInt64(first)
lib.SortSliceInt64(second)
lib.SortSliceAscending(first)
lib.SortSliceAscending(second)

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

return sum
Expand Down Expand Up @@ -59,12 +59,12 @@ func parseInput(input string) ([]int64, []int64, error) {
continue
}

firstValue, err := lib.ParseInt64(matches[1])
firstValue, err := lib.ParseInt[int64](matches[1])
if err != nil {
return nil, nil, err
}

secondValue, err := lib.ParseInt64(matches[2])
secondValue, err := lib.ParseInt[int64](matches[2])
if err != nil {
return nil, nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion 2024/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/kfarnung/advent-of-code/2024

go 1.23

require github.com/stretchr/testify v1.10.0
require (
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions 2024/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
10 changes: 6 additions & 4 deletions 2024/lib/intmath.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package lib

// AbsInt64 calculates the absolute value of a given int
func AbsInt64(n int64) int64 {
import "golang.org/x/exp/constraints"

// Abs calculates the absolute value of a given int
func Abs[T constraints.Signed](n T) T {
if n < 0 {
return -n
}

return n
}

// ModInt64 calculates the modulus value (as opposed to the remainder)
// Mod calculates the modulus value (as opposed to the remainder)
// https://github.com/golang/go/issues/448
func ModInt64(x, y int64) int64 {
func Mod[T constraints.Signed](x, y T) T {
return ((x % y) + y) % y
}
21 changes: 11 additions & 10 deletions 2024/lib/parse.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package lib

import "strconv"
import (
"strconv"
"unsafe"

// ParseInt32 parses the string as a base-10 int
func ParseInt32(text string) (int32, error) {
value, err := strconv.ParseInt(text, 10, 32)
"golang.org/x/exp/constraints"
)

// ParseInt64 parses the string as a base-10 int64
func ParseInt[T constraints.Signed](text string) (T, error) {
size := unsafe.Sizeof(T(0))
value, err := strconv.ParseInt(text, 10, int(size*8))
if err != nil {
return 0, err
}

return int32(value), nil
}

// ParseInt64 parses the string as a base-10 int64
func ParseInt64(text string) (int64, error) {
return strconv.ParseInt(text, 10, 64)
return T(value), nil
}
2 changes: 1 addition & 1 deletion 2024/lib/point2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *Point2D) Rotate90DegreesCounterClockwise(count int) {

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

func (p Point2D) String() string {
Expand Down
11 changes: 7 additions & 4 deletions 2024/lib/slice.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package lib

import "sort"
import (
"cmp"
"sort"
)

// StringSliceToInt64 converts the slice of strings to a slice of int64 values.
func StringSliceToInt64(input []string) ([]int64, error) {
result := make([]int64, len(input))
for i, val := range input {
num, err := ParseInt64(val)
num, err := ParseInt[int64](val)
if err != nil {
return nil, err
}
Expand All @@ -21,7 +24,7 @@ func StringSliceToInt64(input []string) ([]int64, error) {
func StringSliceToInt32(input []string) ([]int32, error) {
result := make([]int32, len(input))
for i, val := range input {
num, err := ParseInt32(val)
num, err := ParseInt[int32](val)
if err != nil {
return nil, err
}
Expand All @@ -32,7 +35,7 @@ func StringSliceToInt32(input []string) ([]int32, error) {
return result, nil
}

func SortSliceInt64(input []int64) {
func SortSliceAscending[T cmp.Ordered](input []T) {
sort.Slice(input, func(i, j int) bool {
return input[i] < input[j]
})
Expand Down

0 comments on commit ced7c13

Please sign in to comment.