The shortest length paths for goals 1 through 6 have the lengths 7, 8, 7, 8, 9 and 10 respectively.
The following paths show how it can be done for the indicated goal squares:
RRRRDRU 1
RRDRURR 1
DRURRRR 1
DRRRRRU 1
RRRDRURD 2
RRRDRDRU 2
RRDRURDR 2
RDRURDRR 2
RDRRRURD 2
RDRRRDRU 2
RDRDRURR 2
DRURRRDR 2
DRURDRRR 2
DRRRDRUR 2
RRDRRRD 3
DRRRDRR 3
RDRDRRRD 4
DRRRDRDR 4
RRRRDRDDD 5
RRRRDDDRD 5
RRDRDRRDD 5
RRDRDDDRR 5
RRDDRRDRD 5
RRDDDRDRR 5
RDDRDRRRD 5
DRRRRRDDD 5
DRRRRDDRD 5
DRRRDRDDR 5
DRRRDDRRD 5
DRRDDRRRD 5
DRDRRRRDD 5
DRDRRDDRR 5
DRDDRRRRD 5
DRDDDRRRR 5
DDRRRRDRD 5
DDRRDRDRR 5
DDDRRRRRD 5
DDDRDRRRR 5
RRRDRDRDDD 6
RRRDRDDDRD 6
RRDRDRDRDD 6
RRDRDDDRDR 6
RRDDRDRDRD 6
RRDDDRDRDR 6
RDRRRDRDDD 6
RDRRRDDDRD 6
RDRDRDRRDD 6
RDRDRDDDRR 6
RDRDDRRDRD 6
RDRDDDRDRR 6
RDDDRDRRRD 6
DRRRDRDDDR 6
DRDRRRDRDD 6
DRDRRDDRDR 6
DRDRDRRRDD 6
DRDRDRDDRR 6
DRDDDRRRDR 6
DRDDDRDRRR 6
DDRRRDRDRD 6
DDRRDRDRDR 6
DDRDRRRDRD 6
DDRDRDRDRR 6
DDDRDRRRDR 6
DDDRDRDRRR 6
for the bonus:
RRRDRURD 2
RRDRURDR 2
RDRURDRR 2
RDRRRDRU 2
DRURDRRR 2
DRRRDRUR 2
RRRDLDRRRD 4
RRRDDDRDRU 4
RRDLDRRRDR 4
RRDRRRDLDR 4
RRDRDRURDD 4
RRDDRURDRD 4
RRDDDRDRUR 4
RDLDRRRDRR 4
RDRURRRDDD 4
RDRURDDRRD 4
RDRRRDLDRR 4
RDRRDDRURD 4
RDRDRURDDR 4
RDRDRRDDRU 4
RDRDDDRRRU 4
RDDRURDRDR 4
RDDRRDRDRU 4
RDDDRRRURD 4
RDDDRDRURR 4
DRURRRDDDR 4
DRURDRDRRD 4
DRURDDRRDR 4
DRRRURDRDD 4
DRRRDLDRRR 4
DRRDRDRURD 4
DRRDDRURDR 4
DRDRURRRDD 4
DRDRURDDRR 4
DRDRRDDRUR 4
DRDDDRRRUR 4
DDRURDRDRR 4
DDRRRURDRD 4
DDRRDRDRUR 4
DDRDRURRRD 4
DDDRRRURDR 4
DDDRDRURRR 4
RRRDRDDDRD 6
RRDRDDDRDR 6
RDRRRDRDDD 6
RDRDDDRDRR 6
RDDDRDRRRD 6
DRRRDRDDDR 6
DRDRRRDRDD 6
DRDDDRDRRR 6
DDRDRRRDRD 6
DDDRDRRRDR 6
For goals 2 and 6, these are a subset of the respective solutions to the main question.
For goal 4, the bonus requires more steps (10) than for the main question (8).
The bonus seems impossible for odd numbered goals.
DECLARE SUB moveIt ()
DECLARE FUNCTION pathPiece$ (o$, r!, c!)
CLEAR , , 25000
DIM SHARED orient$, row, col, goalRow, mvHist$, minMoves, path$
DIM SHARED minM$(6)
CLS
FOR goalRow = 1 TO 6
orient$ = "t": row = 1: col = 1
path$ = pathPiece$(orient$, row, col)
minMoves = 30
moveIt
DO: LOOP UNTIL INKEY$ > ""
NEXT
FOR i = 1 TO 6
PRINT minM$(i)
NEXT
SUB moveIt
FOR dr = -1 TO 1
FOR dc = -1 TO 1
saveOrient$ = orient$
saveRow = row: saveCol = col
IF (dr = 0 OR dc = 0) AND (ABS(dr) = 1 OR ABS(dc) = 1) THEN
newRow = row + dr: newCol = col + dc
IF newRow > 0 AND newRow <= 6 THEN
IF newCol > 0 AND newCol <= 6 THEN
ho = INSTR("wteb", orient$)
IF ho > 0 AND ABS(dc) = 1 THEN
ho = ho - dc: IF ho < 1 THEN ho = ho + 4: ELSE IF ho > 4 THEN ho = ho - 4
orient$ = MID$("wteb", ho, 1)
END IF
vo = INSTR("ntsb", orient$)
IF vo > 0 AND ABS(dr) = 1 THEN
vo = vo - dr: IF vo < 1 THEN vo = vo + 4: ELSE IF vo > 4 THEN vo = vo - 4
orient$ = MID$("ntsb", vo, 1)
END IF
code$ = pathPiece$(orient$, newRow, newCol)
IF INSTR(path$, code$) = 0 AND LEN(path$) <= minMoves THEN
path$ = path$ + code$
row = newRow: col = newCol
drct = 2 * dr + dc + 3
direct$ = MID$("UL RD", drct, 1)
mvHist$ = mvHist$ + direct$
IF col = 6 AND row = goalRow AND orient$ = "t" THEN
minMoves = LEN(path$)
minM$(goalRow) = mvHist$
PRINT mvHist$, goalRow
ELSE
moveIt
END IF
mvHist$ = LEFT$(mvHist$, LEN(mvHist$) - 1)
path$ = LEFT$(path$, LEN(path$) - 1)
END IF
END IF
END IF
END IF
row = saveRow: col = saveCol
orient$ = saveOrient$
NEXT
NEXT
END SUB
FUNCTION pathPiece$ (o$, r, c)
n = ((r - 1) * 6 + c - 1) * 6 + INSTR("tbnsew", o$)
pathPiece$ = CHR$(n)
END FUNCTION
for the bonus:
DECLARE FUNCTION pathPiece$ (o$, o2$, r!, c!)
DECLARE SUB moveIt ()
CLEAR , , 25000
DIM SHARED orient$, orient2$, row, col, goalRow, mvHist$, minMoves, path$
DIM SHARED minM$(6)
CLS
FOR goalRow = 1 TO 6
orient$ = "t": orient2$ = "n": row = 1: col = 1
path$ = pathPiece$(orient$, orient2$, row, col)
minMoves = 17
moveIt
PRINT goalRow; "end"
DO: LOOP UNTIL INKEY$ > ""
NEXT
FOR i = 1 TO 6
PRINT minM$(i)
NEXT
SUB moveIt
FOR dr = -1 TO 1
FOR dc = -1 TO 1
saveOrient$ = orient$
saveOrient2$ = orient2$
saveRow = row: saveCol = col
IF (dr = 0 OR dc = 0) AND (ABS(dr) = 1 OR ABS(dc) = 1) THEN
newRow = row + dr: newCol = col + dc
IF newRow > 0 AND newRow <= 6 THEN
IF newCol > 0 AND newCol <= 6 THEN
ho = INSTR("betw", orient$)
IF ho > 0 AND ABS(dc) = 1 THEN
ho = ho - dc: IF ho < 1 THEN ho = ho + 4: ELSE IF ho > 4 THEN ho = ho - 4
orient$ = MID$("betw", ho, 1)
END IF
ho = INSTR("betw", orient2$)
IF ho > 0 AND ABS(dc) = 1 THEN
ho = ho - dc: IF ho < 1 THEN ho = ho + 4: ELSE IF ho > 4 THEN ho = ho - 4
orient2$ = MID$("betw", ho, 1)
END IF
vo = INSTR("bstn", orient$)
IF vo > 0 AND ABS(dr) = 1 THEN
vo = vo - dr: IF vo < 1 THEN vo = vo + 4: ELSE IF vo > 4 THEN vo = vo - 4
orient$ = MID$("bstn", vo, 1)
END IF
vo = INSTR("bstn", orient2$)
IF vo > 0 AND ABS(dr) = 1 THEN
vo = vo - dr: IF vo < 1 THEN vo = vo + 4: ELSE IF vo > 4 THEN vo = vo - 4
orient2$ = MID$("bstn", vo, 1)
END IF
code$ = pathPiece$(orient$, orient2$, newRow, newCol)
IF INSTR(path$, code$) = 0 AND LEN(path$) / 2 <= minMoves THEN
path$ = path$ + code$
row = newRow: col = newCol
drct = 2 * dr + dc + 3
direct$ = MID$("UL RD", drct, 1)
mvHist$ = mvHist$ + direct$
IF col = 6 AND row = goalRow AND orient$ = "t" AND orient2$ = "n" THEN
minMoves = LEN(path$) / 2
minM$(goalRow) = mvHist$
PRINT mvHist$, goalRow
ELSE
moveIt
END IF
mvHist$ = LEFT$(mvHist$, LEN(mvHist$) - 1)
path$ = LEFT$(path$, LEN(path$) - 2)
END IF
END IF
END IF
END IF
row = saveRow: col = saveCol
orient$ = saveOrient$
orient2$ = saveOrient2$
NEXT
NEXT
END SUB
FUNCTION pathPiece$ (o$, o2$, r, c)
n = ((r - 1) * 6 + c - 1) * 6 + INSTR("tbnsew", o$) + 5
pathPiece$ = CHR$(n) + CHR$(INSTR("tbnsew", o2$) - 1)
END FUNCTION
Based on Enigma No. 1565, "It's a rollover", by Bob Walker, New Scientist, 3 October 2009, page 28. |