New answers tagged

1

C (gcc), 121 bytes l;e;r;o;a;f(m,n)int*m;{r=l=o=0;a=~0;for(e=1;r++<n;o|=*m,a&=*m++)~*m<<32-n?e&=!*m:++l;for(o-=a;a;a/=2)l+=a&1;r=l<2&&e^!o;} This has entire rows stored in integer arrays, making the maximum matrix size 32. Try it online!


1

Haskell, 12 bytes f x|even x=x Try it online!


0

Julia 1.0, 44 bytes ⊊ & > x$_=x < foldr != | !| == !foldr ^ x$_=!x <= !& !⊊ Try it online! based on Dennis's Julia 0.4 answer


1

MMIX, 128 bytes (32 instrs) (v0: only 0/1 input, 0/1 output; uncommented lines are "return a") false POP 0,0 // return 0 and ZSNZ $0,$1,$0 // if(!b) a = 0 POP 1,0 nimp ZSZ $0,$1,$0 // if(b) a = 0 POP 1,0 a POP 1,0 ncimp ZSZ $0,$0,$1 // a = a? 0 : b POP 1,0 b POP 2,0 // ...


1

GNU-APL, 34 bytes {+/(⍺≡+/,⍵)∧(⍵∧.=⍺⍴1)∨((⍺⍴1)∧.=⍵)} Input the size of the matrix, and the matrix itself. ⍵∧.=⍺⍴1 finds the row with all ones, a1=1∧a2=1∧a3=1 ... (⍺⍴1)∧.=⍵ finds the column with all ones The disjunction of the above two ensures atleast one of them happends ⍺≡+/,⍵ ensures that only required numbers of ones are present


1

PowerShell, 49 45 46 bytes inspired by Arnauld's and Neil's answers. +1 bytes with additional Test cases. "$args "-match'^(0*10* )\1+$|^(0+ )*1+ [0 ]*$' Try it online! convert the arguments to a string with space char as separator add the trail space char match with regexp


2

