feat: ugh...this shouldn't be that hard

This commit is contained in:
Stevan Freeborn
2025-12-02 07:18:38 -06:00
parent e7c180de4b
commit d61a630607
5 changed files with 111 additions and 4 deletions
+48
View File
@@ -2,35 +2,75 @@
package dial package dial
import ( import (
"fmt"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/direction" "github.com/StevanFreeborn/advent-of-code-2025/cmd/01/direction"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction" "github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction"
) )
type Dial interface { type Dial interface {
ZeroCount() int
Value() int Value() int
Turn(instruction instruction.Instruction) Turn(instruction instruction.Instruction)
} }
type dial struct { type dial struct {
zeroCount int
value int value int
} }
func New() Dial { func New() Dial {
return &dial{ return &dial{
zeroCount: 0,
value: 50, value: 50,
} }
} }
func From(value int) Dial {
return &dial{
zeroCount: 0,
value: value,
}
}
func (d *dial) ZeroCount() int {
return d.zeroCount
}
func (d *dial) Value() int { func (d *dial) Value() int {
return d.value return d.value
} }
func (d *dial) Turn(instruction instruction.Instruction) { func (d *dial) Turn(instruction instruction.Instruction) {
logPrefix := fmt.Sprintf("[TURN %s %d]", instruction.Direction(), instruction.Distance())
if d.value < 0 || d.value > 99 {
panic("NEVER SHALL YOU PASS!!!")
}
// d.value is always 0 to 99
if instruction.Direction() == direction.Left { if instruction.Direction() == direction.Left {
if d.value == 0 {
d.zeroCount--
}
d.value -= instruction.Distance() d.value -= instruction.Distance()
// that here we are at 0
if d.value == 0 {
fmt.Printf("%s zeroCount++\n", logPrefix)
d.zeroCount++
}
for d.value < 0 { for d.value < 0 {
// WE NEVER GET HERE
// UNLESS d.value is -1 or less
currentValue := d.value
d.zeroCount++
d.value += 100 d.value += 100
fmt.Printf("%s zeroCount++ => Correcting %d to %d (%d)\n", logPrefix, currentValue, d.value, d.zeroCount)
} }
return return
@@ -39,8 +79,16 @@ func (d *dial) Turn(instruction instruction.Instruction) {
if instruction.Direction() == direction.Right { if instruction.Direction() == direction.Right {
d.value += instruction.Distance() d.value += instruction.Distance()
// that here we at 100 or the equilavent
// of 0. this is counted
for d.value > 99 { for d.value > 99 {
// WE NEVER GET HERE
// UNLESS d.value is 100 or more
currentValue := d.value
d.zeroCount++
d.value -= 100 d.value -= 100
fmt.Printf("%s zeroCount++ => Correcting %d to %d (%d)\n", logPrefix, currentValue, d.value, d.zeroCount)
} }
return return
+19
View File
@@ -16,6 +16,25 @@ func TestNew(t *testing.T) {
} }
} }
func TestProblem(t *testing.T) {
instructions := []instruction.Instruction{
instruction.FromLine("R48"),
instruction.FromLine("L5"),
}
dial := dial.From(52)
for _, i := range instructions {
dial.Turn(i)
}
result := dial.ZeroCount()
if result != 1 {
t.Errorf("ya done fucked up. you wanted %d but got %d", 1, result)
}
}
type TurnTestCase struct { type TurnTestCase struct {
Instruction instruction.Instruction Instruction instruction.Instruction
ExpectedDialValue int ExpectedDialValue int
+16
View File
@@ -26,3 +26,19 @@ func SolvePartOne(filePath string) int {
return total return total
} }
func SolvePartTwo(filePath string) int {
lines := file.ReadLines(filePath)
dial := dial.New()
for _, line := range lines {
if line == "" {
continue
}
instruction := instruction.FromLine(line)
dial.Turn(instruction)
}
return dial.ZeroCount()
}
+26
View File
@@ -25,3 +25,29 @@ func TestSolvePartOneWithInput(t *testing.T) {
t.Errorf(`SolvePartOne("INPUT.txt") = %d; want %d`, result, expected) t.Errorf(`SolvePartOne("INPUT.txt") = %d; want %d`, result, expected)
} }
} }
func TestSolvePartTwoWithExampleInput(t *testing.T) {
expected := 6
result := solution.SolvePartTwo("EXAMPLE.txt")
if result != expected {
t.Errorf(`SolvePartTwo("EXAMPLE.txt") = %d; want %d`, result, expected)
}
}
func TestSolvePartTwoWithInput(t *testing.T) {
expected := -1
tooLow := 5849
tooHigh := 5967
result := solution.SolvePartTwo("INPUT.txt")
if result <= tooLow || result >= tooHigh {
t.Errorf(`SolvePartTwo("INPUT.txt") = %d; want something between %d and %d`, result, tooLow, tooHigh)
}
if result != expected {
t.Errorf(`SolvePartTwo("INPUT.txt") = %d; want %d`, result, expected)
}
}
-2
View File
@@ -3,7 +3,6 @@ package file
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
) )
@@ -20,7 +19,6 @@ func ReadLines(filePath string) []string {
for hasLine := scanner.Scan(); hasLine; hasLine = scanner.Scan() { for hasLine := scanner.Scan(); hasLine; hasLine = scanner.Scan() {
line := scanner.Text() line := scanner.Text()
fmt.Println(line)
lines = append(lines, line) lines = append(lines, line)
} }