Skip to content

Instantly share code, notes, and snippets.

@kadko
Last active May 11, 2019 12:35
Show Gist options
  • Save kadko/8593b85fb954adfa7b4b264e43b7a6de to your computer and use it in GitHub Desktop.
Save kadko/8593b85fb954adfa7b4b264e43b7a6de to your computer and use it in GitHub Desktop.
Net puzzle solver Algoritm
/*
Generate puzzle here: https://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/net.html
Generated puzzle stored to H array and outputs solution to same H array
makekomsu() finds border cells and neighbour cell states and arranges H array as output. This function invoked whenever H array changes.
solvePuzzle() checks for border and later checks locked(solved) neighbours connections
not tested for above 4x4,
Author: Kadir Korkmaz
*/
function getAllIndexes(arr, val) {
var indexes = [], i = -1;
while ((i = arr.indexOf(val, i+1)) != -1){
indexes.push(i);
}
return indexes;
}
function rotate_array(array) {
var popped = array.pop();
array.unshift(popped);
return array;
}
var H = [];
var komsu = [];
var HUSAS = [];
/*
örnek puzzle (sample puzzle)
0: indicates no pipe to this direction
1: indicates there is pipe to this direction
Order: [TOP, RIGHT, BOTTOM, LEFT]
*/
H =[
[[0,1,1,0], [0,0,0,1], [1,0,0,0], [0,1,0,0]],
[[0,0,1,1], [0,1,0,1], [0,1,1,1], [1,0,1,0]],
[[0,0,0,1], [1,1,1,0], [1,1,0,1], [1,1,0,1]],
[[1,0,0,0], [0,1,1,1], [0,1,0,0], [1,0,0,0]]
];
function makekomsu(){
for( var a=0; a<4; a++ ){
for( var b=0; b<4; b++ ){
if( H[b][a-1]==null ){ HUSAS[3]=2; }else{ HUSAS[3] = H[b][a-1][1]; }
if( H[b+1]==null || H[b+1][a]==null){ HUSAS[2]=2; }else{ HUSAS[2] = H[b+1][a][0]; }
if( H[b][a+1]==null ){ HUSAS[1]=2; }else{ HUSAS[1] = H[b][a+1][3]; }
if( H[b-1]==null || H[b-1][a]==null){ HUSAS[0]=2; }else{ HUSAS[0] = H[b-1][a][2]; }
komsu[b+","+a] = [ HUSAS[0], HUSAS[1], HUSAS[2], HUSAS[3] ];
}
}
}
makekomsu();
function solvePuzzle(n1){
for( var a=0; a<n1; a++ ){
for( var b=0; b<n1; b++ ){
var k = getAllIndexes(H[b][a], 0);
var t = getAllIndexes(komsu[b+","+a], 2);
if( k[1]-k[0]==2 && k.length == 2 ) H[b][a][k[0]] = "x"; //boru için (trick for pipe)
if( k.length == t.length && k.length !=0 ){
for(var dd=0; dd<14; dd++){
if( JSON.stringify(k) !== JSON.stringify(t) ){
rotate_array(H[b][a]); makekomsu();
k = getAllIndexes(H[b][a], 0);
if(k.length ==0){ k = getAllIndexes(H[b][a], 2);}
t = getAllIndexes(komsu[b+","+a], 2);
}else{
for(var d=0;d<4;d++){
if(H[b][a][d]=="x"){ H[b][a][d]=2;}//boru için
if(H[b][a][d]==0){ H[b][a][d]=2;}
if(H[b][a][d]==1){ H[b][a][d]=3;}
makekomsu();
}
}
}
}
var k2 = getAllIndexes(H[b][a], 1);
var t2 = getAllIndexes(komsu[b+","+a], 3);
if( k2.length == t2.length && k2.length !=0 ){
for(var dd=0;dd<14;dd++){
if( JSON.stringify(k2) !== JSON.stringify(t2) ){
rotate_array(H[b][a]); makekomsu();
k2 = getAllIndexes(H[b][a], 1);
if(k2.length ==0){ k2 = getAllIndexes(H[b][a], 3);}
t2 = getAllIndexes(komsu[b+","+a], 3);
}else{
for(var d=0;d<4;d++){
if(H[b][a][d]==0){ H[b][a][d]=2;}
if(H[b][a][d]==1){ H[b][a][d]=3;}
makekomsu();
}
}
}
}
}
}
}
export function puzzleSolver(){
var size = H.length;
for(var s = 0; s < size*size+1; s++ ){
solvePuzzle(size);
}
return H;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment