11use crate :: directional_keypad:: { self , DirectionalKeypad } ;
22use crate :: keypad:: Keypad ;
3- use itertools:: Itertools as _;
43use pathfinding:: prelude:: dijkstra;
54use std:: collections:: HashMap ;
65use std:: hash:: Hash ;
@@ -17,8 +16,10 @@ impl<KP: Keypad> PrecomputedCosts<KP> {
1716 pub fn new ( keypad : & KP , costs : & dyn Costs < KP > ) -> Self {
1817 PrecomputedCosts (
1918 enum_iterator:: all :: < KP :: Key > ( )
20- . cartesian_product ( enum_iterator:: all :: < KP :: Key > ( ) )
21- . map ( |( source, target) | ( ( source, target) , costs. cost ( keypad, source, target) ) )
19+ . flat_map ( |source| {
20+ enum_iterator:: all :: < KP :: Key > ( )
21+ . map ( move |target| ( ( source, target) , costs. cost ( keypad, source, target) ) )
22+ } )
2223 . collect ( ) ,
2324 )
2425 }
@@ -46,7 +47,7 @@ impl<KP: Keypad> Costs<KP> for Human {
4647
4748#[ derive( Debug , Clone , Copy ) ]
4849pub struct Robot < C : Costs < DirectionalKeypad > > {
49- parent : C ,
50+ operator : C ,
5051}
5152
5253#[ derive( Debug , PartialEq , Eq , Hash , Clone , Copy ) ]
@@ -56,8 +57,8 @@ struct State<Key> {
5657}
5758
5859impl < C : Costs < DirectionalKeypad > > Robot < C > {
59- pub fn new ( parent : C ) -> Self {
60- Self { parent }
60+ pub fn new ( operator : C ) -> Self {
61+ Self { operator }
6162 }
6263
6364 // Next states and the costs to get there.
@@ -84,7 +85,7 @@ impl<C: Costs<DirectionalKeypad>> Robot<C> {
8485 parent_key : new_parent_key,
8586 key,
8687 } ,
87- self . parent
88+ self . operator
8889 . cost ( & DirectionalKeypad , parent_key, new_parent_key) ,
8990 ) ) ;
9091 }
@@ -103,11 +104,11 @@ impl<KP: Keypad, C: Costs<DirectionalKeypad>> Costs<KP> for Robot<C> {
103104 // Find the cost of the shortest path to the state where the
104105 // robot is hovering over `target`.
105106 let start: State < <KP as Keypad >:: Key > = State {
106- parent_key : directional_keypad :: Key :: A ,
107+ parent_key : DirectionalKeypad :: ACTIVATE ,
107108 key : source,
108109 } ;
109110 let target: State < <KP as Keypad >:: Key > = State {
110- parent_key : directional_keypad :: Key :: A ,
111+ parent_key : DirectionalKeypad :: ACTIVATE ,
111112 key : target,
112113 } ;
113114 let ( _, cost) = dijkstra (
0 commit comments