DEV Community

Cover image for LEGO 3D Coordinates
Scott Gordon
Scott Gordon

Posted on

LEGO 3D Coordinates

The Grid Dimensions

  • Length (X): The number of blocks you can place in a row.
  • Width (Y): The number of blocks you can place in a column, perpendicular to the row.
  • Height (Z): The number of layers of blocks you can stack on top of each other.

Imagine you have a LEGO base that's 1 block wide, 1 block long, and you're allowed to stack up to 1 block high. This gives you a tiny (2 X 2 X 2) space to work with, because you start counting from 0 (so you have positions 0 and 1 in each dimension).

The Rule (N)

Now, let's say you have a rule: You can't place a LEGO block at any position where the sum of the coordinate's equals (N). For instance, if (N=2), you can't place a block at any spot where adding up the row number, column number, and stack height equals 2.

Visualizing Coordinates

Each spot where you could place a LEGO block has coordinates (i, j, k):

  • (i) is the position along the length of your base.
  • (j) is the position along the width.
  • (k) is the height or layer number.

Real-World Example

Let's apply this with our (1 X 1 X 1) model and (N=2):

  • You start placing blocks at (0, 0, 0), the very bottom corner.
  • You can also place blocks at (0, 0, 1), moving up one layer without changing the row or column.
  • However, you can't place a block at (0, 1, 1) because (0+1+1=2), and our rule forbids any spot where the coordinates add up to 2.

The List of Allowed Coordinates

Using a list comprehension to apply these rules, we find the allowed spots to place our LEGO blocks without breaking the (N=2) rule. For our tiny LEGO base, the possible placements, avoiding spots where the sum equals 2, might look like this: [(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 1)]. Each of these coordinates represents a spot where you can safely place a block according to the rules.

Visualization

Imagine laying out a grid of possible block positions on your LEGO base and then marking which spots are allowed under the rule. Each allowed spot gets a LEGO block, building a unique structure that adheres to your constraints.

This visualization takes the abstract concept of 3D coordinates and the rule-based exclusion and turns it into a tangible LEGO building exercise, making it easier to grasp how list comprehensions can generate and filter complex collections based on specific conditions.

Here is the code in Python using a list comprehension.

#!/usr/bin/python3
# lego_3d_coords.py

def lego_3d_coordinates(x, y, z, n):
    """
    This function will print all possible coordinates on the 3D grid within the
    specified dimensions, excluding those combinations where the sum of the dimenstions
    equals 'n'
    """
    coordinates = [[i, j, k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i + j + k != n]
    print(coordinates)

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    lego_3d_coordinates(x, y, z, n)
Enter fullscreen mode Exit fullscreen mode

Once you have created this script be sure to make it executable using chmod +x lego_3d_coords.py then run it in the same directory using ./lego_3d_coords.py

Also remember to add the coordinates input values using your preferred CLI once you run your Python script.

Photo by Xavi Cabrera on Unsplash

Top comments (0)