clean up runner
This commit is contained in:
parent
bac2473cd9
commit
c9ff65d949
1 changed files with 30 additions and 22 deletions
|
@ -1,28 +1,36 @@
|
||||||
defmodule AOCRunner do
|
defmodule AOCRunner do
|
||||||
defp is_empty(s) do
|
@spec is_empty?(s :: String.t()) :: boolean()
|
||||||
(s |> String.trim() |> String.length()) == 0
|
defp is_empty?(s) do
|
||||||
|
String.trim(s) |> String.length() == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(advent, part) do
|
@spec both_empty?(a :: String.t(), b :: String.t()) :: boolean()
|
||||||
{_, state} = IO.stream() |>
|
defp both_empty?(a, b) do
|
||||||
Enum.reduce_while(
|
is_empty?(a) && is_empty?(b)
|
||||||
{nil, advent.init_state(part)},
|
end
|
||||||
fn line, acc -> line
|
|
||||||
|> then(fn line ->
|
|
||||||
{u, state} = acc
|
|
||||||
{u, advent.execute(line, state)}
|
|
||||||
end)
|
|
||||||
|> then(fn {u, s} ->
|
|
||||||
if (line |> is_empty()) && (u |> is_empty()) do
|
|
||||||
{:halt, {u, s}}
|
|
||||||
else
|
|
||||||
{:cont, {u, s}}
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|> then(fn {reduce_atom, {_, state}} -> {reduce_atom, {String.trim(line), state}} end)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
IO.puts(advent.get_answer(state))
|
@spec run(advent :: Advent, part :: :part1 | :part2) :: nil
|
||||||
|
def run(advent, part) do
|
||||||
|
|
||||||
|
# runs the advent execute method with the current line,
|
||||||
|
# until two empty lines are sent
|
||||||
|
run_until_exit = fn line, acc ->
|
||||||
|
{_, state} = acc
|
||||||
|
|
||||||
|
put_elem(acc, 1, advent.execute(line, state)) |>
|
||||||
|
then(fn {last_line, s} ->
|
||||||
|
{(if both_empty?(line, last_line), do: :halt, else: :cont), {String.trim(line), s}}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
IO.stream() |>
|
||||||
|
Enum.reduce_while(
|
||||||
|
{nil, advent.init_state(part)},
|
||||||
|
&run_until_exit.(&1, &2)) |>
|
||||||
|
elem(1) |>
|
||||||
|
advent.get_answer() |>
|
||||||
|
IO.puts()
|
||||||
|
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue