@@ -252,6 +252,40 @@ class Grid {
252
252
return true ;
253
253
}
254
254
255
+ localInclude ( r , c , handled ) {
256
+ if ( this . cells [ r ] [ c ] || handled . has ( JSON . stringify ( [ r , c ] ) ) ) {
257
+ return false ;
258
+ }
259
+ let possible = this . neighbors ( [ r , c ] ) . filter ( ( [ node , edge ] ) => this . edges [ edge ] !== "unused" ) ;
260
+ let used = possible . filter ( ( [ node , edge ] ) => this . edges [ edge ] === "used" ) ;
261
+ return ( used . length < possible . length ) && ( possible . length <= 2 || used . length >= 2 ) ;
262
+ }
263
+
264
+ local ( ) {
265
+ const rows = this . cells . length ;
266
+ const cols = this . cells [ 0 ] . length ;
267
+ const stack = [ ] ;
268
+ const handled = new Set ( ) ;
269
+ for ( let r = 0 ; r < rows ; r ++ ) {
270
+ for ( let c = 0 ; c < cols ; c ++ ) {
271
+ if ( this . localInclude ( r , c , handled ) ) {
272
+ stack . push ( [ r , c ] ) ;
273
+ handled . add ( JSON . stringify ( [ r , c ] ) ) ;
274
+ }
275
+ }
276
+ }
277
+ while ( stack . length > 0 ) {
278
+ let [ r , c ] = stack . pop ( ) ;
279
+ this . adjustEdges ( new Set ( [ JSON . stringify ( [ r , c ] ) ] ) ) ;
280
+ for ( let [ [ nr , nc ] , _ ] of this . neighbors ( [ r , c ] ) ) {
281
+ if ( this . localInclude ( nr , nc , handled ) ) {
282
+ stack . push ( [ nr , nc ] ) ;
283
+ handled . add ( JSON . stringify ( [ nr , nc ] ) ) ;
284
+ }
285
+ }
286
+ }
287
+ }
288
+
255
289
parity1 ( ) {
256
290
let m = this . cells . length ;
257
291
let n = this . cells [ 0 ] . length ;
@@ -401,15 +435,7 @@ window.onload = function () {
401
435
} , 'p' ) ;
402
436
403
437
createButton ( 'Parity on each cell (l)' , function ( ) {
404
- const rows = grid . cells . length ;
405
- const cols = grid . cells [ 0 ] . length ;
406
- for ( let r = 0 ; r < rows ; r ++ ) {
407
- for ( let c = 0 ; c < cols ; c ++ ) {
408
- if ( ! grid . cells [ r ] [ c ] ) {
409
- grid . adjustEdges ( new Set ( [ JSON . stringify ( [ r , c ] ) ] ) ) ;
410
- }
411
- }
412
- }
438
+ grid . local ( ) ;
413
439
} , 'l' ) ;
414
440
415
441
createButton ( 'Parity 1 (1)' , function ( ) {
0 commit comments