Created
December 1, 2018 19:38
-
-
Save natanbc/172159534a95e1146ff6155f23b19a15 to your computer and use it in GitHub Desktop.
Advent of Code runner. Reads input and calls each day, running all days asynchronously and printing outputs in order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Adventofcode do | |
@days 1 | |
def main(_) do | |
Enum.flat_map(1..@days, &run/1) | |
|> wait_for() | |
end | |
def run_day(day), do: run(day) |> wait_for() | |
defp wait_for([]), do: nil | |
defp wait_for([pid | pids]) do | |
receive do | |
{:result, ^pid, {day, part, result}} -> IO.puts("Day #{day} part #{part}: #{result}") | |
end | |
wait_for(pids) | |
end | |
defp run(day) do | |
[] |> maybe_run(day, 1) |> maybe_run(day, 2) | |
end | |
defp maybe_run(pids, day, part) do | |
parent = self() | |
mod = String.to_atom("Elixir.Tasks.Day#{day}") | |
fname = String.to_atom("part#{part}") | |
if function_exported?(mod, fname, 1) do | |
pid = spawn_link fn -> | |
res = apply(mod, fname, [ | |
File.read!("input/Day#{day}-#{part}.txt") | |
]) | |
send parent, {:result, self(), {day, part, res}} | |
end | |
pids ++ [pid] | |
else | |
pids | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment