Algorithm Question: Stacking bricks of different colours?
Let $n$ be the number of bricks and $c_i$ be the number of bricks of color $i$.
First notice that in a $w \times h$ grid we can fit at most $wh$ bricks.
Therefore, $\lceil \frac{n}{Y} \rceil$ is a lower bound on your height. The
minimum height $h$ to stack all your bricks without violating the constraint will be
$\max(\max_i(c_i), \lceil \frac{n}{Y} \rceil)$. 

The idea is to go from left to right (column 1 to X) and keep pushing a brick of the same color on to the left most non-full stack. And by non-full I mean that the size of the stack is lower than our defined height $h$. No two colors will be in the same row and it is also obviously true, that we have enough room for all $n$ bricks. 

A pseudo code is also provided:

color_count[i] = number of bricks of color i
height = max(max(color_count), ceil(n / Y))

current_column = 1
current_row = 1
current_color = 1
while current_color &lt;= X
  while color_count[current_color] &gt; 0
    put a brick at (current_row, current_column) with current_color
    color_count[current_color] -= 1
    if current_row == height
      current_column += 1
      current_row = 1
    else
      current_row += 1
  current_color += 1