Solve the Halting Problem for Befinge
ES6 (JavaScript), 111,101 bytes
EDIT: changed output values to true and false, instead of Y and N, to shave off 10 bytes more
Golfed
F=(I,M=[...I],c=0,i)=>(i={j:v=I.search`\n`+1,k:-v,h:-1,l:1,q:i,0:0}[M[c]])?F(I,M,c+i+(M[c]=0),i):i!=0
Test
F=(I,M=[...I],c=0,i)=>(i={j:v=I.search`\n`+1,k:-v,h:-1,l:1,q:i,0:0}[M[c]])?F(I,M,c+i+(M[c]=0),i):i!=0
//Alphabet Map
tr={
'<':'h',
'>':'l',
'^':'k',
'v':'j',
'.':'q',
'\n':'\n'
};
//Test
T=(I,A)=>{
console.log({"Y":"#Halting","N":"#Non-Halting"}[A]);
console.log("I=\n",I,"\nF(I)=",O=F([...I].map(s=>tr[s]).join('')));
console.log('NY'[O*1] == A ? "OK !" : "NOT OK !");
}
//Halting
T(
`>.v
..<`
,'Y');
//Non-Halting
T(
`>....v
..v..<
..>v..
^..<..`
,'N');
//Halting
T(
`.`
,'Y')
//Halting
T(
`v>
>^`
,'Y');
//Halting
T(
`....v....
....>...v
.^..<....
.......v<
.......v.
....^..<.`
,'Y');
//Halting
T(
`v<>v>v^
>v^>^>v
<>>^v<v
v^<>v^<`
,'Y');
//Non-Halting
T(
`>..v
^..<`
,'N');
//Non-Halting
T(
`>v<
v<.
>v.
v<.
>.^`
,'N');
//Non-Halting
T(
`>.>.>.v
.><.<.<`
,'N');
Sample Output
#Halting
I=
>.v
..<
F(I)= true
OK !
#Non-Halting
I=
>....v
..v..<
..>v..
^..<..
F(I)= false
OK !
#Halting
I=
.
F(I)= true
OK !
#Halting
I=
v>
>^
F(I)= true
OK !
#Halting
I=
....v....
....>...v
.^..<....
.......v<
.......v.
....^..<.
F(I)= true
OK !
#Halting
I=
v<>v>v^
>v^>^>v
<>>^v<v
v^<>v^<
F(I)= true
OK !
#Non-Halting
I=
>..v
^..<
F(I)= false
OK !
#Non-Halting
I=
>v<
v<.
>v.
v<.
>.^
F(I)= false
OK !
#Non-Halting
I=
>.>.>.v
.><.<.<
F(I)= false
OK !
Python 2, 116 105 bytes
x=1
X=Y=y=0
H=[]
G=input()
while(X,Y,x,y)not in H:H+=[(X,Y,x,y)];C=ord(G[Y][X]);x=C%3-1;y=C%5-1;X+=x;Y+=y
Try it online!
The challenge is old, but I figured since this is the shortest Python, I will post it. Input is a list of strings, but the characters used are unusual.
> G
< B
v C
^ F
. L
For example, the third halting example turns into ['LLLLCLLLL', 'LLLLGLLLC', 'LFLLBLLLL', 'LLLLLLLCB', 'LLLLLLLCL', 'LLLLFLLBL']
.
Output is via exit code, 0 (success) for non-halting, and 1 (error) for halting. Any tips or tricks appreciated.
Befunge-98 (PyFunge), 217 209 200 bytes
#v10dpf1dp12dp3dpk
>#v~:a-#v_$10dp1dg1+1dp >
v >10dp;>0dg1dgp0dg1+0dp^;f1dp
>0dg1dgg:'^-#v_n1-v
^<v01_v#!->':<
< >:'<-#v_01-0> v
v^pd1+gd3gd1[:'v-#v_01>3dp2dpndg1dgp
>0dg2dg+0dp ^ @.!;>:'.-#;_
Try it online!
A befinge halting problem needs a befunge solution. Returns 0 for truthy and 1 for falsey. Puts the input on the grid starting from 1,15 and then moves on top, replacing the arrows with zeros. As soon as we hit a zero, we know it loops. Anything besides ><^v. and zero is considerd to halt the program, which includes the border of spaces we get around the program by placing it on the grid slightly offset.
One easy way to shave off a few bites would be to use numbers instead of ><^v. but I don't feel like that's worth it.