11use pyo3:: { pyfunction, PyResult } ;
2+ use std:: convert:: TryInto ;
3+
24pub ( crate ) struct Puzzle {
3- pub puzz : Vec < u8 > ,
5+ pub puzz : [ u8 ; 81 ] ,
46 pub blank_positions : Vec < u8 > ,
57 pub possibilities : Vec < Vec < u8 > > ,
68 pub cached_possibilities : Vec < [ bool ; 10 ] > ,
@@ -24,9 +26,10 @@ fn no_repeats(items: Vec<u8>) -> bool {
2426
2527#[ pyfunction]
2628pub ( crate ) fn is_valid ( puzzle : Vec < u8 > ) -> PyResult < bool > {
27- if puzzle. len ( ) != 81 {
28- return Err ( pyo3:: exceptions:: PyValueError :: new_err ( "A puzzle must have a length of 81!" ) ) ;
29- }
29+ backend_is_valid ( puzzle. try_into ( ) . expect ( "A puzzle must have a length of 81!" ) )
30+ }
31+
32+ pub ( crate ) fn backend_is_valid ( puzzle : [ u8 ; 81 ] ) -> PyResult < bool > {
3033
3134 for i in 0 ..9 { //check rows
3235 if !no_repeats ( Vec :: from ( & puzzle[ i * 9 ..( i + 1 ) * 9 ] ) ) {
@@ -67,7 +70,7 @@ pub(crate) fn is_valid(puzzle: Vec<u8>) -> PyResult<bool> {
6770 Ok ( true )
6871}
6972
70- pub ( crate ) fn get_possibilities ( puzz : & Vec < u8 > , pos : u8 ) -> Vec < u8 > {
73+ pub ( crate ) fn get_possibilities ( puzz : & [ u8 ; 81 ] , pos : u8 ) -> Vec < u8 > {
7174 let mut possibilities: Vec < u8 > = Vec :: new ( ) ;
7275 let mut seen: [ bool ; 10 ] = [ false ; 10 ] ;
7376 let row: usize = ( pos / 9 ) as usize ;
@@ -92,7 +95,7 @@ pub(crate) fn get_possibilities(puzz: &Vec<u8>, pos: u8) -> Vec<u8> {
9295 }
9396 possibilities
9497}
95- fn solver_prep ( puzz : Vec < u8 > ) -> Puzzle {
98+ fn solver_prep ( puzz : [ u8 ; 81 ] ) -> Puzzle {
9699 let mut p: Puzzle = Puzzle {
97100 puzz,
98101 blank_positions : Vec :: new ( ) ,
@@ -143,7 +146,7 @@ fn solver_prep(puzz: Vec<u8>) -> Puzzle {
143146 p
144147}
145148
146- pub ( crate ) fn get_possibilities_as_array ( puzz : & Vec < u8 > , pos : usize ) -> [ bool ; 10 ] {
149+ pub ( crate ) fn get_possibilities_as_array ( puzz : & [ u8 ; 81 ] , pos : usize ) -> [ bool ; 10 ] {
147150 let mut seen: [ bool ; 10 ] = [ true ; 10 ] ;
148151 let row: usize = pos / 9 ;
149152 let col: usize = pos % 9 ;
@@ -161,18 +164,20 @@ pub(crate) fn get_possibilities_as_array(puzz: &Vec<u8>, pos: usize) -> [bool; 1
161164 }
162165 seen
163166}
167+
164168#[ pyfunction]
165- pub fn solve ( puzz : Vec < u8 > ) -> PyResult < Vec < u8 > > {
166- if puzz. len ( ) != 81 {
167- return Err ( pyo3:: exceptions:: PyValueError :: new_err ( "Your puzzle must have a length of 81!" ) ) ;
168- }
169+ pub fn solve ( puzz : Vec < u8 > ) -> PyResult < [ u8 ; 81 ] > {
170+ backend_solve ( puzz. try_into ( ) . expect ( "A puzzle must have a length of 81!" ) )
171+ }
172+
173+ pub fn backend_solve ( puzz : [ u8 ; 81 ] ) -> PyResult < [ u8 ; 81 ] > {
169174
170175 let mut p = solver_prep ( puzz) ;
171176 if p. solved {
172177 return Ok ( p. puzz ) ;
173178 }
174179
175- if !is_valid ( p. puzz . clone ( ) ) ? {
180+ if !backend_is_valid ( p. puzz . clone ( ) ) ? {
176181 return Err ( pyo3:: exceptions:: PyValueError :: new_err ( "The puzzle is illegal!" ) ) ;
177182 }
178183
@@ -221,7 +226,7 @@ pub fn solve(puzz: Vec<u8>) -> PyResult<Vec<u8>> {
221226 }
222227}
223228
224- pub async fn async_solve ( puzz : Vec < u8 > ) -> PyResult < Vec < u8 > > {
225- solve ( puzz)
229+ pub async fn async_solve ( puzz : [ u8 ; 81 ] ) -> PyResult < [ u8 ; 81 ] > {
230+ backend_solve ( puzz)
226231}
227232
0 commit comments