Files
aoc-2023/lib/common.ex
bluepython508 683522ddc0 Day 5
2023-12-05 21:23:38 +00:00

42 lines
858 B
Elixir

defmodule Aoc2023.Common do
def stringify(s) when is_binary(s) do s end
def stringify(n) when is_integer(n) do inspect(n) end
def stringify(nil), do: "nil"
def map_nth(tup, n, fun) do
put_elem(tup, n, fun.(elem(tup, n)))
end
defmacro pipe(expr) do
quote do
fn (val) -> val |> unquote(expr) end
end
end
defmacro l &&& r do
quote do
fn val ->
l = val |> unquote(l)
r = val |> unquote(r)
{l, r}
end
end
end
def runs(lst, f) do
lst
|> Enum.with_index()
|> Enum.chunk_by(pipe(elem(0) |> f.()))
|> Enum.filter(pipe(Enum.at(0) |> elem(0) |> f.()))
|> Enum.map(Enum.at(0) |> elem(1) &&& length)
end
def id(x), do: x
def const(_, x), do: x
def lines(x), do: String.split(x, "\n")
def head([head | _]), do: head
def tail([_ | tail]), do: tail
end