tinylisp, 88 bytes (load library (d _(q((M)(e 1(*(sum(map all M))(sum(map any M (q((M)(+(_ M)(_(transpose M The solution is the anonymous function in the last line. Try it online! Here's an ungolfed version that verifies all test cases: Try it online! Explanation Define a helper function _ that takes a matrix (list of lists) M: (sum(map all M)) ...


4

Python 2, 43 bytes Heavily based on dingledooper's answer, which you should upvote. lambda a,n:set(map(sum,a+zip(*a)))=={0,1,n} Try it online! Or: Python 3, 44 bytes: lambda a,n:{*map(sum,a+[*zip(*a)])}=={0,1,n} Saves bytes with set unpacking, but loses bytes because zip doesn't return a list. Try it online! Explanation In a truthy input, the sum of a row ...


1

APL (Dyalog Unicode), 63 55 bytes {(F⍉⍵)+(F←{(1↑⍴⍵)=(+/,⍵)×+/∧/⍵})⍵} Try it online! changed approach F used on input + input transposed ×+/∧/⍵ ⍝ number of all 1s lines multiplied by (+/,⍵) ⍝ total 1s (1↑⍴⍵)= ⍝ equal to side length?


2

T-SQL, 152 bytes Input is a table variable Returns 1 for true, 0 for false To save bytes, this can only handle matrix up to length 10 SELECT top 1iif(x=replace(a,0,'')or sum(x)over()=x*2-1and a not like'%0%'and 0=min(a)over(),1,0)FROM(SELECT rank()over(order by a)x,*FROM @)c ORDER BY-x Try it online


1

Charcoal, 27 bytes WS⊞υι∨∧⁼⌊υ⌈υ⁼¹Σ⌈υ⁼ΦυΣι⟦⭆⌈υ¹ Try it online! Link is to verbose version of code. Takes input as a newline-terminated list of strings. Explanation: WS⊞υι Input the matrix. ⁼⌊υ⌈υ All rows are identical ∧ Logical And Σ⌈υ The sum of a row ⁼¹ Equals literal `1` ∨ ...


3

Julia, 37 bytes x$n=(l=sum([x x'],dims=1))[l.>1]==[n] Try it online! uses Razetime's approach


2

R, 158 bytes function(a,b,`+`=function(m,`?`=toString,x=seq(l=nrow(m))){for(i in x)x[K]=min(x[K<-as.matrix(dist(m))[i,]<=1]);?sort(by(m,x,function(d)?Map(rank,d)))})+a==+b Try it online! A function taking two matrices having two columns x,y with the coordinates of 1's and returning TRUE/FALSE. The order of the coordinates must be the same in the two ...


6

05AB1E, 8 bytes Based on the first part of Bubblers Jelly answer, which uses dingledooper's algorithm. Input is a 2d matrix and the side length, output is 1 for truthy inputs and any other integer for falsy ones. This is the definition of truthiness in 05AB1E. Dø«O{¥θα Try it online! or Try all cases! D # push two copies of the input matrix ø ...


3

Retina 0.8.2, 32 bytes ^(0*10*)(¶\1)*$|^(0+¶)*1+(¶0+)*$ Try it online! Turns out to be much like @Arnauld's answer, except I wanted to solve it for no trailing newline instead.


10

JavaScript (ES6), 46 bytes Thanks to @PertinentDetail for a bug fix Expects a multiline string with a trailing line feed. Returns a Boolean value. s=>/^(0*10*\n)\1*$|^(0+\n)*1+\n[^1]*$/.test(s) Try it online!


6

Jelly, 9 bytes ŒṪZEƇȧiɗJ Try it online! The fourth totally different Jelly 9 byter. Returns either 1 or 2 for truthy and either an empty list or the number 0 for falsy. Also the program structure is more convoluted. How it works ŒṪZEƇȧiɗJ Monadic link. Input: the matrix M of size n ŒṪZ Take the transpose of multi-dimensional indices of 1 ...


11

Ruby, 47 bytes ->m,n{m.uniq[1]?m-[[0]*n]==[[1]*n]:m[0].sum==1} Try it online! Explain please: Split the two cases: If m has at least 2 different rows, remove all zeros and check if we are left with a single row consisting of ones. If not (all the rows are the same), check that the sum of the first row is one.


8

Wolfram Language (Mathematica), 21 13 35 bytes Norm@#==#2&&Norm@Eigenvectors@#>#2& Try It Online! (Thanks to @ZaMoC for configuring this.) Takes # as the matrix, and #2 as the square root of the side length, n. Returns True if the conditions are met, and False otherwise Explanation In Mathematica, the Norm function, when applied to a matrix, ...


13

Brachylog, 8 bytes {z|}≡ᵛ+1 Try it online! {z|} Either the rows or the columns ≡ᵛ are all the same list +1 which sums to 1.


5

Wolfram Language (Mathematica), 25 bytes #||#/.{a_..}:>Tr@a==1& Try it online! Returns True if the matrix has a single line, and nonTrue otherwise. #||# input OR its tranpose /.{a_..}:> replace lists of repeated elements with Tr@a==1 (is the sum of that element equal to 1?) Then, any Or ...


7

Vyxal, 12 bytes :∑$v∑Js¯Nt›= Try it Online! Yay! Beating Lyxal by 1 byte with a different approach!!! -1 byte from @Lyxal himself thanks


5

Jelly, 9 bytes ,ZfJṁ=ⱮJƲ Try it online! Idea stolen from Razetime in TNB , Pair the input with Z its transpose. f Filter the pair to elements contained in Jṁ=ⱮJƲ every matrix of the same dimensions with a single column of 1s. Jṁ Mold [1 .. len(input)] to the shape of the input. ⱮJ For each 1 .. len(...


8

Jelly, 9 bytes S;§ṢIṪ‘=L Try it online! Slightly modified port of dingledooper's algorithm. How it works S;§ṢIṪ‘=L Monadic link; input = matrix M S;§ Column sums of M ++ Row sums of M Ṣ Sort I Increments; (next - previous) for each adjacent pair Ṫ Last item ‘ +1 =L Equals the length of M? ...


10

Factor + math.unicode, 59 bytes [ [ concat Σ = ] keep dup flip [ all-equal? ] bi@ or and ] Try it online! Takes \$n\$ and a matrix of \$1\$s and \$0\$s, in that order. [ concat Σ = ] keep Is the sum of the matrix equal to \$n\$? dup flip [ all-equal? ] bi@ or and And are all the rows in either the original matrix or its transposition equal?


2

Vyxal, 14 bytes :ÞTJv∑s2Nȯ1?"⁼ Try it Online! Another stealing of the python answer. Explained :ÞTJv∑s2Nȯ1?"⁼ :ÞTJ # merge the input list with itself transposed. v∑s # sort the list that results from getting the sum of each item 2Nȯ # take the last two items of that list 1?"⁼ # and check for equality ...


6

J, 25 bytes (1:0}0"0)e.],&(#\|."{])|: Try it online! Not the shortest (my previous translation of dingledooper's was a couple bytes shorter), but I wanted to come up with a different approach: (1:0}0"0) Is the matrix with all ones in the first row and zeros elsewhere... e. an elment of... ],&(#\|."{])|: All possible rotations of ...


6

Jelly, 10 bytes S;§>Ƈ1⁼LW$ Try it online! Uses Unrelated string's test suite. Fixed with some help from Unrelated string. Explanation S;§>Ƈ1⁼LW$ S sums of columns ; concat with § sums of rows Ƈ keep the sums which are: > 1 greater than 1 ⁼LW$ = [length] ?


27

Python 2, 49 bytes Takes as input a 2D binary matrix \$ a \$, and its size \$ n \$. lambda a,n:sorted(map(sum,a+zip(*a)))[-2:]==[1,n] Try it online! There may be shorter approaches, but this is what I could find for now. Please let me know if the algorithm is incorrect. Explanation Since there is exactly one line, there should be exactly one row/column ...


0

Retina 0.8.2, 61 bytes .+ 1$&2 +`(?<=(.)*)(1|2)((?>.*¶(?<-1>.)*))?(?!\2)[12] 2$+2 ^2 Try it online! Explanation: .+ 1$&2 Wrap each row between a 1 and a 2. +`(?<=(.)*)(1|2)((?>.*¶(?<-1>.)*))?(?!\2)[12] 2$+2 Repeatedly find pairs of 1s and 2s either horizontally or vertically adjacent and replace them both with 2s. This will ...


1

JavaScript, 85 bytes A=>f=(i=0,X=0,Y=-1)=>Y<0||A[Y][X]?(A+0)[i]&&[2,1,0,-1].map(t=>f(i+1,X+t--%2,Y+t%2)):0 Slow. Error if possible. Treating all area Y<0 as solid and try walking enough time, error if it goes Y>Ymax(A[Y] is undefined) Try it online!


2

Python 3, 135 127 123 bytes Answering my own question since there is no python solution yet. Expects a transposed matrix. Strongly inspired by Lynn's rotation method. I'm sure there is room for improvement! def f(x): x+=[[3]*len(x)] for _ in x*4*len(x):x=[map(lambda a,b:a|a*2&b,y,(0,*y))for y in zip(*x)][::-1] return 3in x[0] Try it online!


2

MATL, 21 20 19 18 bytes Translation of @elementiro's MATLAB/Octave answer, with small modifications. The input is a binary matrix (using ; as row separator). The output is either a row vector with all entries eqwual to 1 (which is truthy), or a row vector with at least one entry eqwual to 0 (which is falsy). The footer contains the truthiness/falsihood test. ...


1

Wolfram Language (Mathematica), 81 bytes Max[Intersection@@MorphologicalComponents[#,CornerNeighbors->False][[{1,-1}]]]>0& Solves the transpose problem, looking for connections between the top and bottom rows. The long-named overkill builtin MorphologicalComponents[#,CornerNeighbors->False] changes the 1s in the input to different integers ...


1

Charcoal, 25 bytes WS⊞υ⭆⪫11ι℅¬Iκ⪫υ¶←¤-J⁰¦⁰T¹ Try it online! Link is to verbose version of code. Takes input as a newline-terminated list of strings (-3 bytes to take a input as a cumbersome array of strings: Try it online!) and outputs a Charcoal boolean, i.e. - if a path exists, blank if not. Explanation: WS⊞υ⭆⪫11ι℅¬Iκ Wrap each line in 1s. This changes ...


1

Perl 5 (-p), 65 bytes / /;$,="(.{@-})?";s/^1/#/gm;1while s/#$,\K1|1(?=$,#)/#/s;$_=/#$/m Try it online! -00l012 options are just used to run all tests and to print result. /\n/ to match first end of line, to get the width $,="(.{@-})?"; to define the variable $, with expression that matches the line width characters (used to match a ...


6

Haskell, 96 bytes e=[]:e (2:l)%1=2:2:l l%x=x:l h a=any(>=[2])$foldl(zipWith(%))e`iterate`map(2:)a!!(sum(2<$a)^2+2) Try it online! Similarly to other answers, this "flood-fills" the input with 2s and checks if we reach the right edge. l%x acts like x:l, but if l starts with a 2 and x is 1, then x is replaced by a 2 also. foldl(zipWith(%))e ...


2

APL (Dyalog Unicode), 24 bytes Uses the same method as Jonah's J answer. Full program that detects top-bottom connections. ⊃⌽⌈.×⍣≡⍨2>+/¨|∘.-⍨⍸1⍪⎕⍪1 Try it online! Footer transposes inputs. 1⍪⎕⍪1 ⍝ the input matrix with additional rows of 1's at the top and bottom ⍸ ⍝ get all 2d indices of 1's |∘.-⍨ ...


7

J, 48 bytes _1{0{1([:+./ .*^:_~1>:|@-/~)@($j./@#:I.@,)@,~1,] Try it online! Takes a binary matrix, and checks for a top-to-bottom path. Hence, the input pre-processor transposes the test cases. the idea Add a row of ones to the top and bottom. Convert all ones to coordinates, encoded as complex numbers. Create an adjacency matrix for those points, with ...


3

Jelly, 28 bytes +þ2ŒRBU¤ẎQfðƬṪ ŒṪḷ/Ị$Ƈç$Ḣ€iL Try it online! -2 bytes thanks to caird coinheringaahing -2 bytes using transposed form Returns 0 for falsy and non-zero for truthy Uses the exact same breadth-first-fill helper link as my answer to "But, Is It Art?" +þ2ŒRBU¤ẎQfðƬṪ Helper Link; given a list of starting coordinates on the left and a ...


2

05AB1E, 42 36 bytes A slower but maybe golfable? version. After looking at Jonah's J answer I was able to remove 6 bytes. gÅ1¸.øεā*0K€¸<Nδš}€`DδαO2‹DvDδ*O}нθĀ Try it online! 05AB1E, 42 bytes A lot of this is similar to this recent answer of mine. Tests for a top-bottom connection. εā*0K€¸<Nδš}D€gU€`DδαO2‹DvDδ*O}Xθ.£øOXн£OĀ Try it online! Header ...


2

JavaScript (ES7),  103 101  94 bytes Expects a transposed binary matrix. Returns 0 or 1. f=(m,X,Y=-1)=>!m[Y+1]|m.some((r,y)=>r.some((v,x)=>v&&(X-x)**2+(Y-y)**2<2|!y&&--r[x]|f(m,x,y))) Try it online! Commented f = ( // f is a recursive function taking: m, // m[] = input matrix X, Y = -1 ...


1

Jelly, 20 bytes ḣ1ŒṪạÆịỊẸʋƇ@ƬŒṪ=LḄỊȦ A monadic Link that accepts a list of the columns (i.e. transposed version of the OP's examples) and yields 1 (no connection) or 0 (connection). Try it online! Or see the test-suite. How? Builds up all coordinates reachable from the first column by repeatedly adding those within a unit circle of any found so far, then ...


6

R, 138... 118 bytes -9 bytes thanks to Dominic van Essen. function(m,k=nrow(m),r=m,`/`=cbind){r[,-1]=0 for(i in m)r=r|m&r[,-1]/0+0/r[,-k]+t(t(r[-1,])/0+0/t(r[-k,])) any(r[,k])} Try it online! At first, the matrix r contains only the 1s of the first column of the input m; all other entries are 0. At iteration i of the loop, entries which are attainable ...


12

MATLAB/Octave, 115 109 98* bytes -6 bytes thanks to Luis Mendo -7 bytes thanks to Luis Mendo function p=f(A) B=A;B(:,2:end)=0;for k=1:nnz(A) B=conv2(B,mod(magic(3),2),'same').*A;end p=any(B); Try it online! (end keyword is in footer because it's not required when you define the function in a file) Takes binary array, outputs: truthy value as vector full of ...


1

Z80, 59 bytes Input value in dehl. Output in c, 0, 1, 2, 3, 4 for ldr, mvn, mov, movw, movs respectively. Trashes b and a. This is indeed rather silly. 0E 03 7C B7 20 01 0C 7B B2 C8 AF 4F 3C F5 7A 2F 57 7B 2F 5F 7C 2F 67 7D 2F 6F 06 20 37 CB 7A 20 01 3F CB 15 CB 14 CB 13 CB 12 7C B3 B2 20 03 F1 4F F5 01 E8 F1 D8 37 3C F5 18 D3 foo: ld c,3 //...


2

MMIX, 96 bytes (24 instrs) Returns 8 for movs, else is a bitfield (1 for mov, 2 for mvn, 4 for movw). 0 is thus for ldr. I'm fairly certain that this can never return 6 or 7. 00000000: 3fff0008 4aff0003 e3010008 f8020000 ?”¡®J”¡¤ẉ¢¡®ẏ£¡¡ 00000010: 3fffff08 7301ff04 cf040000 e3050000 ?””®s¢”¥Ẇ¥¡¡ẉ¦¡¡ 00000020: fe020004 f2030003 f6040002 28010301 “£¡¥ṗ¤¡¤ẇ¥¡...


2

Python 3.8 (pre-release), 154 144 bytes lambda a:1-any([len({*a})>8,{(s:={sum(a[i%7:i%5*3:i%4])-a[4]for i in b"\r@@@+?"}).pop()}-{a[0]+a[8],a[2]+a[6]},s,sum(i<0 or i**.5%1for i in a)]) @ represents unprintables chars Outputs 1 for Parker, 0 for not Parker Try it online! A pretty messy solution ^^ How it works: 1-any([...]) will return 1 if all ...


2

<>^v, 184 bytes Prints 1 if number is prime, else prints 0. >]v2i>IT%0=vIT[vv > vv 1 I TI T T ) T = i t > ^ v , ^ < v >"1"; > >"0"; ^@| Explanation @ Pointer starts here, goes right | Mirror — ...


1

Charcoal, 151 120 bytes F²«≔⟦⟧θWS«≔⊕Lκη≔⁺θI⪪⁺κ0¹θ»Fη⊞θ⁰≔⟦⟧ζWΣθ«≔⟦⌕θ¹⟧εF嫧≔θλ⁰F⁺λ⟦η¹±¹±η⟧¿§θμ⊞εμ»⊞ζEε⟦⁻﹪λη﹪⌊εη÷⁻λ⌊εη⟧»≔⟦⟧θW⁻ζθF№ζ⌊κ⊞θ⌊κ⊞υθ»⁼⊟υ⊟υ Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for equivalent, nothing if not. Explanation: F²« Process two matrices. ≔⟦⟧θWS« Start reading the matrix into a flat array. ≔⊕...


Top 50 recent answers are included