advent_of_code/lib/util/string.ex

24 lines
467 B
Elixir

defmodule AoC.Util.String do
@doc """
Transposes the string when it represents
a grid of newline separated rows where each
character is a new column.
iex> AoC.Util.String.transpose(
iex> ~s(abc
iex>def
iex>ghi)
iex>)
~s(adg
beh
cfi)
"""
def transpose(str) do
AoC.Util.Enum.transpose(
str
|> String.split("\n")
|> Enum.map(&String.graphemes(&1))
)|> Enum.map(&Enum.join(&1,"")) |> Enum.join("\n")
end
end