arrangeTiles rMax tiles = fst $ head $ foldM arrange (M.empty, tiles) locations
where locations = init $ scanl nextLoc (0, 0) tiles
nextLoc (r, c) _ = if c == rMax then (r + 1, 0) else (r, c + 1)
arrangeTiles rMax tiles = fst $ head $ foldM arrange (M.empty, tiles) locations
where locations = init $ scanl nextLoc (0, 0) tiles
nextLoc (r, c) _ = if c == rMax then (r + 1, 0) else (r, c + 1)