| Bytes | Lang | Time | Link |
|---|---|---|---|
| 351 | Python3 | 250718T175731Z | Ajax1234 |
| 249 | Python 2 | 170925T030331Z | Jonathan |
| 413 | Kotlin | 170926T152642Z | jrtapsel |
| 134 | JavaScript ES6 | 170925T101458Z | edc65 |
| 104 | Mathematica | 170925T231148Z | Misha La |
| 297 | C# .NET Core | 170925T095541Z | Ayb4btu |
| 149 | JavaScript ES6 | 170925T093344Z | Neil |
Python3, 351 bytes
A little long, but fast on larger inputs.
E=enumerate
def f(s,b):
d={(x,y):v for x,r in E(b)for y,v in E(r)}
q=[(*i,s[1:],[i])for i in d if s[0]==d[i]]
for i in d:
if s[0]==d[i]:
q=[(*i,s[1:],[i])]
while q:
(x,y,S,p),*q=sorted(q,key=lambda x:len(x[2]))
if''==S:return 1
q+=[(*v,S[1:],p+[v])for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]if d.get(v:=(x+X,y+Y))==S[0]and v not in p]
Python 2, 275 271 264 249 bytes
- Saved four bytes by replacing
-1withHand 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 valuev 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 ortoS<[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])
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.
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")
}
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>
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
GridGraph@{##4}is aGraphobject for a grid of vertices with adjacent vertices connected by edges, with dimensions{##4}- that is,{#4,#5}or{width,height}.- We iterate over all starting vertices
x(numbered1to1##4 = width*height), all ending verticesy, and all pathspof length at most#3fromxtoy. - For each such path,
""<>Part[Join@@#,p]extracts the corresponding characters of the matrix and puts them into a string. - We also include the matrix itself, whose characters are all the strings of length 1 that can be found in it.
- 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.
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;}}
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;
}
}
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.