# Give this a shot...

Back in my senior year of high school my shop teacher was offering extra credit for anyone that could come up with some code (Commodore 128 at the time) to answer his challenge.  Well, I graduated over 10 years ago, now play with Visual Basic 6, and his challenge still bothers me.  If anyone can help me with this I will give you all the points I have (sorry, I just joined so it's only 75).

You are building a shed and know exactly what cut lenghts of wood you need.  So now you need to go buy the wood, assuming the 2x4's (in this case it's all 2x4's) come in 8ft lenghts, how many 2x4's will you need to build the shed.

Even more extra credit was available if the cut width was added into the algorithm...

This has bothered me since 1988 and will for a while..  Any help would be great...

###### Who is Participating?

Commented:
Here you go...

1) Create a new project.

2) Add a MODULE to the program.

3) Set the Startup Object to "Sub Main".  You do this by clicking "Project->Project1 Properties..." and select it from the General Tab.

4) Add the code below to the module and run the program...

THE CODE:

Type WoodType
Size As Long
Count As Long
End Type
Public LumberCount As Long
Public WoodPieces() As WoodType
Public Working() As WoodType

Sub Main()
Dim iVal As Long
Dim lBuf As String

'CLEAR THE ARRAYS
On Error Resume Next
ReDim WoodPieces(-1) As WoodType
ReDim Working(-1) As WoodType
Err = 0

'LOAD THE ARRAYS WITH THE WOOD SIZES AND AMOUNT
AddWood 4, 2 '(2) 4ft lengths
AddWood 2, 4 '(4) 2ft lenghts
AddWood 5, 1 '(1) 5ft lenght
AddWood 3, 1 '(1) 3ft lenght
AddWood 2, 1 '(1) 2ft lenght

CalculateWood
lBuf = "You will need " + CStr(LumberCount) + " 8-foot 2x4"
For iVal = 0 To UBound(Working)
With Working(iVal)
If Not .Count = 0 Then
lBuf = lBuf + vbCrLf + "There will be " + CStr(.Count) + " " + CStr(.Size) + "-foot piece leftover"
End If
End With
Next iVal
MsgBox lBuf
End Sub
Sub AddWood(Size As Long, Count As Long)
Dim iVal As Long
Dim jVal As Long
Dim lSwitch As Boolean
On Error Resume Next
iVal = UBound(WoodPieces)
If Not Err = 0 Then
ReDim WoodPieces(0) As WoodType
With WoodPieces(0): .Size = Size: .Count = Count: End With
ReDim Working(0) As WoodType
With Working(0): .Size = Size: .Count = 0: End With
Else
lSwitch = False
For jVal = 0 To iVal
If WoodPieces(jVal).Size = Size Then
WoodPieces(jVal).Count = WoodPieces(jVal).Count + Count
lSwitch = True
Exit For
End If
Next jVal
If lSwitch = False Then
iVal = iVal + 1
ReDim Preserve WoodPieces(iVal) As WoodType
With WoodPieces(iVal): .Size = Size: .Count = Count: End With
ReDim Preserve Working(iVal) As WoodType
With Working(iVal): .Size = Size: .Count = 0: End With
End If
End If
End Sub

Sub AddLeftoverWood(Size As Long, Count As Long)
Dim iVal As Long
Dim jVal As Long
Dim lSwitch As Boolean
On Error Resume Next
iVal = UBound(Working)
lSwitch = False
For jVal = 0 To iVal
If Working(jVal).Size = Size Then
Working(jVal).Count = Working(jVal).Count + Count
lSwitch = True
Exit For
End If
Next jVal
If lSwitch = False Then
ReDim Preserve Working(UBound(Working) + 1) As WoodType
With Working(UBound(Working)): .Size = Size: .Count = Count: End With
End If
End Sub

Sub woodAvailable(lWood As WoodType)
Dim iVal As Long
For iVal = 0 To UBound(Working)
With Working(iVal)
If .Size = lWood.Size Then
If .Count > lWood.Count Then
.Count = .Count - lWood.Count
lWood.Count = 0
Else
lWood.Count = lWood.Count - .Count
.Count = 0
End If
Exit For
End If
End With
Next iVal
End Sub

Function CalculateWood()
Dim iVal As Long
Dim jVal As Long
Dim kVal As Long
For iVal = 0 To UBound(WoodPieces)
woodAvailable WoodPieces(iVal)
LumberCount = LumberCount + ((WoodPieces(iVal).Size * WoodPieces(iVal).Count) / 8)
kVal = IIf(8 - ((WoodPieces(iVal).Size * WoodPieces(iVal).Count) Mod 8) = 8 _
, 0, 8 - ((WoodPieces(iVal).Size * WoodPieces(iVal).Count) Mod 8))
If Not kVal = 0 Then AddLeftoverWood kVal, 1
Next iVal
jVal = 0
For iVal = 0 To UBound(Working)
If Working(iVal).Count <> 0 Then
jVal = jVal + (Working(iVal).Count * Working(iVal).Size)
End If
Next iVal
Do
If jVal <= 0 Then Exit Do
jVal = jVal - 8
LumberCount = LumberCount + 1
Loop
End Function
0

