getDrawCols :: [Gate] -> [[Gate]]
getDrawCols = (\(x,y) -> x ++ [y]) . foldl' colFold ([[]],[])
  where colFold (res,curr) next =
          if fits (map gateToRange curr) (gateToRange next)
          then (res, next : curr)
          else (res ++ [curr], [next])
        gateToRange g =
          case g of
            Not n -> (n,n)
            Cnot n1 n2 -> (max n1 n2 , min n1 n2)
            Toff n1 n2 n3 -> (maximum [n1, n2, n3] , minimum [n1, n2, n3])
            Fred n1 n2 n3 -> (maximum [n1, n2, n3] , minimum [n1, n2, n3])
            Swap n1 n2 -> (max n1 n2 , min n1 n2)
            Hadamard n -> (n,n)
        fits [] _ = True
        fits rs r = all (notInRange r) rs
          where notInRange x y = minT x > maxT y || maxT x < minT y
                maxT = uncurry max
                minT = uncurry min
getDrawCols :: [Gate] -> [[Gate]]
getDrawCols = (\(x,y) -> x ++ [y]) . foldl' colFold ([[]],[])
  where colFold (res,curr) next =
          if fits (map gateToRange curr) (gateToRange next)
          then (res, next : curr)
          else (res ++ [curr], [next])
        gateToRange g =
          case g of
            Not n -> (n,n)
            Cnot n1 n2 -> (max n1 n2 , min n1 n2)
            Toff n1 n2 n3 -> (maximum [n1, n2, n3] , minimum [n1, n2, n3])
            Fred n1 n2 n3 -> (maximum [n1, n2, n3] , minimum [n1, n2, n3])
            Swap n1 n2 -> (max n1 n2 , min n1 n2)
            Hadamard n -> (n,n)
        fits [] _ = True
        fits rs r = all (notInRange r) rs
          where notInRange x y = minT x > maxT y || maxT x < minT y
                maxT = uncurry max
                minT = uncurry min
getDrawCols = (\(x,y) -> x ++ [y]) . foldl' colFold ([[]],[])
  where colFold (res,curr) next =
          if fits (map gateToRange curr) (gateToRange next)
          then (res, next : curr)
          else (res ++ [curr], [next])
        gateToRange g =
          case g of
            Not n -> (n,n)
            Cnot n1 n2 -> (max n1 n2 , min n1 n2)
            Toff n1 n2 n3 -> (maximum [n1, n2, n3] , minimum [n1, n2, n3])
            Fred n1 n2 n3 -> (maximum [n1, n2, n3] , minimum [n1, n2, n3])
            Swap n1 n2 -> (max n1 n2 , min n1 n2)
            Hadamard n -> (n,n)
        fits [] _ = True
        fits rs r = all (notInRange r) rs
          where notInRange x y = minT x > maxT y || maxT x < minT y
                maxT = uncurry max
                minT = uncurry min