crypto-square

This commit is contained in:
2025-04-25 13:42:24 -04:00
parent ab3508e9fc
commit 5f594b12e3
2 changed files with 7 additions and 11 deletions

View File

@@ -19,13 +19,17 @@ defmodule CryptoSquare do
|> String.replace(~r/[^a-z0-9]/, "")
end
defp square(""), do: ""
defp square(str) do
length = String.length(str)
row = :math.ceil(length ** 0.5)
col = :math.ceil(length / row)
row = round(ceil(length ** 0.5))
str
|> String.split("", trim: true)
|> Enum.chunk_every(col, col)
|> Enum.chunk_every(row, row, List.duplicate(" ", row))
|> Enum.zip()
|> Enum.map(&Tuple.to_list(&1))
|> Enum.map_join(" ", &Enum.join(&1))
end
end