refactor: make not ugly
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// Package problem provides models and methods for representing a problem.
|
||||
package problem
|
||||
|
||||
type Problem interface {
|
||||
Solve() int
|
||||
}
|
||||
|
||||
type problem struct {
|
||||
operator string
|
||||
operands []int
|
||||
}
|
||||
|
||||
func From(operator string, operands []int) Problem {
|
||||
return problem{
|
||||
operator: operator,
|
||||
operands: operands,
|
||||
}
|
||||
}
|
||||
|
||||
func (p problem) Solve() int {
|
||||
result := 0
|
||||
|
||||
switch p.operator {
|
||||
case "+":
|
||||
for _, operand := range p.operands {
|
||||
result += operand
|
||||
}
|
||||
case "*":
|
||||
result = 1
|
||||
|
||||
for _, operand := range p.operands {
|
||||
result *= operand
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package problem_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/06/problem"
|
||||
)
|
||||
|
||||
func TestSolve(t *testing.T) {
|
||||
expected := 6
|
||||
operator := "+"
|
||||
operands := []int{1, 2, 3}
|
||||
|
||||
result := problem.From(operator, operands).Solve()
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("expected %d, got %d", expected, result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user