Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users

Dashboard
Notifications
Mark all as read
Challenges

Longest parallel lines

+4
−0

Challenge

You will be given a single 2D boolean array $M$. You may take its dimensions as a separate argument if needed.

Find the longest parallel lines in the matrix. Parallel lines can be vertical, horizontal, or diagonal, and there can't be a 1 anywhere between the two lines. (Imagine 0s are air, and the 1s are solid, the two lines have to be able to 'see' each other) For example, in the matrix

0 1 1 1 0 0
0 0 0 0 0 0
0 1 1 1 1 0 
0 0 1 1 0 0
0 0 0 0 0 0

the longest parallel line is length 3. If there are no parallel lines, output 0.

Test Cases

I:
0 0 0 1
0 0 0 0
0 0 0 0
0 1 0 0
O: 0
I:
0 0 1 0 1
0 1 0 1 0
1 0 1 0 0
O: 3
I:
0 0 1 0 1
0 0 1 0 1
0 0 1 1 1
0 0 1 0 1
0 0 1 0 1
O: 2
(The 1 in the middle is obstructing the line of sight)
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment

You may want to add some testcases with broken lines i.e. 5 5 ⍴ 1 0 1 1 0 0 Razetime‭ 3 months ago

1 answer

+5
−0

JavaScript (Node.js), 541 540 bytes

for(_='=>)==!(+==0h,.map(M,R(=1,,e,b	&&Nl=[-1,))s(n,=(a	a=u,v(i=g(z,u,f=(a,w,cs=Math.max,r?[...r(--a	)	(a)]:[],R>&b>&a<w&b<g=(f	,c,d(v(i(fc	d++f	)?a[b][f]:e)),v=fr(w+s).some(f),na(c,uc(f,z!f||(M1])olNO(oO)|(o--O(Po,l(Qx=z,yxN,yOx,y)a[y][x]&g(x,y,o,l)PQ++,n=s(n,Q,m1,0||(d0,1),n0,k=1]p[dp),1]q(jt=ze++,tpt)a[e][t]&g(t,q,!q)(q?m:d)++j,k=s(k,jkcf(r(w,gr(fa[f][g],w,0';G=/[-]/.exec(_);)with(_.split(G))_=join(shift());eval(_)

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

3 comments

Wow! Did not expect this one to be answered so quickly. Unfortunately it seems to fail for [[0, 1, 0], [1, 0, 0], [0, 0, 0]], two lines of length 1 that are parallel diagonally. rak1507‭ 3 months ago

@rak1507 I updated the answer to cover the [[0, 1, 0], [1, 0, 0], [0, 0, 0]] case, but I think you need to provide a better definition of what you mean by "diagonal". From your examples I thought that that particular test case that you show should return 0 and not 1. You probably should provide more test cases and/or a reference implementation. Hakerh400‭ 3 months ago

Yeah, my bad, I realise now it wasn't fully thought through. rak1507‭ 3 months ago

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!