Commented:
If you fill your profile you get another 125 points
;->
0

Commented:
hmm...I dont even know what its asking  =/
0

Author Commented:
Sorry AzraSound, I re-read my question and realized it is a little difficult.

Basically if you needed the following lenghts:

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

Then you would have to purchase four 8ft 2x4's to cover your lengths, and and you would have a 6ft piece left over.

I hope this helps to explain.
0

Commented:
I think you can try this for a solution..

Area of the shed=(Time of the day * length of shadow )-(Distance between the sun and the earth)/Time at which the first ray from the sun reached the shed.

Once we get the area we can easily solve the other problem you mentioned.

;->
0

Author Commented:
Uhm, that sounds like a good solution as long as there is lemonade (read: Beer) involved...  hehehe
0

Commented:
//////////////
Sorry AzraSound, I re-read my question and realized it is a little difficult.

Basically if you needed the following lenghts:

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

Then you would have to purchase four 8ft 2x4's to cover your lengths, and and you would have a 6ft piece left over.

I hope this helps to explain.
////////////

hey with this hint you dont need to calculate the area....

i will give you the solution but you have to increase the points to 200....

;->
0

Commented:
//////////////
Sorry AzraSound, I re-read my question and realized it is a little difficult.

Basically if you needed the following lenghts:

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

Then you would have to purchase four 8ft 2x4's to cover your lengths, and and you would have a 6ft piece left over.

I hope this helps to explain.
////////////

hey with this hint you dont need to calculate the area....

i will give you the solution but you have to increase the points to 200....

;->
0

progCommented:
You need to consider every possible ordering of the cut wood and how it could be cut from the 8 foot lengths in sequence.

A question - is there actually a recognized solution to this problem, i.e is there a 'magic' combination to find which will use less pieces of wood than any other?
0

Commented:
why cannot this be

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

like this

(2) 4ft lengths
(5) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
0

Commented:
deighton: Yes, there is.
I saw an example like this in one of my courses back in school. It has to do with optimization routines. I'll give it a look.
0

Author Commented:
Wow, you actually did it!  I put the routine through a few tests and found that with higher numbers it sometimes had some extra remaining wood (2 - 4ft pieces).  But all in all I am VERY impressed!!

Thank You!

Now, I wonder if that teacher still works at the school....

0

Commented:
Well, it was just off the top of my head... But I'm glad you liked it.

Thanks for the points!

0

Commented:
Try it with fractions...

0

Author Commented:
Erick7,

I thought of that too and modified Mcriders code to allow fractions.  It's not working as well as I'd hoped, but it does work and that's fine with me...  :)
0

Commented:
Sorry, but I didn't take fractions into consideration when I wrote that... You didn't mention it ;-)

0

Commented:
It's common sense.
The last time I built a shed, I believe I needed some non-integer sizes of lumber.  And my saw took about a 3/32" out of each cut.

Measure twice, cut once.
0

Commented:
Well, I dont build sheds, I build programs.. ;-)  Standard rule... If it's not in the program spec, Don't expect it in the program...

0

Commented:
Fair enough.
At least provide the user with a warning:

MsgBox "I'm sorry, You can't use fractional cuts to build your shed"

Otherwise, the builder may not get the right number of 2x4's.

;)
0

Author Commented:
I don't plan to use Mcriders code for anything, I just wanted to see how this sort of thing is done.  Therefore fractional lengths really weren't that big of a deal (for me), and I didn't ask for support for them.

I'm sure if you offer Mcrider some points he'd be more than happy to modify the code for you...  :)  Since he is the #2 Expert on this site..  ;)

hehe, I feel as though I'm in the presence of greatness...  :)
0

Author Commented:
Erick37,

I must apoligize.  I see you are #7 on the experts list.  I didn't know that before now, so when I mentioned mcrider being #2 I wasn't trying to be a smart-ass or anything.  I just didn't know you were up there also.

Just wanted to say that...
0

Commented:
jitz,

No genuflecting is necessary... I just like helping. ;-)

Cheers!
0

Author Commented:
hehehehehe....

That was not worship (or genuflection), it was a simple "high five" if you will, or maybe just a "thumbs up", or how about the underrated "pat on the back"...

Now, if you were to re-do the sample you posted to handle non-integer numbers and, well, make it "bullet-proof" then, YES, it would be (minor) genuflection...  hehehe  :)

Later...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.