I ended up with something like this:
PHP Code:
function onCreated() {
temp.n = 4; // Amount of Slots in Units
temp.l = 2; // Length of Cars in Units
temp.parking_available = new[temp.n];
this.tick = 0;
this.cars_sum = 0;
this.cars_min = 0;
this.cars_max = 0;
this.cars_sample = 0;
this.tracking = {};
findParking(temp.parking_available, 1, temp.l);
temp.avg = this.cars_sum / this.cars_sample;
echo("Sample Size: " @ this.cars_sample);
echo("Min: " @ this.cars_min);
echo("Max: " @ this.cars_max);
echo("Sum: " @ this.cars_sum);
echo("Average: " @ temp.avg);
}
function findParking(temp.slots, temp.car, temp.car_length) {
// Find Possible Spots
temp.spots = findPossibleSpots(temp.car - 1, temp.slots.link(), temp.car_length);
temp.spots_count = temp.spots.size();
if (temp.spots_count > 1) {
// Avoid Crashing Server
this.tick++;
if (this.tick == 1000) {
//echo("Calculating..." SPC timevar2);
this.tick = 0;
sleep(0.05);
}
for (temp.spot: temp.spots) {
temp.updated_slots = parkInSpot(temp.slots, temp.spot, temp.car, temp.car_length);
findParking(temp.updated_slots, temp.car + 1, temp.car_length);
}
} else {
// No Parking Available for Car
// Add Amount of Cars that did Find Parking
// Increment Sample Size
temp.cars = (temp.spots_count == 1 ? temp.car : temp.car - 1);
this.cars_sum += temp.cars;
this.cars_sample++;
this.cars_min = (this.cars_min == 0) ? temp.cars : min(this.cars_min, temp.cars);
this.cars_max = (this.cars_max == 0) ? temp.cars : max(this.cars_max, temp.cars);
this.tracking.add(temp.slots);
}
}
function parkInSpot(temp.slots, temp.spot, temp.car, temp.car_length) {
temp.car_slots = {};
temp.car_slots.addarray(temp.slots);
for (temp.i = 0; temp.i < temp.car_length; temp.i++) {
temp.car_slots[temp.spot + temp.i] = true;
}
return temp.car_slots;
}
function findPossibleSpots(temp.last_car, temp.slots, temp.car_length) {
temp.found = {};
temp.max = temp.slots.size();
for (temp.i: temp.slots.indices(0)) {
temp.canpark = true;
for (temp.j = 0; temp.j < temp.car_length && temp.canpark; temp.j++) {
temp.k = temp.i + temp.j;
if (temp.slots[temp.k] > 0 || temp.k == temp.max) {
temp.canpark = false;
}
}
if (temp.canpark) {
temp.found.add(temp.i);
}
}
return temp.found;
}
Which recursively generates every possible combination that the parking lot can accept. When the parking lot is full then it records the amount of cars the combination could take add it's to the sum and increases the sample size.
I don't think that's the 'linear' way though since when N=4 I get 1.666_. If I track the patterns and ignore ones that have already been generated I get the right answer for N=4.
I'm guessing there's a much better way of going about this than brute force since I can't even calculate an answer where N=100 with the (npcserver).