From bfcad28fff5e8df8e8b9bc7c008a606541a0c507 Mon Sep 17 00:00:00 2001 From: tristan Date: Sat, 9 Dec 2023 15:51:37 +0000 Subject: [PATCH] 2023 day 08 properly --- 2023/08/part2/part2/part2.go | 14 +++++--------- 2023/08/part2/part2/part2_test.go | 12 +++--------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/2023/08/part2/part2/part2.go b/2023/08/part2/part2/part2.go index 59a781c..435361b 100644 --- a/2023/08/part2/part2/part2.go +++ b/2023/08/part2/part2/part2.go @@ -14,7 +14,7 @@ func StepsToSolveAll(ins string, net Network) (int, error) { current := GetStarts(net) res := 1 for _,start := range current { - _, steps, err := StepsToSolve(ins, net, start) + steps, err := StepsToSolve(ins, net, start) if err != nil { return 0, err } @@ -43,10 +43,9 @@ func IsDone(state []string) bool { return true } -func StepsToSolve(ins string, net Network, start string) (int, int, error) { +func StepsToSolve(ins string, net Network, start string) (int, error) { current := start steps := 0; - history := []string{current} for { switch ins[steps % len(ins)] { case 'L': @@ -54,15 +53,12 @@ func StepsToSolve(ins string, net Network, start string) (int, int, error) { case 'R': current = net[current][1] default: - return 0, 0, fmt.Errorf("instructions unclear! Must only be 'R' and 'L', but got '%c' at [%v]", ins[steps], steps) + return 0, fmt.Errorf("instructions unclear! Must only be 'R' and 'L', but got '%c' at [%v]", ins[steps], steps) } steps ++ - for step, state := range history { - if state == current && step == steps % len(ins) { - return step, steps-step, nil - } + if current[2] == goal { + return steps, nil } - history = append(history, current) } } diff --git a/2023/08/part2/part2/part2_test.go b/2023/08/part2/part2/part2_test.go index 3db6fbb..0966fd6 100644 --- a/2023/08/part2/part2/part2_test.go +++ b/2023/08/part2/part2/part2_test.go @@ -55,18 +55,12 @@ func TestStepsToSolve(t *testing.T) { "22Z": [2]string{"22B", "22B"}, "XXX": [2]string{"XXX", "XXX"}, } - start, steps, err := StepsToSolve("RL", net, "11B") + steps, err := StepsToSolve("LR", net, "11A") equal(t, err, nil) - equal(t, start, 0) equal(t, steps, 2) - start, steps, err = StepsToSolve("LR", net, "11A") + steps, err = StepsToSolve("LR", net, "22A") equal(t, err, nil) - equal(t, start, 1) - equal(t, steps, 2) - start, steps, err = StepsToSolve("LR", net, "22A") - equal(t, err, nil) - equal(t, start, 1) - equal(t, steps, 6) + equal(t, steps, 3) } func TestLCM(t *testing.T) {