Submission fixes
This commit is contained in:
@@ -23,7 +23,7 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
defp tests(day) do
|
defp tests(day) do
|
||||||
case File.ls(tests_dir(day)) do
|
case File.ls(tests_dir(day)) do
|
||||||
{:ok, paths} ->
|
{:ok, paths} ->
|
||||||
paths |> Enum.filter(&(not String.contains?(&1, "."))) |> Enum.sort
|
paths |> Enum.filter(&(not String.contains?(&1, "."))) |> Enum.sort()
|
||||||
|
|
||||||
{:error, e} ->
|
{:error, e} ->
|
||||||
dbg(e)
|
dbg(e)
|
||||||
@@ -119,22 +119,26 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
defp run(day, ["test", "watch"]) do
|
defp run(day, ["test", "watch"]) do
|
||||||
{:ok, pid} = Mix.Tasks.Aoc.Watcher.start_link({[dirs: ["lib/", "tests/day#{day}/"]], self()})
|
{:ok, pid} = Mix.Tasks.Aoc.Watcher.start_link({[dirs: ["lib/", "tests/day#{day}/"]], self()})
|
||||||
Process.monitor(pid)
|
Process.monitor(pid)
|
||||||
loop = fn (loop) ->
|
|
||||||
|
loop = fn loop ->
|
||||||
IO.puts(IO.ANSI.clear() <> IO.ANSI.cursor(0, 0))
|
IO.puts(IO.ANSI.clear() <> IO.ANSI.cursor(0, 0))
|
||||||
IO.puts "At #{DateTime.utc_now(:second)}:"
|
IO.puts("At #{DateTime.utc_now(:second)}:")
|
||||||
System.shell("mix compile")
|
System.shell("mix compile")
|
||||||
:code.purge(module(day))
|
:code.purge(module(day))
|
||||||
:code.purge(Aoc2023.Common)
|
:code.purge(Aoc2023.Common)
|
||||||
:code.load_file(module(day))
|
:code.load_file(module(day))
|
||||||
:code.load_file(Aoc2023.Common)
|
:code.load_file(Aoc2023.Common)
|
||||||
spawn fn ->
|
|
||||||
|
spawn(fn ->
|
||||||
run(day, ["test"])
|
run(day, ["test"])
|
||||||
end
|
end)
|
||||||
|
|
||||||
receive do
|
receive do
|
||||||
:update -> loop.(loop)
|
:update -> loop.(loop)
|
||||||
{:DOWN, _, :process, ^pid, _} -> nil
|
{:DOWN, _, :process, ^pid, _} -> nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
loop.(loop)
|
loop.(loop)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -147,13 +151,21 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
|> Enum.map(fn {test, input, p1e, p2e} ->
|
|> Enum.map(fn {test, input, p1e, p2e} ->
|
||||||
parsed = mod.parse(input)
|
parsed = mod.parse(input)
|
||||||
|
|
||||||
result = fn (part, got, expected) ->
|
result = fn part, got, expected ->
|
||||||
cond do
|
cond do
|
||||||
stringify(got) == expected ->
|
stringify(got) == expected ->
|
||||||
IO.puts(IO.ANSI.format [:green, "Test #{test}.#{part} succeeded (#{expected})"])
|
IO.puts(IO.ANSI.format([:green, "Test #{test}.#{part} succeeded (#{expected})"]))
|
||||||
|
|
||||||
expected ->
|
expected ->
|
||||||
IO.puts(IO.ANSI.format [:red, "Test #{test}.#{part} failed: expected #{expected}, got #{stringify(got)}"])
|
IO.puts(
|
||||||
true -> IO.puts("Test #{test}.#{part}: #{stringify(got)}")
|
IO.ANSI.format([
|
||||||
|
:red,
|
||||||
|
"Test #{test}.#{part} failed: expected #{expected}, got #{stringify(got)}"
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
|
true ->
|
||||||
|
IO.puts("Test #{test}.#{part}: #{stringify(got)}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -171,13 +183,24 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
run(day, ["fetch"])
|
run(day, ["fetch"])
|
||||||
mod = module(day)
|
mod = module(day)
|
||||||
parsed = mod.parse(read("#{base_dir()}/inputs/day#{day}"))
|
parsed = mod.parse(read("#{base_dir()}/inputs/day#{day}"))
|
||||||
val = case part do
|
|
||||||
"1" -> mod.part1(parsed)
|
val =
|
||||||
"2" -> mod.part2(parsed)
|
case part do
|
||||||
end
|
"1" -> mod.part1(parsed)
|
||||||
|
"2" -> mod.part2(parsed)
|
||||||
|
end
|
||||||
|
|
||||||
HTTPoison.start()
|
HTTPoison.start()
|
||||||
resp = HTTPoison.post!("https://adventofcode.com/2023/day/#{day}/answer", {:form, [level: part, answer: stringify(val)]}, "user-agent": "aoc-ex by ben@soroos.net", cookie: "session=#{System.get_env("AOC_SESSION")}")
|
|
||||||
resp |> dbg
|
resp =
|
||||||
|
HTTPoison.post!(
|
||||||
|
"https://adventofcode.com/2023/day/#{day}/answer",
|
||||||
|
"level=#{part}&answer=#{stringify(val)}",
|
||||||
|
"user-agent": "aoc-ex by ben@soroos.net",
|
||||||
|
cookie: "session=#{System.get_env("AOC_SESSION")}",
|
||||||
|
"content-type": "application/x-www-form-urlencoded"
|
||||||
|
).body
|
||||||
|
Regex.named_captures(~r[<main>(?<main>.*)</main>]is, resp)["main"] |> IO.puts
|
||||||
end
|
end
|
||||||
|
|
||||||
defp run(day, ["run", "-"]) do
|
defp run(day, ["run", "-"]) do
|
||||||
@@ -204,7 +227,6 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
module(day).main(if file, do: read(file), else: IO.read(:stdio, :eof))
|
module(day).main(if file, do: read(file), else: IO.read(:stdio, :eof))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
defmodule Watcher do
|
defmodule Watcher do
|
||||||
use GenServer
|
use GenServer
|
||||||
|
|
||||||
@@ -218,7 +240,10 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
{:ok, %{watcher_pid: watcher_pid, callback: callback}}
|
{:ok, %{watcher_pid: watcher_pid, callback: callback}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({:file_event, watcher_pid, {_path, _events}}, %{watcher_pid: watcher_pid, callback: callback} = state) do
|
def handle_info(
|
||||||
|
{:file_event, watcher_pid, {_path, _events}},
|
||||||
|
%{watcher_pid: watcher_pid, callback: callback} = state
|
||||||
|
) do
|
||||||
send(callback, :update)
|
send(callback, :update)
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user