# Inglenook Algorithm - check

• October 16th, 2012, 11:27 AM
manefist
Inglenook Algorithm - check
Hello everyone,

I am trying to solve the Inglenook puzzle, you can find the link here:
http://www.wymann.info/ShuntingPuzzl...ook-rules.html

Here I have pasted my algorithm and I would like to have some feedback before implement it in Java.

class Wagons {

integer Garbage[3]
integer L1[5]
integer L2[3]
integer L3[3]

integer train[5]
integer index_train = 0;

Wagons() {

//insert 5 random wagons in train[] array in ascending order
// i.e train[0] := 6 train[1] := 5 train[2] := 4 etc...
}
garbage_check(Line L) {

integer index_garbage = 0;

for i Garbage.length-1 to i 0 do { i--;

for j 0 to train.length do { j++; //check if I need any wagons in Garbage
if Garbage[i] == train[j] do { then sends them to Line 1
L1[index_train] := Garbage[index_garbage];
index_garbage--;
index_train++;
}
}
while hasFreeSpace(Garbage) do { //insert wagons in Garbage
for j = L.length to j > index_train && L[j] != null do { j++;
Garbage[index_garbage] := L[j];
index_garbage++;
}
}
}
while hasFreeSpace(L2) do { i++;
for i 0 to L2.length-1 do {
L2[i] := L2[i+1];
}
for i L2.length -1 to i 0 do { i--;
L2[i] := Garbage[index_garbage];
}
}
while hasFreeSpace(L3) do {
for i 0 to L3.length-1 do { i++;
L3[i] := L3[i+1];
}
for i L3.length -1 to i 0 do { i--;
L3[i] := Garbage[index_garbage];
}
}
}

hasFreeSpace(Line L) {

integer count;

for i 0 to L.length do {
if L[i] != null do {
count++;
}
}
switch(L) {

case L1: if count < 5 do { return true; };
break;
case L2: if count < 3 do { return true; };
break;
case L3: if count < 3 do { return true; };
break;
default:
}
return false;
}

checkLine(Line L) {

for i 0 to L.length do { i++;
for j 0 to train.length do { j++;
if L[i] != train[j] do {
garbage_check(L);
}
else do {
for j 0 to index_train do {
if hasFreeSpace(L1) do {
L1[index_train] := L[j];
index_train++;
}
}
}
}
}
}
}