Find the shortest way to advance a counter to a certain number
Lua, 327763 steps (optimum, 276 bytes)
Golfed version:
a={[0]=""}t=tonumber for i=0,52 do A={}for k,v in pairs(a)do A[k]=v L=("%04d"):format(k)for i=1,4 do c=L:sub(i,i)d=L:gsub(c,(t(c)+1)%10)e=a[t(d)]A[d]=(not e or #e>#v)and v..c or e end b=k+1 if k<9999then e=a[b]A[b]=(not e or #e>#v)and v.."C"or e end end a=A endprint(a[(...)])
Improved version of the examples in question (only 1000
is improved):
0001:C
0093:CCCCCCCCCC12345678CCC
1000:0CCCCCCCCCCC2345678C23456789
(0000>1111>1122>1199>1200>1000)
9999:012345678
Ungolfed version:
a = {[0]=""}
for i=0,52 do
A = {}
for k,v in pairs(a) do
A[k] = v
L=("%04d"):format(k)
for i=1,4 do
c=L:sub(i,i)
d=L:gsub(c,(tonumber(c)+1)%10)
e=a[tonumber(d)]
A[d] = (not e or #e > #v) and v..c or e
end
b=k+1
if k < 9999 then
e=a[b]
A[b] = (not e or #e > #v) and v.."C" or e
end
end
a=A
end
print(a[93],a[1000],a[9999])