32
\$\begingroup\$

Challenge

Given a binary matrix and a binary string, determine if that binary string can be found starting at any point in the matrix and moving in any direction at any subsequent point to form the binary string. That is, can the string be found folded up however inside the matrix?

The string can only be folded at 90 degrees or 180 degrees (edge connections; Manhattan Distance 1) and cannot overlap itself at any point.

Example

Let's take the following example:

Matrix:

010101
111011
011010
011011

Snake: 0111111100101

This is a truthy test case. We can see the snake folded up in the following position:

0-1 0 1 0 1
  |
1 1 1-0 1 1
  | | |   |
0 1 1 0-1-0
  | |
0 1-1 0 1 1

Rules

  • Standard Loopholes Apply
  • You may take the length of the string and the width and height of the matrix as input if you'd like
  • You may take the binary matrix and the binary string as a multiline string/array of strings/newline-joined string/anything else-joined string and a string
  • You may take the dimensions as a flat array instead of several arguments
  • Your program must finish for any 5 x 5 matrix with any string of up to length 10 in under a minute

Limitations

  • The matrix is not necessarily square
  • The string will be non-empty
  • The string can be length-1
  • The string will not contain more squares than available (that is, len(string) <= width(matrix) * height(matrix)

Test Cases

Truthy

01010
10101
01010
10101
01010

0101010101010101010101010



01110
01100
10010
10110
01101

011111000110100



0

0



10
01

1010



100
010
001

100010001

Falsy

00000
00000
00000
00000
00000

1



10101
01010
10101
01010
10101

11



100
010
001

111



10001
01010
00100
01010
10001

1000100010001000101010100
\$\endgroup\$
  • \$\begingroup\$ Sandbox Post (Deleted) \$\endgroup\$ – HyperNeutrino Sep 25 '17 at 2:07
  • 4
    \$\begingroup\$ Or: Binary Boggle! Also, can you add a few more test cases? \$\endgroup\$ – Jonah Sep 25 '17 at 3:04
  • 1
    \$\begingroup\$ What do flat, sharp, and round mean in this context? Does not square mean that the width and height maybe not be equal, or that the array can be jagged? \$\endgroup\$ – Tahg Sep 25 '17 at 10:28
  • \$\begingroup\$ what on earth is a round array \$\endgroup\$ – Conor O'Brien Sep 25 '17 at 10:45
  • \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Sep 25 '17 at 11:08
13
\$\begingroup\$

Python 2, 275 271 264 249 bytes

  • Saved four bytes by replacing -1 with H and removing one slicing operation ([:]).
  • Saved seven bytes thanks to Halvard Hummel; removing yet another slicing operation ([:]), using multiple-target assignment to give a visited entry a value v not in "01" (S=S[1:];M[y][x]=H; -> S=M[y][x]=S[1:];) and switching from a ternary if/else to a simple logical or (any(...)if S else 1 -> not S or any(...)).
  • If you somewhat extend your definition of truthy and falsey, you could allow this 257 byte long solution. It raises an exception (ZeroDivisionError) when the snake is found and returns an empty list ([]) when there is no snake to be found, which are two distinct behaviours.
  • Saved fourteen bytes thanks to user202729; golfing two array deep copies
  • Saved a byte; golfed not S or to S<[1]or ~ S==[]or.
lambda M,S,w,h:any(H(eval(`M`),S,w,h,x,y)for y in range(h)for x in range(w)if S[0]==M[y][x])
def H(M,S,w,h,x,y):S=M[y][x]=S[1:];return S<[1]or any(H(eval(`M`),S,w,h,x+X,y+Y)for X,Y in[(~0,0),(1,0),(0,~0),(0,1)]if~0<x+X<w>0<=y+Y<h!=S[0]==M[y+Y][x+X])

Try it online!

Explanation

Lambda function which takes in the matrix as a two-dimensional list of strings (either "0" or "1"), the snake as a one-dimensional list and the matrix dimensions as two integers.
The lambda function searches the matrix for entries that match the snake's first element. For every found match, it calls H with a deep copy of the matrix, no copy of the snake, the matrix dimensions and the match's position.

When H is called, it removes S' first entry and sets the given position's matrix entry to something else than "0", "1". If S' length is zero, it returns True; as it calls itself recursively, the snake was found somewhere in the matrix.
If S' length is non-zero, it loops through the four cardinal directions, tests if that position is in the matrix, compares the matrix' element at that position with the first element of S and -- if it matches -- calls itself recursively.
H's return values are funneled up the stack frames, always checking if at least one function found a possible snake.

Formatted Output

I have augmented my program to also output the path the snake slithers (if there is one). It uses the same ASCII output design as the question. TIO link.

\$\endgroup\$
  • \$\begingroup\$ 264 bytes \$\endgroup\$ – Halvard Hummel Sep 25 '17 at 7:38
  • 1
    \$\begingroup\$ @HalvardHummel Thanks; especially for spotting the superfluous slicing operation. \$\endgroup\$ – Jonathan Frech Sep 25 '17 at 8:07
  • \$\begingroup\$ @user202729 You think m[:]for ~> m*1for? Might work. \$\endgroup\$ – Jonathan Frech Jul 5 '18 at 11:46
  • \$\begingroup\$ @user202729 Thanks, the linked tip worked as I think this needs a deep copy. \$\endgroup\$ – Jonathan Frech Jul 5 '18 at 11:53
9
\$\begingroup\$

JavaScript (ES6), 138 134

Not so different from @Neil's, but what else could it be?

Input: matrix as a multi line string, binary string, width (not counting newline)

NB: the logic in the recursive function r is somewhat inverted to save a couple of bytes

(m,s,w)=>[...m].some((c,p,m,r=(p,o)=>s[m[p]=r,o]&&([~w,-~w,1,-1].every(d=>m[d+=p]!=s[o]||r(d,o+1))&&(m[p]=s[o-1])))=>c==s[0]&&!r(p,1))

Less golfed

(m,s,w)=>(
  m=[...m],
  r= (p, o) => 
    (m[p] = -w, s[o])
    && (
         [~w, -~w, 1, -1].every( d =>
            m[d+=p] != s[o] || r(d, o+1)
         )
         && (m[p]=s[o-1])
    ),
  m.some((c,p) =>c == s[0] && !r(p,1))
)

Test

var F=
(m,s,w)=>[...m].some((c,p,m,r=(p,o)=>s[m[p]=r,o]&&([~w,-~w,1,-1].every(d=>m[d+=p]!=s[o]||r(d,o+1))&&(m[p]=s[o-1])))=>c==s[0]&&!r(p,1))

// this slightly modified version tracks the path
var Mark=
(m,s,w)=>(m=[...m]).some((c,p,m,r=(p,o)=>s[m[p]=-o,o]&&([~w,-~w,1,-1].every(d=>m[d+=p]!=s[o]||r(d,o+1))&&(m[p]=s[o-1])))=>c==s[0]&&!r(p,1))
?m.map((c,p)=>c<-1?'.───│┘└.│┐┌.│'[((m[p-1]-c)**2<2)+((m[p+1]-c)**2<2)*2+((m[p+~w]-c)**2<2)*4+((m[p-~w]-c)**2<2)*8]:c<0?'*':c).join``:''

function go()
{
  O.textContent =F(M.value, S.value, M.value.search('\n'))+'\n\n'
  +Mark(M.value, S.value, M.value.search('\n'))
}

go()
#M {width:100px; height:100px }
<textarea id=M>010101
111011
011010
011011</textarea><br>
<input id=S value='0111111100101' oninput='go()'>
<button onclick='go()'>go</button>
<pre id=O></pre>

\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 149 bytes

(m,s,w)=>[...m].some((c,i)=>c==s[0]&&g(m,s,i),g=(m,s,i)=>!(s=s.slice(1))||[~w,-1,1,-~w].some(o=>m[o+=i]==s[0]&&g(m.slice(0,i)+' '+m.slice(i+1),s,o)))

Takes the matrix as a newline-delimited string, the snake as a string and the width (as an integer). Loosely based on @JonathanFrech's answer.

\$\endgroup\$
4
\$\begingroup\$

Mathematica, 180 156 141 153 138 136 104 bytes

MemberQ[#|Table[""<>Part[Join@@#,p],{x,1##4},{y,1##4},{p,FindPath[GridGraph@{##4},x,y,#3,All]}],#2,All]&

Example input

[{{"1","1","1","1","1"},{"0","0","0","0","0"}},"10011001",8,5,2]

Explanation

  1. GridGraph@{##4} is a Graph object for a grid of vertices with adjacent vertices connected by edges, with dimensions {##4} - that is, {#4,#5} or {width,height}.
  2. We iterate over all starting vertices x (numbered 1 to 1##4 = width*height), all ending vertices y, and all paths p of length at most #3 from x to y.
  3. For each such path, ""<>Part[Join@@#,p] extracts the corresponding characters of the matrix and puts them into a string.
  4. We also include the matrix itself, whose characters are all the strings of length 1 that can be found in it.
  5. We see if one of these strings matches s, searching at all levels because this is a very multidimensional list we've built.

Note: Replacing #3 by {#3-1} in FindPath, so that we only find paths of exactly the right length, is a huge improvement in terms of speed - but costs 4 more bytes.


-24 bytes: taking dimensions of things as input

-15 bytes: using StringPart and StringJoin properly

+12 bytes: fixing the length-1 case

-15 bytes: ...

-2 bytes: taking size of the matrix as input as an array

-32 bytes: using Table to iterate over the path lets us avoid using Function, and using MemberQ[...,s,All] lets us just sort of stick the matrix onto the table when dealing with snakes of length 1.

\$\endgroup\$
3
\$\begingroup\$

C# (.NET Core), 346 341 336 302 297 bytes

(m,h,w,s,l)=>{for(int y=0;y<h;y++)for(int x=0;x<w;x++)if(N(x,y,l-1))return 0<1;return 1<0;bool N(int x,int y,int p){if(p<0)return 0<1;if(y<0|x<0|y==h|x==w||m[y,x]>1||s[p]!=m[y,x])return 1<0;int g=m[y,x];m[y,x]=2;if(N(x,y-1,--p)||N(x-1,y,p)||N(x,y+1,p)||N(x+1,y,p))return 0<1;m[y,x]=g;return 1<0;}}

Try it online!

5 bytes saved by golfing the p increment

5 bytes saved by taking in the snake length and starting at its tail, and removing an unnecessary space

34 bytes saved by reading the challenge properly and seeing I can take in the matrix's height and width

5 bytes saved, the single element test case failed, and the fix was beneficial

Ungolfed

(m,h,w,s,l)=>{
    // Go through every potential starting point
    for(int y=0; y<h; y++)
        for(int x=0; x<w; x++)
            if(N(x,y,l-1)) // start the recursive steps
                return 0<1; // return true if N returns true, otherwise check the next element

    return 1<0; // return false as the snake doesn't fit into the matrix

    // C#7 local function in a Func
    bool N(int x, int y, int p)
    {
        // if there is no more snake to fit return true
        if(p<0)
            return 0<1;

        // if m element has part of the snake or 
        // snake part doesn't match matrix element then return false
        if(y<0 | x<0 | y==h | x==w || m[y,x]>1 || s[p] != m[y,x])
            return 1<0;

        // hold the current matrix element
        int g=m[y,x];
        // set the current matrix element to 2 to indicate it has a part of the snake
        m[y,x]=2;

        // check each of the four neighbours and recurse down that neighbour 
        // except if they are outside the matrix
        if(N(x,y-1,--p) ||
           N(x-1,y,p) ||
           N(x,y+1,p) ||
           N(x+1,y,p))
               return 0<1; // return true if remainder of the snake fits into the matrix

        // if snake doesn't fit then set the matrix element as not having part of the snake
        m[y,x]=g;
        // return false to indicate this neighbour direction doesn't fit the snake
        return 1<0; 
    }
}
\$\endgroup\$
  • \$\begingroup\$ A golfing start would be to remove all unnecessary whitespace... \$\endgroup\$ – Jonathan Frech Sep 25 '17 at 9:56
  • \$\begingroup\$ if(...)return true; -> return ...;. \$\endgroup\$ – Jonathan Frech Sep 25 '17 at 9:58
  • \$\begingroup\$ @JonathanFrech Agreed, but I left it like that to allow others to read it a little more easily until I get a chance to get back to it (sometime tomorrow). \$\endgroup\$ – Ayb4btu Sep 25 '17 at 9:58
  • \$\begingroup\$ @JonathanFrech Doesn't work, b[y,x] needs to be reset at some point. (Also sorry for misspelling your name in my answer.) \$\endgroup\$ – Neil Sep 25 '17 at 10:11
  • \$\begingroup\$ I meant if(N(x,y,0)>0)return 0<1;; the first appearance of return. \$\endgroup\$ – Jonathan Frech Sep 25 '17 at 10:17
1
\$\begingroup\$

Kotlin, 413 bytes

var x:(Array<Array<Char>>,String)->Boolean={b,s->fun f(s:String,x:Int,y:Int):Boolean{if(b[x][y]!=s[0])
return 0>1
if(s.length<2)
return 1>0
val v=b[x][y]
b[x][y]='Z'
try{return(-1..1).map{x+it}.flatMap{t->(-1..1).map{y+it}.map{t to it}}.filter{(X,Y)->(x-X)*(x-X)+(y-Y)*(y-Y)==1&&X in b.indices&&Y in b[0].indices&&f(s.substring(1),X,Y)}.any()}finally{b[x][y]=v}}
b.indices.any{x->(0..b[0].size-1).any{f(s,x,it)}}}

Beautified

var x: (Array<Array<Char>>, String) -> Boolean = { b, s ->
    fun f(s: String, x: Int, y: Int): Boolean {
        if (b[x][y] != s[0])
            return 0 > 1
        if (s.length < 2)
            return 1 > 0
        val v = b[x][y]
        b[x][y] = 'Z'
        try {
            return (-1..1).map{ x + it }
                    .flatMap { t -> (-1..1).map{y+it}.map { t to it } }
                    .filter { (X, Y) ->
                        (x - X)*(x - X) + (y - Y)*(y - Y) == 1 &&
                                X in b.indices && Y in b[0].indices &&
                                f(s.substring(1), X, Y) }
                    .any()
        } finally {
            b[x][y] = v
        }
    }
    b.indices.any { x -> (0..b[0].size - 1).any { f(s, x, it) } }
}

Test

var x:(Array<Array<Char>>,String)->Boolean={b,s->fun f(s:String,x:Int,y:Int):Boolean{if(b[x][y]!=s[0])
return 0>1
if(s.length<2)
return 1>0
val v=b[x][y]
b[x][y]='Z'
try{return(-1..1).map{x+it}.flatMap{t->(-1..1).map{y+it}.map{t to it}}.filter{(X,Y)->(x-X)*(x-X)+(y-Y)*(y-Y)==1&&X in b.indices&&Y in b[0].indices&&f(s.substring(1),X,Y)}.any()}finally{b[x][y]=v}}
b.indices.any{x->(0..b[0].size-1).any{f(s,x,it)}}}

data class Test(val board: String, val snake: String, val output: Boolean)

val tests = listOf(
        Test("""01010
            |10101
            |01010
            |10101
            |01010""", "0101010101010101010101010", true),
        Test("""01110
            |01100
            |10010
            |10110
            |01101""", "011111000110100", true),
        Test("""0""", "0", true),
        Test("""10
            |01""", "1010", true),
        Test("""100
            |010
            |001""", "100010001", true),
        Test("""00000
            |00000
            |00000
            |00000
            |00000""", "1", false),
        Test("""10101
            |01010
            |10101
            |01010
            |10101""", "11", false),
        Test("""100
            |010
            |001""", "111", false),
        Test("""10001
            |01010
            |00100
            |01010
            |10001""", "1000100010001000101010100", false)
)

fun main(args: Array<String>) {
    tests.filter {(board, snake, expected) ->
        val boardR = board.trimMargin().lines().map { it.toCharArray().toTypedArray() }.toTypedArray()
        val result = x(boardR, snake)
        result != expected
    }.forEach { throw AssertionError(it) }
    println("Test Passed")
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.