@@ -59,6 +59,8 @@ impl Dominoes {
59
59
}
60
60
let results: Vec < Option < Self > > = neighbours
61
61
. into_iter ( )
62
+ // Recurse the list of dominoes until there is no more
63
+ // left to play
62
64
. map ( |neighbour| {
63
65
let mut next_domino = available[ neighbour] ;
64
66
if current_dom. 1 != next_domino. 0 {
@@ -68,23 +70,25 @@ impl Dominoes {
68
70
available,
69
71
allocated,
70
72
} = Self :: play_domino ( & next_domino, & available, & allocated) ;
71
- let tmp = Self :: sequence_helper ( next_domino, available, allocated) ;
72
- tmp
73
+ Self :: sequence_helper ( next_domino, available, allocated)
73
74
} )
74
- . collect ( ) ;
75
- let new_results: Vec < Option < Self > > = results
76
- . iter ( )
77
- . cloned ( )
75
+ // Remove non-solutions
78
76
. filter ( |result| match result {
79
77
Some ( value) => value. available . is_empty ( ) ,
80
78
None => false ,
81
79
} )
82
80
. collect ( ) ;
83
- match new_results. get ( 0 ) {
81
+ // Return the first solution
82
+ match results. get ( 0 ) {
84
83
None => None ,
85
84
Some ( value) => Some ( value. clone ( ) . unwrap ( ) ) ,
86
85
}
87
86
}
87
+ /**
88
+ * Returns a list of neighbours for the given domino.
89
+ * Assumes that the given domino has already been removed
90
+ * from the list of candidates.
91
+ */
88
92
fn find_neighbours ( available : & [ Domino ] , dots : usize ) -> Vec < usize > {
89
93
let mut matches = vec ! [ ] ;
90
94
for ( index, domino) in available. iter ( ) . enumerate ( ) {
@@ -96,29 +100,26 @@ impl Dominoes {
96
100
matches
97
101
}
98
102
/**
99
- * Returns a list of neighbours for the given domino.
100
- * Assumes that the given domino has already been removed
101
- * from the list of candidates.
103
+ * Moves the given domino from available to allocated resource.
102
104
*/
103
105
fn play_domino ( domino : & Domino , available : & [ Domino ] , allocated : & [ Domino ] ) -> Self {
104
- let mut new_allocated = allocated. to_vec ( ) ;
105
- new_allocated . push ( * domino) ;
106
+ let mut allocated = allocated. to_vec ( ) ;
107
+ allocated . push ( * domino) ;
106
108
let reversed_dom = Self :: reverse_domino ( * domino) ;
107
109
let position = available
108
110
. iter ( )
109
111
. position ( |& dom| dom == * domino || dom == reversed_dom)
110
112
. unwrap ( ) ;
111
- let new_available = available
113
+ let available = available
112
114
. iter ( )
113
115
. enumerate ( )
114
116
. filter_map ( |( index, & value) | if index != position { Some ( value) } else { None } )
115
117
. collect ( ) ;
116
118
Dominoes {
117
- available : new_available ,
118
- allocated : new_allocated ,
119
+ available,
120
+ allocated,
119
121
}
120
122
}
121
-
122
123
fn start_and_end_dots_are_equal ( & self ) -> bool {
123
124
let allocation = self . allocated . to_vec ( ) ;
124
125
let first_dom = allocation. first ( ) . unwrap ( ) ;
0 commit comments