0

This is more of a math/algorithm question than a programming question, but I hope you guys can help anyway.

Scenario #1:

Player 1 has 40 crates in his inventory.

Player 1 has 2 trucks,

1x small (capacity: 8 crates)

1x medium (capacity: 16 crates)


Given capacity:

A small truck can hold 8 crates

A medium truck can hold 16 crates

A large truck can hold 30 crates

How many trucks does player 1 need to be able to take all 40 crates?

Scenario #2, what happens if there is cargo already in trucks?

Player 1 has 40 crates and 2 trucks as above scenario.

If small already has 2 crates in its load, giving him 8-2 = 6 space

If medium already has 4 crates in its load, giving him 16-4 = 8 space

How many trucks does player 1 need to take all 40 crates? What would the algorithm be?

Scenario #3: No trucks

Player 1 has 0 trucks at all. How many trucks does he need to take all 40 crates? Again, what is the algoritm you would use?

Scenario #4: Too many trucks

Player 1 has 10 trucks, all at large capacity. How many trucks does it take to ship all 40 crates?

I'm thinking.

Scenario 1,

2 trucks, 1 small = 8 and 1 medium = 16
8+16 = 24 crates
40 - 24 = 16 trucks?? // This looks wrong.

Costs of trucks are done earlier on (you buy them first).

I think my algorithm is wrong. Do I need to divide it by a base number? Do I divide it by trucks?

Any help on this would be very helpful.

Community
  • 1
  • 1
zardon
  • 2,910
  • 6
  • 37
  • 58
  • Although the number of crates player 1 is even, it should work for odd numbers. Trucks can only take even numbers (base of 2) – zardon May 06 '12 at 10:17
  • A truck does not have to be fully loaded. Although it would make it easier if it were. – zardon May 06 '12 at 10:24

2 Answers2

1

It's very helpful when you're trying to work out this sort of thing to keep the units

40 crates - 24 crates = 16 crates

The player has 40 crates, he has the ability to transport 24 crates, so he needs additional trucks to transport the remaining crates. The most efficient way, presumably, to transport 16 crates would be with 1 additional medium truck (you could also get 1 large truck or 2 small trucks).

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • Its mainly for AI and for background processing. Ideally the AI needs to know how many trucks it needs to buy, or have in order to deliver a load. – zardon May 06 '12 at 10:15
  • If, in scenario 4 (too many trucks) you'd end up with a minus figure? `40 - (10 trucks @ 30 cr each = 300) = -260?` I've probably made a mistake though – zardon May 06 '12 at 10:21
  • In 0 trucks scenario. You'd still have to work out how many trucks you'd need. `40 crates - 0 = 40` – zardon May 06 '12 at 10:25
1

I suggest the following algorithm (in pseudocode)

do truck = 1,number_trucks
  current_capacity(truck) = total_capacity(truck) - loaded_crates(truck)
enddo
sort trucks according to current_capacity (biggest first)
remaining_crates = 40
do truck = 1,number_trucks
  if(remaining_crates - current_capacity(truck) > 0)
    load truck full
    remaining_crates -= current_capacity(truck)
  else
    if(truck != last truck)
      if(remaining_crates - current_capacity(truck+1) > 0)
        load truck with remaining_crates
        remaining_crates = 0
      endif
    else
      load truck full
      remaining_crates -= current_capacity(truck)
    endif
  endif
enddo
sort truck_class according to total_capacity(truck_class) (biggest first)
do truck_class = 1,number_truck_classes
  while(remaining_crates - total_capacity(truck_class) > 0)
    buy truck of truck_class
    remaining_crates -= total_capacity(truck_class)
  end while
  if(truck_class == number_truck_classes && remaining_crates > 0)
    buy truck of truck_class
  endif
enddo
Azrael3000
  • 1,847
  • 12
  • 23
  • This looks promising! I'm going to code up a simple app for this and see if I can get it work. – zardon May 06 '12 at 10:28
  • I hope I didn't make a mistake. Good luck. – Azrael3000 May 06 '12 at 10:28
  • Its alright, I can use it as an outline. I was hoping I could do it all with one simple algorithm, however it looks like programming is needed. If I could, I'd do this up in excel and feed in different numbers but doing it in a small app is fine. – zardon May 06 '12 at 10:30
  • Well you can simplify the algorithm by using modulo. But I'll leave that as an exercise to you. Also I consider this algorithm to be quite simple, I mean I made it on a lazy Sunday morning :) – Azrael3000 May 06 '12 at 10:33
  • I don't get the 'if truck != last truck'part. If the flow falls down there is because the current truck has enough space, so why make that check? – BlackBear May 06 '12 at 10:43
  • Because in the next line I check for truck+1 so I need to make sure I don't go over my array bounds. – Azrael3000 May 06 '12 at 10:58
  • I've accepted your answer, it gives me enough to work with. Thanks! – zardon May 15 '12 at 06:03