2025-12-01 06:49:26 -06:00
|
|
|
// Package dial provides a model for representing a dial
|
|
|
|
|
package dial
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-02 07:18:38 -06:00
|
|
|
"fmt"
|
|
|
|
|
|
2025-12-01 06:49:26 -06:00
|
|
|
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/direction"
|
|
|
|
|
"github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Dial interface {
|
2025-12-02 07:18:38 -06:00
|
|
|
ZeroCount() int
|
2025-12-01 06:49:26 -06:00
|
|
|
Value() int
|
|
|
|
|
Turn(instruction instruction.Instruction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type dial struct {
|
2025-12-02 07:18:38 -06:00
|
|
|
zeroCount int
|
|
|
|
|
value int
|
2025-12-01 06:49:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New() Dial {
|
|
|
|
|
return &dial{
|
2025-12-02 07:18:38 -06:00
|
|
|
zeroCount: 0,
|
|
|
|
|
value: 50,
|
2025-12-01 06:49:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 07:18:38 -06:00
|
|
|
func From(value int) Dial {
|
|
|
|
|
return &dial{
|
|
|
|
|
zeroCount: 0,
|
|
|
|
|
value: value,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *dial) ZeroCount() int {
|
|
|
|
|
return d.zeroCount
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 06:49:26 -06:00
|
|
|
func (d *dial) Value() int {
|
|
|
|
|
return d.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *dial) Turn(instruction instruction.Instruction) {
|
2025-12-02 07:18:38 -06:00
|
|
|
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
|
|
|
|
|
|
2025-12-01 06:49:26 -06:00
|
|
|
if instruction.Direction() == direction.Left {
|
2025-12-02 07:18:38 -06:00
|
|
|
if d.value == 0 {
|
|
|
|
|
d.zeroCount--
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 06:49:26 -06:00
|
|
|
d.value -= instruction.Distance()
|
|
|
|
|
|
2025-12-02 07:18:38 -06:00
|
|
|
// that here we are at 0
|
|
|
|
|
|
|
|
|
|
if d.value == 0 {
|
|
|
|
|
fmt.Printf("%s zeroCount++\n", logPrefix)
|
|
|
|
|
d.zeroCount++
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 06:49:26 -06:00
|
|
|
for d.value < 0 {
|
2025-12-02 07:18:38 -06:00
|
|
|
// WE NEVER GET HERE
|
|
|
|
|
// UNLESS d.value is -1 or less
|
|
|
|
|
currentValue := d.value
|
|
|
|
|
d.zeroCount++
|
2025-12-01 06:49:26 -06:00
|
|
|
d.value += 100
|
2025-12-02 07:18:38 -06:00
|
|
|
fmt.Printf("%s zeroCount++ => Correcting %d to %d (%d)\n", logPrefix, currentValue, d.value, d.zeroCount)
|
2025-12-01 06:49:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if instruction.Direction() == direction.Right {
|
|
|
|
|
d.value += instruction.Distance()
|
|
|
|
|
|
2025-12-02 07:18:38 -06:00
|
|
|
// that here we at 100 or the equilavent
|
|
|
|
|
// of 0. this is counted
|
|
|
|
|
|
2025-12-01 06:49:26 -06:00
|
|
|
for d.value > 99 {
|
2025-12-02 07:18:38 -06:00
|
|
|
// WE NEVER GET HERE
|
|
|
|
|
// UNLESS d.value is 100 or more
|
|
|
|
|
currentValue := d.value
|
|
|
|
|
d.zeroCount++
|
2025-12-01 06:49:26 -06:00
|
|
|
d.value -= 100
|
2025-12-02 07:18:38 -06:00
|
|
|
fmt.Printf("%s zeroCount++ => Correcting %d to %d (%d)\n", logPrefix, currentValue, d.value, d.zeroCount)
|
2025-12-01 06:49:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|