2020-12-17 14:51:05 +01:00
pub mod cell ;
pub mod direction ;
pub mod position ;
pub use direction ::* ;
pub use position ::* ;
2020-12-11 10:18:58 +01:00
2020-12-17 14:51:05 +01:00
use itertools ::join ;
use std ::{ collections ::HashMap , fmt ::Display , hash ::BuildHasher } ;
2020-12-17 14:04:05 +01:00
2021-07-08 19:05:31 +02:00
#[ derive(Debug, Clone, PartialEq) ]
2020-12-17 21:30:30 +01:00
pub struct Grid < const D : usize , T : Display + Default > {
pub fields : HashMap < PositionND < D > , T > ,
2020-12-17 14:04:05 +01:00
}
2020-12-17 21:30:30 +01:00
impl < const D : usize , T : Display + Default + Copy > Grid < D , T > {
pub fn get ( & self , pos : & PositionND < D > ) -> T {
2021-07-08 19:35:31 +02:00
self . fields . get ( pos ) . copied ( ) . unwrap_or_else ( T ::default )
2020-12-17 14:04:05 +01:00
}
2020-12-17 21:30:30 +01:00
pub fn insert < Pos : Into < PositionND < D > > > ( & mut self , pos : Pos , t : T ) {
2020-12-17 14:04:05 +01:00
self . fields . insert ( pos . into ( ) , t ) ;
}
}
2020-12-17 21:30:30 +01:00
impl < const D : usize , T : Display + Default > std ::iter ::FromIterator < ( PositionND < D > , T ) > for Grid < D , T > {
fn from_iter < I : IntoIterator < Item = ( PositionND < D > , T ) > > ( iter : I ) -> Self {
2020-12-17 14:13:02 +01:00
Grid {
fields : iter . into_iter ( ) . collect ( ) ,
}
}
}
2020-12-11 10:18:58 +01:00
struct Boundaries {
x_min : i64 ,
x_max : i64 ,
y_min : i64 ,
y_max : i64 ,
}
#[ rustfmt::skip ]
2021-07-08 19:21:07 +02:00
fn get_boundaries ( input : & [ & PositionND < 2 > ] ) -> Boundaries {
let x_min = input . iter ( ) . min_by_key ( | k | k . points [ 0 ] ) . map ( | p | p . points [ 0 ] ) . unwrap_or ( 0 ) ;
let x_max = input . iter ( ) . max_by_key ( | k | k . points [ 0 ] ) . map ( | p | p . points [ 0 ] ) . unwrap_or ( 0 ) ;
let y_min = input . iter ( ) . min_by_key ( | k | k . points [ 1 ] ) . map ( | p | p . points [ 1 ] ) . unwrap_or ( 0 ) ;
let y_max = input . iter ( ) . max_by_key ( | k | k . points [ 1 ] ) . map ( | p | p . points [ 1 ] ) . unwrap_or ( 0 ) ;
2020-12-11 10:18:58 +01:00
Boundaries { x_min , x_max , y_min , y_max }
}
2021-07-08 19:21:07 +02:00
pub fn draw_ascii < T : Display + Default , S : BuildHasher > ( coordinates : & HashMap < PositionND < 2 > , T , S > ) -> String {
2020-12-11 10:18:58 +01:00
let b = get_boundaries ( & coordinates . keys ( ) . collect ::< Vec < _ > > ( ) ) ;
join (
( b . y_min ..= b . y_max ) . rev ( ) . map ( | y | {
( b . x_min ..= b . x_max )
2021-07-08 19:21:07 +02:00
. map ( | x | coordinates . get ( & PositionND { points : [ x , y ] } ) . unwrap_or ( & T ::default ( ) ) . to_string ( ) )
2020-12-11 10:18:58 +01:00
. collect ::< String > ( )
} ) ,
" \n " ,
)
}
#[ cfg(test) ]
mod tests {
use super ::* ;
#[ test ]
fn test_add ( ) {
2021-07-08 19:35:31 +02:00
assert_eq! ( PositionND ::from ( [ 0 , 2 ] ) + PositionND ::from ( [ - 1 , 0 ] ) , [ - 1 , 2 ] . into ( ) ) ;
assert_eq! ( PositionND ::from ( [ 0 , - 1 ] ) + PositionND ::from ( Direction ::Up ) , [ 0 , 0 ] . into ( ) ) ;
2020-12-11 10:18:58 +01:00
}
}