Skip to content

Instantly share code, notes, and snippets.

@jhonnymoreira
Last active May 29, 2018 05:31
Show Gist options
  • Save jhonnymoreira/d2f10e45ac9dc4894e3d4ded1dfe9957 to your computer and use it in GitHub Desktop.
Save jhonnymoreira/d2f10e45ac9dc4894e3d4ded1dfe9957 to your computer and use it in GitHub Desktop.
rows = [
[9, 0, 5, 0, 0, 8, 0, 0, 2],
[0, 0, 8, 0, 0, 0, 5, 0, 0],
[0, 4, 0, 0, 3, 5, 0, 6, 9],
[4, 0, 3, 7, 0, 2, 0, 0, 0],
[0, 0, 7, 0, 4, 0, 2, 0, 0],
[0, 0, 0, 3, 0, 9, 6, 0, 4],
[2, 8, 0, 4, 9, 0, 0, 1, 0],
[0, 0, 9, 0, 0, 0, 4, 0, 0],
[5, 0, 0, 8, 0, 0, 7, 0, 6]
]
rows.map { |row| row.each_slice(3).to_a }
.transpose
.map { |row| row.each_slice(3).map(&:flatten) }
.transpose
.flatten(1)
# [
# [9, 0, 5, 0, 0, 8, 0, 4, 0],
# [0, 0, 8, 0, 0, 0, 0, 3, 5],
# [0, 0, 2, 5, 0, 0, 0, 6, 9],
# [4, 0, 3, 0, 0, 7, 0, 0, 0],
# [7, 0, 2, 0, 4, 0, 3, 0, 9],
# [0, 0, 0, 2, 0, 0, 6, 0, 4],
# [2, 8, 0, 0, 0, 9, 5, 0, 0],
# [4, 9, 0, 0, 0, 0, 8, 0, 0],
# [0, 1, 0, 4, 0, 0, 7, 0, 6]
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment