thank you both,
single line version of yours and Jan Pieter's
HR =: 1 >: [: #@(-.&0)@~.@, [: (* (* >./) (5 2$0 0 1 0 _1 0 0 1 0 _1) |.!.0"1 _
])^:_ (* >:@i.@$)
isconnected =: (2 >: #@~.@,@((>./ * *@{.)@:((9 2$0 0 0 _1 0 1 _1 0 _1 _1 _1 1 1
0 1 _1 1 1)&(|.!.0))^:_))@(* i.@:$)
isconnected"_1 g
original article was corrupted, but code updated (new link) with your
contributions as a filter that reduces solution time to 10 seconds
http://www.jsoftware.com/jwiki/PascalJasmin/sin
----- Original Message -----
From: Henry Rich <[email protected]>
To: [email protected]
Cc:
Sent: Sunday, April 12, 2015 1:09 PM
Subject: Re: [Jprogramming] challenge: is this map connected?
Assuming diagonals are not connected:
ooze =. * (* >./) (_2 ]\ 0 0 1 0 _1 0 0 1 0 _1) |.!.0"1 _ ]
isconn =. (1 >: [: #@(-.&0)@~.@, [: ooze^:_ (* >:@i.@$))"2
isconn g
1 1 0 0 0 0
g
0 0 1 1 1 1
0 0 1 1 1 1
0 0 1 1 1 1
0 0 1 1 1 1
0 1 1 1 0 1
0 0 0 0 0 1
0 0 1 1 1 1
0 0 0 0 1 1
0 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
1 1 1 1 1 1
0 0 1 1 1 1
0 0 1 1 1 1
0 0 0 1 1 1
1 1 0 0 0 0
1 1 1 1 0 0
1 1 1 0 0 1
0 0 1 1 1 1
0 0 1 1 0 0
0 0 0 1 0 0
1 1 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 1
0 0 1 1 0 1
0 0 0 0 0 1
1 0 0 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
0 0 1 1 1 1
0 0 0 0 1 0
0 0 0 0 0 0
1 0 0 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
Henry Rich
On 4/12/2015 12:40 PM, 'Pascal Jasmin' via Programming wrote:
> I did develop further the "multiple item Power" function I brought up earlier
> to solve a semihard problem. A write up is here.
>
> http://www.jsoftware.com/jwiki/PascalJasmin/Single%20line%20path%20searching%20frameworks
>
>
> But I pose a challenge:
>
> consider these 6 grids:
>
> 0 0 1 1 1 1
> 0 0 1 1 1 1
> 0 0 1 1 1 1
> 0 0 1 1 1 1
> 0 1 1 1 0 1
> 0 0 0 0 0 1
>
> 0 0 1 1 1 1
> 0 0 0 0 1 1
> 0 0 0 0 0 1
> 1 0 0 0 0 1
> 1 1 1 1 1 1
> 1 1 1 1 1 1
>
> 0 0 1 1 1 1
> 0 0 1 1 1 1
> 0 0 0 1 1 1
> 1 1 0 0 0 0
> 1 1 1 1 0 0
> 1 1 1 0 0 1
>
> 0 0 1 1 1 1
> 0 0 1 1 0 0
> 0 0 0 1 0 0
> 1 1 0 0 0 0
> 1 1 1 1 1 1
> 1 1 1 1 1 1
>
> 0 0 0 0 0 1
> 0 0 1 1 0 1
> 0 0 0 0 0 1
> 1 0 0 1 1 1
> 1 1 1 1 1 1
> 1 1 1 1 1 1
>
> 0 0 1 1 1 1
> 0 0 0 0 1 0
> 0 0 0 0 0 0
> 1 0 0 1 1 1
> 1 1 1 1 1 1
> 1 1 1 1 1 1
>
>
>
> The first 2 are "connected" because all of the 1s form a single continuous
> island. The last 4 are not connected because there are more than a single
> continuous island of 1s.
>
> Can you write a function that returns 1 if a grid is connected, 0 otherwise?
>
> An easier way to load the data for each of the grids.
>
> 6 6$0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 1
> 6 6$0 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
> 6 6$0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 0 0 1
> 6 6$0 0 1 1 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
> 6 6$0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> 6 6$0 0 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm