Skip to content

Commit

Permalink
Convert math utils to generics
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 2, 2022
1 parent 25e73df commit 8865583
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 61 deletions.
15 changes: 5 additions & 10 deletions challenge/day1/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package day1
import (
"fmt"

"github.com/nlowe/aoc2022/util/gmath"

"github.com/nlowe/aoc2022/util"

"github.com/spf13/cobra"
Expand All @@ -25,10 +27,7 @@ func partA(challenge *challenge.Input) int {
bestElf := 0
for line := range challenge.Lines() {
if line == "" {
if currentElf > bestElf {
bestElf = currentElf
}

bestElf = gmath.Max(currentElf, bestElf)
currentElf = 0
continue
}
Expand All @@ -37,11 +36,7 @@ func partA(challenge *challenge.Input) int {
currentElf += v
}

if currentElf != 0 {
if currentElf > bestElf {
bestElf = currentElf
}
}

// And once more in case there's no trailing newline
bestElf = gmath.Max(currentElf, bestElf)
return bestElf
}
1 change: 1 addition & 0 deletions challenge/day1/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func partB(challenge *challenge.Input) int {
currentElf += v
}

// And once more in case there's no trailing newline
if currentElf != 0 {
topThree = populatePodium(currentElf, topThree)
}
Expand Down
6 changes: 4 additions & 2 deletions gen/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"text/template"
"time"

"github.com/nlowe/aoc2022/util/gmath"

"github.com/nlowe/aoc2022/util"
)

Expand Down Expand Up @@ -72,10 +74,10 @@ func dayLimit(now time.Time) int {

// Challenges unlock at 0500 UTC
if now.Hour() < 5 {
return util.IntClamp(0, now.Day()-1, 25)
return gmath.Clamp(0, now.Day()-1, 25)
}

return util.IntClamp(0, now.Day(), 25)
return gmath.Clamp(0, now.Day(), 25)
}

func genDay(n int) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9
)

require (
Expand All @@ -23,7 +24,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -462,8 +464,9 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
47 changes: 47 additions & 0 deletions util/gmath/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package gmath

import (
"fmt"

"golang.org/x/exp/constraints"
)

type Number interface {
constraints.Float | constraints.Integer
}

func Abs[T Number](n T) T {
if n < 0 {
return -n
}

return n
}

func Max[T Number](a, b T) T {
if a > b {
return a
}

return b
}

func Min[T Number](a, b T) T {
if a < b {
return a
}

return b
}

func Clamp[T Number](low, n, high T) T {
if low > high {
panic(fmt.Errorf("Clamp: low cannot be > high: %v > %v", low, high))
}

return Max(Min(n, high), low)
}

func ManhattanDistance[T Number](x1, y1, x2, y2 T) T {
return Abs(x2-x1) + Abs(y2-y1)
}
12 changes: 6 additions & 6 deletions util/math_test.go → util/gmath/math_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package gmath

import (
"fmt"
Expand All @@ -23,7 +23,7 @@ func TestIntMin(t *testing.T) {

for _, tt := range tests {
t.Run(fmt.Sprintf("%d %d", tt.a, tt.b), func(t *testing.T) {
assert.Equal(t, tt.expected, IntMin(tt.a, tt.b))
assert.Equal(t, tt.expected, Min(tt.a, tt.b))
})
}
}
Expand All @@ -42,7 +42,7 @@ func TestIntMax(t *testing.T) {

for _, tt := range tests {
t.Run(fmt.Sprintf("%d %d", tt.a, tt.b), func(t *testing.T) {
assert.Equal(t, tt.expected, IntMax(tt.a, tt.b))
assert.Equal(t, tt.expected, Max(tt.a, tt.b))
})
}
}
Expand All @@ -61,13 +61,13 @@ func TestIntClamp(t *testing.T) {

for _, tt := range tests {
t.Run(fmt.Sprintf("%d <= %d <= %d", tt.low, tt.n, tt.high), func(t *testing.T) {
assert.Equal(t, tt.expected, IntClamp(tt.low, tt.n, tt.high))
assert.Equal(t, tt.expected, Clamp(tt.low, tt.n, tt.high))
})
}

t.Run("Panics if low > high", func(t *testing.T) {
assert.PanicsWithError(t, "IntClamp: low cannot be > high: 2 > 1", func() {
_ = IntClamp(2, 0, 1)
assert.PanicsWithError(t, "Clamp: low cannot be > high: 2 > 1", func() {
_ = Clamp(2, 0, 1)
})
})
}
Expand Down
41 changes: 0 additions & 41 deletions util/math.go

This file was deleted.

0 comments on commit 8865583

Please sign in to comment.