diff --git a/cmd/01/dial/dial.go b/cmd/01/dial/dial.go index e75f273..a9b69d5 100644 --- a/cmd/01/dial/dial.go +++ b/cmd/01/dial/dial.go @@ -2,35 +2,75 @@ package dial import ( + "fmt" + "github.com/StevanFreeborn/advent-of-code-2025/cmd/01/direction" "github.com/StevanFreeborn/advent-of-code-2025/cmd/01/instruction" ) type Dial interface { + ZeroCount() int Value() int Turn(instruction instruction.Instruction) } type dial struct { - value int + zeroCount int + value int } func New() Dial { return &dial{ - value: 50, + zeroCount: 0, + 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 { return d.value } 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 d.value == 0 { + d.zeroCount-- + } + 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 { + // WE NEVER GET HERE + // UNLESS d.value is -1 or less + currentValue := d.value + d.zeroCount++ d.value += 100 + fmt.Printf("%s zeroCount++ => Correcting %d to %d (%d)\n", logPrefix, currentValue, d.value, d.zeroCount) } return @@ -39,8 +79,16 @@ func (d *dial) Turn(instruction instruction.Instruction) { if instruction.Direction() == direction.Right { d.value += instruction.Distance() + // that here we at 100 or the equilavent + // of 0. this is counted + for d.value > 99 { + // WE NEVER GET HERE + // UNLESS d.value is 100 or more + currentValue := d.value + d.zeroCount++ d.value -= 100 + fmt.Printf("%s zeroCount++ => Correcting %d to %d (%d)\n", logPrefix, currentValue, d.value, d.zeroCount) } return diff --git a/cmd/01/dial/dial_test.go b/cmd/01/dial/dial_test.go index 37c13cf..e50ca8a 100644 --- a/cmd/01/dial/dial_test.go +++ b/cmd/01/dial/dial_test.go @@ -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 { Instruction instruction.Instruction ExpectedDialValue int diff --git a/cmd/01/main.go b/cmd/01/main.go index 2247a83..d064824 100644 --- a/cmd/01/main.go +++ b/cmd/01/main.go @@ -26,3 +26,19 @@ func SolvePartOne(filePath string) int { 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() +} diff --git a/cmd/01/main_test.go b/cmd/01/main_test.go index c69c8f5..25c51ef 100644 --- a/cmd/01/main_test.go +++ b/cmd/01/main_test.go @@ -25,3 +25,29 @@ func TestSolvePartOneWithInput(t *testing.T) { 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) + } +} diff --git a/internal/file/file.go b/internal/file/file.go index ce90c26..b76a3fd 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -3,7 +3,6 @@ package file import ( "bufio" - "fmt" "os" ) @@ -20,7 +19,6 @@ func ReadLines(filePath string) []string { for hasLine := scanner.Scan(); hasLine; hasLine = scanner.Scan() { line := scanner.Text() - fmt.Println(line) lines = append(lines, line) }