Reputation: 395
Im building a program where it calculates how many bricks will fit into a box of any dimensions in C#. But also where the brick will fit in any orientation. It works some for some of my tests where the answer returned is what I expect it to be but others, the answer (while technically correct) isn't returning the maximum number that you could fit in to the box.
private static int BricksPerDimension(int dimWidth, int dimLength, int dimHeight, int brickSideWidth, int brickSideLength, int brickSideHeight)
{
int newBrickSideHeight;
int newBrickSideWidth;
if (dimHeight >= brickSideLength)
{
newBrickSideHeight = brickSideLength;
return (int)Math.Floor((double)dimHeight / newBrickSideHeight);
}
else if (dimWidth >= brickSideLength)
{
newBrickSideWidth = brickSideLength;
return (int)Math.Floor((double)dimWidth / newBrickSideWidth);
}
else if (dimLength >= brickSideLength)
{
return (int)Math.Floor((double)dimLength / brickSideLength);
}
else if (dimLength <= brickSideLength && dimHeight <= brickSideLength && dimWidth <= brickSideLength)
{
return 0;
}
else
{
return 0;
}
This is my code the test results are as follows: box with dimensions of 10x20x50 with brick of 1x2x5 = 1000 bricks, which is what I expected. And when I've tested the box but with the dimensions reordered for example 20x10x50 im returned 1000 bricks again which is great.
But for example this test box = 20 x 70 x 30 brick = 1 x 2 x 5 returns 2744, which unless my own calculations are seriously wrong should return as 4200 bricks.
I've used breakpoints to see if any of the values of the variables are going wrong somewhere, but nothing seems to be wrong with them. Ive been scratching my head over this for days and am not any closer to figuring it out.
Thanks in advance. P.S The way I constructed the code in regards to bricks fitting in any orientation was that if the longest side of the brick was equal to or less than the longest side of the box, then at the very least 1 brick must be able to fit. and so I did the if statement to check if this was true and then change the variables around. If this is the wrong way to approach this please let me know.
Edit Multiplication:
int numX = BricksPerDimension(boxW, boxL, boxH, brickSideWidth, brickSideLength, brickSideHeight);
int numY = BricksPerDimension(boxW, boxL, boxH, brickSideWidth, brickSideLength, brickSideHeight);
int numZ = BricksPerDimension(boxW, boxL, boxH, brickSideWidth, brickSideLength, brickSideHeight);
return numX * numY * numZ;
Upvotes: 0
Views: 176
Reputation: 48179
Like a pick-3 lottery, the 3 dimensions could represent any position, such as laying thebrick flat, on its end tall, or on its side. So, that said, we have 6 combinations based on ex: 123, 132, 213, 231, 312, 321 where each number is in a respective position. If you have some oddball dimensions, such as 25x33x40 and a brick dimension of 11x10x5 (yeah, right) it won't necessarily make sense that the 11 correspond to 25, 10 to 33, 5 to 40, but if you DID compute based on that, you would get 2 bricks per 25, 3 per the 33 and 8 per the 40 or 2x3x8 = 48 bricks.
Now, if tried where the 11 is in the second position, the 10 in the last position and 5 in the first, you get 5:25, 11:33 and 10:40 or 5 bricks by 3 bricks by 4 bricks 5x3x4 = 60 bricks since we can't cut bricks into fractions and you were handling this by using FLOOR. So, I would try all 6 combinations and just take the highest.
Notice on how I'm testing. I'm ALWAYS leaving the dimensions of the box to length,width,height respectively. Then, in the loop, All I'm doing is changing the TEST variable "l"ength, "w"idth, and "h"eight to each of the respective 6 combinations like a pick-3 lottery. By testing the integer of each will give its own value based on that way being laid in the box for stacking... whichever way has the most wins.
So, for pass 0/1, the LENGTH var stays the same, and h/w swap. in pass 2/3, the WIDTH is in the first position and swap l/h, finally in pass 4/5, the HEIGHT is in the first position and swap l/w. So all 6 combinations accounted for.
Example calls
totalBricks = BricksPerDimension(20, 70, 30, 1, 2, 5);
MessageBox.Show(totalBricks.ToString());
totalBricks = BricksPerDimension(25, 33, 40, 11, 10, 5);
MessageBox.Show(totalBricks.ToString());
And the updated function
private static int BricksPerDimension(int dimWidth, int dimLength, int dimHeight, int brickSideWidth, int brickSideLength, int brickSideHeight)
{
int maxBricks = 0;
int curBricks = 0;
int l, w, h;
l = w = h = 0;
for (int i = 0; i < 6; i++)
{
switch (i)
{
case 0:
l = brickSideLength;
h = brickSideHeight;
w = brickSideWidth;
break;
case 1:
l = brickSideLength;
w = brickSideHeight;
h = brickSideWidth;
break;
case 2:
w = brickSideLength;
l = brickSideHeight;
h = brickSideWidth;
break;
case 3:
w = brickSideLength;
h = brickSideHeight;
l = brickSideWidth;
break;
case 4:
h = brickSideLength;
l = brickSideHeight;
w = brickSideWidth;
break;
case 5:
h = brickSideLength;
w = brickSideHeight;
l = brickSideWidth;
break;
}
curBricks = ((int)dimLength / l)
* ((int)dimWidth / w)
* ((int)dimHeight / h);
if (curBricks > maxBricks)
maxBricks = curBricks;
}
return maxBricks;
}
Upvotes: 1
Reputation: 375
private static int BricksPerDimension(int dimWidth, int dimLength, int dimHeight, int brickSideWidth, int brickSideLength, int brickSideHeight)
{
int dimMax, dimMid, dimMin;
int brickSideMax, brickSideMid, brickSideMin;
//TODO: dimWidth, dimLength, dimHeight => dimMax, dimMid, dimMin
//so does brickSideMax, brickSideMid, brickSideMin.
if (dimMax < brickSideMax)
return 0;
if (dimMid < brickSideMid)
return 0;
if (dimMin < brickSideMin)
return 0;
int maxDirection = (int)Math.Floor((double)dimMax / brickSideMax);
int midDirection = (int)Math.Floor((double)dimMid / brickSideMid);
int minDirection = (int)Math.Floor((double)dimMin / brickSideMin);
return maxDirection * midDirection * minDirection;
{
Upvotes: 0