_TITLE "Word ladder v6 b+ mod david_uwi" '2021-09-10 david has already improved his first version! ' ref: https://www.qb64.org/forum/index.php?topic=4157.msg135293#msg135293
' get original version above, I am modifying below to study
' 2021-09-11 in v3 b+ mod david_uwi I modify to compare whole set to my orignal whole set
' 2021-09-11 in v4 I think I can load the words faster
' 2021-09-12 in v5 lets just call the external file once, use david's idea instead of _continue,
' I think it looks cleaner without _continue which tool out skipping a For loop by a THEN (GOTO) line #.
' Ave of 5 runs before this (mod 4) was 11.27.. see if we improve still more, also DefLng A-Z oh wow.
' Ave of 5 runs now 8.74 another 2.5 secs shaved off
' 2021-09-12 in v6 though using asc instead of mid$ might go faster.
' Ave of 5 runs now is 4.45 secs another 4.29 sec off!
' 2021-09-13 v7 b+ mod david_uwi = v6 b+ mod but I will attempt to translate letter variables into
' more meaningful in attempts to understand what is being done in his code.
' Shaved significant time by making store$ (used to be q4) not fixed!
' Yes run a quick check that target word connects to something does save us so much time that
' we get a better overall time for the whole set even though we added a tiny bit of time to
' the ones that do connect.
' Ave of 5 runs is now 2.57 sec another 1.88 secs shaved off.
DEFLNG A
-Z
' 2 secs off from old Single default!
' do just once for all ladder calls
Split buf$
, CHR$(10), Fwords$
()
' test set of ladder calls
ladder "boy", "man" ' quick
ladder "girl", "lady" ' this takes awhile
ladder "john", "jane" ' quick enough
ladder "alien", "drool" ' cool but takes a long long time!
ladder "child", "adult" ' and this takes awhile
ladder "play", "ball" ' goes quick
ladder "fun", "job" ' ditto
PRINT:
PRINT "Total time including one disk file access:";
TIMER(.001) - start!
tt!
= TIMER(.001) 'include time taken to load to RAM bplus mod to accuracy (.001) DIM w
(10000) AS STRING * 5 ' words from file not String * 5 'make sure we have enough storage!! DIM store$
(10000, 100) ' wow went from fixed string! storing connect words to not fixed string and shaved a sec off time!!! FI = 1
IF wordLength
< 5 THEN q1$
= q1$
+ SPACE$(5 - wordLength
): q2$
= q2$
+ SPACE$(5 - wordLength
) maxWordIndex = maxWordIndex + 1
IF Fwords$
(FI
) = q1$
THEN w
(maxWordIndex
) = "*****" ELSE w
(maxWordIndex
) = Fwords$
(FI
) FI = FI + 1
'q2$ needs to have at least one connect or skip to end
'(this block will add a little more time to each ladder but save over a sec on adult or any target word with no connects)
FOR i
= 1 TO maxWordIndex
IF w
(i
) <> q2$
THEN ' just check before entering loop cnt = 0
IF cnt
= 1 THEN ' q2$ has a connect good to go IF targetOK
= 0 THEN PRINT "No path found! from "; q1$;
" to "; q2$;:
GOTO skip
' carry on
jk = 1
storeIndexs(jk) = 1
store$(1, 1) = q1$
FOR i
= 1 TO maxWordIndex
IF w
(i
) <> "*****" THEN '_Continue ' 500 just check before entering loop cnt = 0
FOR kk
= 1 TO storeIndexs
(jk
) cnt = 0
'If Mid$(w(i), j, 1) = Mid$(store$(kk, jk), j, 1) Then cnt = cnt + 1 Else zz = j
storeIndexs(jk + 1) = storeIndexs(jk + 1) + 1
store$(storeIndexs(jk + 1), jk + 1) = w(i)
z4
(storeIndexs
(jk
+ 1), jk
+ 1) = z4
(kk
, jk
) + MID$(w
(i
), zz
, 1) + CHR$(zz
+ 48) + " " 'stores a letter and change position w(i) = "*****"
kflag = 0
'*****new routine!!!
cnu = 0
FOR i
= 1 TO storeIndexs
(jk
+ 1) cnu = 0
IF ASC(store$
(i
, jk
+ 1), iq
) = ASC(q2$
, iq
) THEN cnu
= cnu
+ 1 IF cnu
= wordLength
- 1 THEN kflag
= 99: final$
= z4
(i
, jk
+ 1) '*********
IF storeIndexs
(jk
+ 1) = 0 THEN kflag
= 99 jk = jk + 1
IF storeIndexs
(jk
) = 0 THEN PRINT "No path found! from "; q1$;
" to "; q2$;
' b+ removed a print blank line 'Print:
c2$
= MID$(final$
, i
+ 1, 1) skip:
curpos
= 1: arrpos
= LBOUND(loadMeArray
): LD
= LEN(delim
) dpos
= INSTR(curpos
, SplitMeString
, delim
) loadMeArray
(arrpos
) = MID$(SplitMeString
, curpos
, dpos
- curpos
) arrpos = arrpos + 1
curpos = dpos + LD
dpos
= INSTR(curpos
, SplitMeString
, delim
) loadMeArray
(arrpos
) = MID$(SplitMeString
, curpos
)