import { ROWS , COLUMNS } from "./config" ; import { characterData } from "./character-data" ; const groupMap = characterData . map ( ( e ) => [ e . name , e . group ] ) . reduce ( ( obj , [ key , val ] ) => { obj [ key ] = val ; return obj ; } , { } as { [ name ] : string ; } ) ; const groupMembers = characterData . reduce ( ( acc , curr ) => { return { ... acc , [ curr . group ] : acc [ curr . group ] ? [ ... acc [ curr . group ] , curr . name ] : [ curr . name ] , } ; } , { } as { [ key ] : string [ ] ; } ) ; export const findClearPieces = ( pieces ) => { const chunks = Array ( ROWS ) . fill ( null ) . map ( ( ) => [ ... Array ( COLUMNS ) . fill ( null ) ] ) ; pieces . forEach ( ( row , y ) => { row . forEach ( ( group , x ) => { if ( group === null ) return ; const visited = Array ( ROWS ) . fill ( null ) . map ( ( ) => [ ... Array ( COLUMNS ) . fill ( false ) ] ) ; const queue = [ [ x , y ] ] ; const ret = [ ] ; while ( queue . length > 0 ) { const [ x , y ] = queue . pop ( ) ! ; if ( x === undefined || y === undefined || y > pieces . length || x > row . length || visited [ y ] [ x ] ) continue ; ret . push ( [ x , y ] ) ; if ( x - 1 >= 0 && ( groupMap [ pieces [ y ] [ x - 1 ] ?? "" ] === groupMap [ group ] || pieces [ y ] [ x - 1 ] === "Item" ) ) queue . push ( [ x - 1 , y ] ) ; if ( y - 1 >= 0 && ( groupMap [ pieces [ y - 1 ] [ x ] ?? "" ] === groupMap [ group ] || pieces [ y - 1 ] [ x ] === "Item" ) ) queue . push ( [ x , y - 1 ] ) ; if ( x + 1 < row . length && ( groupMap [ pieces [ y ] [ x + 1 ] ?? "" ] === groupMap [ group ] || pieces [ y ] [ x + 1 ] === "Item" ) ) queue . push ( [ x + 1 , y ] ) ; if ( y + 1 < pieces . length && ( groupMap [ pieces [ y + 1 ] [ x ] ?? "" ] === groupMap [ group ] || pieces [ y + 1 ] [ x ] === "Item" ) ) queue . push ( [ x , y + 1 ] ) ; visited [ y ] [ x ] = true ; } chunks [ y ] [ x ] = ret ; } ) ; } ) ; const members = chunks . reduce ( ( acc , curr ) => { return [ ... acc , ... curr ] ; } , [ ] as [ number , number ] [ ] [ ] ) . filter ( ( e ) => e != null ) . filter ( ( chunk ) => { const members = Array . from ( new Set ( chunk . map ( ( [ x , y ] ) => pieces [ y ] [ x ] as string ) ) ) . filter ( ( n ) => n !== "Item" ) ; return groupMembers [ groupMap [ members [ 0 ] ] ] ?. length === members . length ; } ) . reduce ( ( acc , curr ) => ( acc . length > curr . length ? acc : curr ) , [ ] ) ; return members . length > 0 ? members : undefined ; } ;