Skip to content

Commit

Permalink
Set fixed width for Run view. Add FindMin and FindMax
Browse files Browse the repository at this point in the history
  • Loading branch information
DaltonSW committed Dec 25, 2024
1 parent 688645a commit 9dc1c4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
10 changes: 6 additions & 4 deletions aocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func RunTest[In InputData, Out AnswerData](title string, solver Solver[In, Out],
borderStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(outColor).
Padding(0, 2)
Padding(0, 2).
MaxWidth(18)

// Pretty info
prettyAnswer := fmt.Sprintf("Answer : %v\n", answerStr)
Expand Down Expand Up @@ -108,11 +109,12 @@ func RunSolve[In InputData, Out AnswerData](title string, solver Solver[In, Out]
borderStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(puzzleSolveColor).
Padding(0, 2)
Padding(0, 2).
MaxWidth(18)

// Pretty info
prettyAnswer := fmt.Sprintf("Answer : %v\n", answerStr)
prettyTime := fmt.Sprintf("Runtime : %v", timeTaken)
prettyAnswer := fmt.Sprintf("Answer : %v\n", answerStr)
prettyTime := fmt.Sprintf("Runtime: %v", timeTaken)

// Render the answer inside the border
wrappedInfo := borderStyle.Render(prettyAnswer + prettyTime)
Expand Down
32 changes: 30 additions & 2 deletions aocutils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ func Distance2D[n Number](x1, y1, x2, y2 n) float64 {
}

// Slope2D calculates the slope between two points in 2D space.
// Returns the slope as a float64
// Returns the slope as a float64. If x1 == x2, returns math.Inf
func Slope2D[n Number](x1, y1, x2, y2 n) float64 {
return float64((y2 - y1) / (x2 - x1))
if x2-x1 == 0 {
return math.Inf(0)
} else {
return float64((y2 - y1) / (x2 - x1))
}
}

// Distance3D calculates the Euclidean distance between two points in 3D space.
Expand All @@ -51,6 +55,30 @@ func ManhattanDistance3D(x1, y1, z1, x2, y2, z2 int) int {
return AbsVal(x2-x1) + AbsVal(y2-y1) + AbsVal(z2-z1)
}

// FindMin finds and returns the smallest value in the list.
func FindMin[n Number](values []n) n {
m := values[0]
for _, x := range values {
if x < m {
m = x
}
}

return m
}

// FindMin finds and returns the largest value in the list.
func FindMax[n Number](values []n) n {
m := values[0]
for _, x := range values {
if x > m {
m = x
}
}

return m
}

// AbsVal calculates the absolute value of a number.
func AbsVal[num Number](n num) num {
if n < 0 {
Expand Down

0 comments on commit 9dc1c4e

Please sign in to comment.