Qbasicnews.com

Full Version: Finding the number of distinguishable cubes based on color
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Recently I came upon a problem to do in QBASIC and I've had some trouble with it. It came from some computer contest or something of that nature. Well here it is:

Write a program to determine the number of distinguishable ways to place and orient a cube with a solid color on each of its six sides. The programs will ask for the one letter color symbol for each of the sides in the following order: TOP, FRONT, BOTTOM, BACK, RIGHT, LEFT. Output will be the statement, NUMBER OF DISTINGUISHABLE CUBES = ##, where ## is the number [of] different ways to position and orient the cube. Examples:

INPUT: Enter TOP side: Y
Enter FRONT side: Y
Enter BOTTOM side: Y
Enter BACK side: Y
Enter RIGHT side: Y
Enter LEFT side: Y
OUTPUT: NUMBER OF DISTINGUISHABLE CUBES = 1

INPUT: Enter TOP side: G
Enter FRONT side: G
Enter BOTTOM side: G
Enter BACK side: G
Enter RIGHT side: G
Enter LEFT side: Y
OUTPUT: NUMBER OF DISTINGUISHABLE CUBES = 6

INPUT: Enter TOP side: B
Enter FRONT side: B
Enter BOTTOM side: O
Enter BACK side: Y
Enter RIGHT side: G
Enter LEFT side: R
OUTPUT: NUMBER OF DISTINGUISHABLE CUBES = 24

I've had a little trouble coming up with the forumula to get the number of cubes from the colors and translating that into code. I've tried a few different ways but the program I have so far barely scratches the surface and I was wondering if I am heading in the right direction. Any help would be greatly appreciated:

'Cube problem
'Sides for equation
sides = 6
INPUT "Enter TOP side: ", top$
INPUT "Enter FRONT side: ", front$
INPUT "Enter BACK side: ", back$
INPUT "Enter RIGHT side: ", ight$
INPUT "Enter LEFT side: ", eft$
IF top$ AND front$ AND back$ AND ight$ AND eft$ <> "R" OR "O" OR "Y" OR "G" OR "B"
PRINT "Use only R, O, Y, G, or B"
WHILE INKEY$ = ""
WEND
'Reselect from menu
ELSEIF top$ = front$ OR top$ = back$ OR top$ = ight$ OR top$ = eft$ THEN


Thank you for any feedback, in the meantime I'll be coding away...
I like this problem. I'll post something helpful as soon as I can. This would be a good challenge for the "Programming Challenges" forum.

*peace*

Meg.

edit: I'm still working on some code for it, but my thoughts go like this:

6 sides on the cube, and each can be the top
for each top, the cube can face 4 directions.
therefore, there are 24 possible orientations.

represent each orientation with a 6-character long string.
(TOP)(BTM)(LFT)(RGT)(FRT)(BCK)

make an array(1 to 24) of 6-character long strings:

Code:
DIM positions(1 to 24) AS STRING * 6

fill the array with the color combinations.
write some kind of code that flags duplicate combos.
count the results.
Here's what I came up with. Let me know if any parts of it are hard to follow.

*peace*

Meg.

Code:
'THIS PROGRAM DETERMINES THE TOTAL NUMBER OF UNIQUE POSITIONS FOR A CUBE
'THAT CAN HAVE ITS SIX SIDES COLORED DIFFERENTLY.
'
'                                              - written 11/02/2004 by mcb

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'first we make a cube and have the user input colors.  I didn't bother
'error checking to make sure they only enter valid colors.  I imagine the
'cube as a die, and label the colored sides with the numbers as they are
'located on a die: 1 opposite 6, 4 opposite 3, and 5 opposite 2.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

DIM OriginalCube(1 TO 6) AS STRING * 1

CLS
INPUT "Enter TOP color    : ", OriginalCube(1)
INPUT "Enter BOTTOM color : ", OriginalCube(6)
INPUT "Enter LEFT color   : ", OriginalCube(4)
INPUT "Enter RIGHT color  : ", OriginalCube(3)
INPUT "Enter FRONT color  : ", OriginalCube(5)
INPUT "Enter BACK color   : ", OriginalCube(2)

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'now, consider that there are 6 sides of the cube which could be the top.
'for each unique top, there are 4 positions for the cube.  Therefore, the
'number of positions for the cube is 6 * 4 = 24.  So we'll make an array
'to store the 24 possible positions, and for each one we need the colors
'on the 6 faces.  So a 24x6 array will cover all possibilities.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

