Paste: too many CONSTANTS
Author: | andy |
Mode: | factor |
Date: | Thu, 12 Sep 2024 17:35:13 |
Plain Text |
USING: literals sequences.product math.combinatorics math.vectors sets make path-finding ;
CONSTANT: CHESS-INDICES $[
7 [0..b] dup 2array <product-sequence>
]
CONSTANT: KNIGHT-MOVES $[
{ 1 -1 } 2 all-selections
[ { 2 1 } v* ] map
dup [ reverse ] map append
]
: knight-neighbors ( pair -- neighbor-pairs )
KNIGHT-MOVES [ v+ ] with map
CHESS-INDICES intersect ;
CONSTANT: ALL-KNIGHT-NEIGHBORS $[
[
CHESS-INDICES [ [ knight-neighbors ] keep ,, ] each
] H{ } make
]
CONSTANT: KNIGHT-BFS $[
ALL-KNIGHT-NEIGHBORS <bfs>
]
: pos>row-col ( str -- pair )
[ last 49 - ] [ first 97 - ] bi 2array ;
: knight-min-moves ( startpos endpos -- n )
[ pos>row-col ] bi@
KNIGHT-BFS find-path
length 1 - ;
Author: | Andy |
Mode: | factor |
Date: | Thu, 12 Sep 2024 17:44:57 |
Plain Text |
USING: literals sequences.product math.combinatorics math.vectors sets make path-finding ;
<<
CONSTANT: CHESS-INDICES $[
7 [0..b] dup 2array <product-sequence>
]
>>
CONSTANT: KNIGHT-MOVES $[
{ 1 -1 } 2 all-selections
[ { 2 1 } v* ] map
dup [ reverse ] map append
]
<<
: knight-neighbors ( pair -- neighbor-pairs )
KNIGHT-MOVES [ v+ ] with map
CHESS-INDICES intersect ;
>>
<<
CONSTANT: ALL-KNIGHT-NEIGHBORS $[
[
CHESS-INDICES [ [ knight-neighbors ] keep ,, ] each
] H{ } make
]
>>
CONSTANT: KNIGHT-BFS $[
ALL-KNIGHT-NEIGHBORS <bfs>
]
: pos>row-col ( str -- pair )
[ last 49 - ] [ first 97 - ] bi 2array ;
: knight-min-moves ( startpos endpos -- n )
[ pos>row-col ] bi@
KNIGHT-BFS find-path
length 1 - ;
New Annotation