feat: solve day 1 part 1
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Package instruction provides a model for representing dial instructions
|
||||
package instruction
|
||||
|
||||
import "strconv"
|
||||
|
||||
type Instruction interface {
|
||||
Direction() string
|
||||
Distance() int
|
||||
}
|
||||
|
||||
type instruction struct {
|
||||
direction string
|
||||
distance int
|
||||
}
|
||||
|
||||
func FromLine(line string) Instruction {
|
||||
dir := string(line[0])
|
||||
dist := line[1:]
|
||||
distAsInt, _ := strconv.Atoi(dist)
|
||||
|
||||
return instruction{
|
||||
direction: dir,
|
||||
distance: distAsInt,
|
||||
}
|
||||
}
|
||||
|
||||
func FromParts(direction string, distance int) Instruction {
|
||||
return instruction{
|
||||
direction: direction,
|
||||
distance: distance,
|
||||
}
|
||||
}
|
||||
|
||||
func (i instruction) Direction() string {
|
||||
return i.direction
|
||||
}
|
||||
|
||||
func (i instruction) Distance() int {
|
||||
return i.distance
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package instruction_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction"
|
||||
)
|
||||
|
||||
func TestFromLine(t *testing.T) {
|
||||
expectedDirection := "L"
|
||||
expectedDistance := 7
|
||||
line := fmt.Sprintf("%s%d", expectedDirection, expectedDistance)
|
||||
|
||||
result := instruction.FromLine(line)
|
||||
|
||||
if result.Direction() != expectedDirection {
|
||||
t.Errorf("got direction %s but expected direction %s", result.Direction(), expectedDirection)
|
||||
}
|
||||
|
||||
if result.Distance() != expectedDistance {
|
||||
t.Errorf("got distance %d but expected distance %d", result.Distance(), expectedDistance)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromParts(t *testing.T) {
|
||||
expectedDirection := "L"
|
||||
expectedDistance := 7
|
||||
|
||||
result := instruction.FromParts(expectedDirection, expectedDistance)
|
||||
|
||||
if result.Direction() != expectedDirection {
|
||||
t.Errorf("got direction %s but expected direction %s", result.Direction(), expectedDirection)
|
||||
}
|
||||
|
||||
if result.Distance() != expectedDistance {
|
||||
t.Errorf("got distance %d but expected distance %d", result.Distance(), expectedDistance)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user