Where Two Views Part Ways
I laid out two stereo cards showing the same wire puzzle, then wrote down the moves I thought would free the loop. The left view suggested ABCD; after studying the right, I had ABCE. The interesting point is where those plans stop agreeing.
Their longest common prefix is ABC: the initial run shared by both sequences. A prefix must start at the beginning, so a match later on cannot extend it. That makes it a useful way to compare two recorded solutions up to their first disagreement, before deciding which view needs another look.
fun commonPrefix(a: String, b: String): String {
var i = 0
while (i < minOf(a.length, b.length) && a[i] == b[i]) {
i++
}
return a.substring(0, i)
}
fun main() {
println(commonPrefix("ABCD", "ABCE"))
}
The program prints ABC. It checks corresponding characters from left to right and stops at the first mismatch or the end of the shorter string. For these single-letter move labels, that is enough; real puzzle moves might be better represented as a list of labels, so a multi-character label stays together.
Forth gives us the length first. This version runs in Gforth and treats the labels as ASCII bytes:
: prefix-length { a alen b blen -- n }
0
begin dup alen blen min < while
dup a + c@ over b + c@ <> if exit then
1+
repeat ;
s" ABCD" s" ABCE" prefix-length . cr
It prints 3. Both versions do work proportional to the shared prefix, with one more comparison if they encounter a mismatch. They do not correct for a missing or extra move: once the records diverge, comparison stops. That is a useful limitation for my cards. The result marks the exact point where I need to return to the image pair, rather than quietly treating a later coincidence as agreement.