feat: solve day 1 part 1

This commit is contained in:
Stevan Freeborn
2025-12-01 06:49:26 -06:00
parent 2734425a33
commit df4bd2cfa3
10 changed files with 297 additions and 3 deletions
+48
View File
@@ -0,0 +1,48 @@
// Package dial provides a model for representing a dial
package dial
import (
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/direction"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction"
)
type Dial interface {
Value() int
Turn(instruction instruction.Instruction)
}
type dial struct {
value int
}
func New() Dial {
return &dial{
value: 50,
}
}
func (d *dial) Value() int {
return d.value
}
func (d *dial) Turn(instruction instruction.Instruction) {
if instruction.Direction() == direction.Left {
d.value -= instruction.Distance()
for d.value < 0 {
d.value += 100
}
return
}
if instruction.Direction() == direction.Right {
d.value += instruction.Distance()
for d.value > 99 {
d.value -= 100
}
return
}
}
+78
View File
@@ -0,0 +1,78 @@
package dial_test
import (
"testing"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/dial"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction"
)
func TestNew(t *testing.T) {
expectedStartingValue := 50
result := dial.New()
if result.Value() != expectedStartingValue {
t.Errorf("got unexpected beginning value. expected %d but got %d", expectedStartingValue, result.Value())
}
}
type TurnTestCase struct {
Instruction instruction.Instruction
ExpectedDialValue int
}
func TestTurn(t *testing.T) {
testCases := []TurnTestCase{
{
Instruction: instruction.FromParts("L", 68),
ExpectedDialValue: 82,
},
{
Instruction: instruction.FromParts("L", 30),
ExpectedDialValue: 52,
},
{
Instruction: instruction.FromParts("R", 48),
ExpectedDialValue: 0,
},
{
Instruction: instruction.FromParts("L", 5),
ExpectedDialValue: 95,
},
{
Instruction: instruction.FromParts("R", 60),
ExpectedDialValue: 55,
},
{
Instruction: instruction.FromParts("L", 55),
ExpectedDialValue: 0,
},
{
Instruction: instruction.FromParts("L", 1),
ExpectedDialValue: 99,
},
{
Instruction: instruction.FromParts("L", 99),
ExpectedDialValue: 0,
},
{
Instruction: instruction.FromParts("R", 14),
ExpectedDialValue: 14,
},
{
Instruction: instruction.FromParts("L", 82),
ExpectedDialValue: 32,
},
}
dial := dial.New()
for i, testCase := range testCases {
dial.Turn(testCase.Instruction)
if dial.Value() != testCase.ExpectedDialValue {
t.Errorf("%d: got dial value %d, but expected dial value %d", i, dial.Value(), testCase.ExpectedDialValue)
}
}
}