View Single Post
  #33  
Old 11-12-2011, 01:57 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
I ended up with something like this:

PHP Code:
function onCreated() {

  
temp.4// Amount of Slots in Units
  
temp.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_available1temp.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.slotstemp.cartemp.car_length) {
  
// Find Possible Spots
  
temp.spots findPossibleSpots(temp.car 1temp.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.spottemp.spots) {
      
temp.updated_slots parkInSpot(temp.slotstemp.spottemp.cartemp.car_length);
      
findParking(temp.updated_slotstemp.car 1temp.car_length);
    }
  } else {
    
// No Parking Available for Car
    // Add Amount of Cars that did Find Parking
    // Increment Sample Size
    
temp.cars = (temp.spots_count == 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_mintemp.cars);
    
this.cars_max = (this.cars_max == 0) ? temp.cars max(this.cars_maxtemp.cars);
    
this.tracking.add(temp.slots);
  }
}

function 
parkInSpot(temp.slotstemp.spottemp.cartemp.car_length) {
  
temp.car_slots = {};
  
temp.car_slots.addarray(temp.slots);
  for (
temp.0temp.temp.car_lengthtemp.i++) {
    
temp.car_slots[temp.spot temp.i] = true;
  }
  return 
temp.car_slots;
}

function 
findPossibleSpots(temp.last_cartemp.slotstemp.car_length) {
  
temp.found = {};
  
temp.max   temp.slots.size();
  for (
temp.itemp.slots.indices(0)) {
    
temp.canpark true;
    for (
temp.0temp.temp.car_length && temp.canparktemp.j++) {
      
temp.temp.temp.j;
      if (
temp.slots[temp.k] > || temp.== 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).
__________________
Quote:

Last edited by fowlplay4; 11-12-2011 at 07:04 PM..
Reply With Quote