DIM RotatedCube(1 TO 24, 1 TO 6) AS STRING * 1

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'now the hard part.  we've got to examine each of the positions and
'see how the original faces are repositioned.  Check my comments on the
'DATA statements to see how I went about this.  I'm sure there's more
'efficient ways, but this is the most straightforward approach I could
'come up with.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

FOR position% = 1 TO 24
     FOR side% = 1 TO 6
          READ RotatedColor%
          RotatedCube(position%, side%) = OriginalCube(RotatedColor%)
        
          '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          'uncomment this line if you want the combinations printed out.
          '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          'PRINT RotatedCube(position%, side%);

     NEXT side%
NEXT position%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'now that we've got our array filled, we need to count up the number of
'unique combinations. I'll scan through the 24 positions and keep a
'counter rolling on each unique combination found.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

FOR position% = 1 TO 24
    
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     'concatenate the 6 colors on the rotated cube into one string.
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     currentcombo$ = ""
     FOR side% = 1 TO 6
          currentcombo$ = currentcombo$ + RotatedCube(position%, side%)
     NEXT side%

     foundmatch% = 0
     FOR check% = 1 TO position% - 1
        
          '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          'concatenate the 6 colors on the check cube into one string.
          '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          checkcombo$ = ""
          FOR side% = 1 TO 6
               checkcombo$ = checkcombo$ + RotatedCube(check%, side%)
          NEXT side%

          '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          'see if they match. if so, then current position has already
          'been encountered, so it's not a unique position and is ignored.
          '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          IF checkcombo$ = currentcombo$ THEN
               foundmatch% = 1
          END IF
    
     NEXT check%

     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     'No matches were found, so consider this a new combination.
     '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     IF foundmatch% = 0 THEN UniqueCombos% = UniqueCombos% + 1

NEXT position%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'and here's your final answer.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PRINT
PRINT "This cube has"; UniqueCombos%; "unique positions."

DO: LOOP UNTIL INKEY$ <> ""
SYSTEM

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'here's all the possible positions of the cube.  I used the numbers on
'a standard die to represent the 6 faces.  I then rotated the die to
'all 24 positions, and recorded the numbers in their new positions.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'    TpBtLfRtFrBk
DATA 1,6,4,3,5,2
DATA 1,6,5,2,3,4
DATA 1,6,3,4,2,5
DATA 1,6,2,5,4,3
DATA 2,5,4,3,1,6
DATA 2,5,1,6,3,4
DATA 2,5,3,4,6,1
DATA 2,5,6,1,4,3
DATA 3,4,1,6,5,2
DATA 3,4,5,2,6,1
DATA 3,4,6,1,2,5
DATA 3,4,2,5,1,6
DATA 4,3,2,5,6,1
DATA 4,3,6,1,5,2
DATA 4,3,5,2,1,6
DATA 4,3,1,6,2,5
DATA 5,2,4,3,6,1
DATA 5,2,6,1,3,4
DATA 5,2,3,4,1,6
DATA 5,2,1,6,4,3
DATA 6,1,4,3,2,5
DATA 6,1,2,5,3,4
DATA 6,1,3,4,5,2
DATA 6,1,5,2,4,3
Thanks for the help on this! I've been trying for awhile to get this and I didn't of using a DATA statement but after reading your program it all made sense. Thanks for helping me figure it out.
Glad I could help! This was a pretty fun proggy to write. (c >.³)b

*peace*

Meg.
Please interpret that smilie(if at all its a smilie)
(c >.³)b
it's a face turned to the side ( '.')
with an ear (c '.')
winking with its closer eye (c >.³)
and giving a thumbs up (c >.³)b

Now what's this one? t(-_- t)

*peace*

MB.
Quote:it's a face turned to the side ( '.')
with an ear (c '.')
winking with its closer eye (c >.³)
and giving a thumbs up (c >.³)b

Now what's this one? t(-_- t)

*peace*

MB.


:rotfl:

--pr0gger
Quote: t(-_- t)

*peace*

MB.

cant tell :oops: