Submission fixes

This commit is contained in:
bluepython508
2023-12-14 09:56:12 +00:00
parent a7beb06a28
commit e24b45de16

View File

@@ -23,7 +23,7 @@ defmodule Mix.Tasks.Aoc do
defp tests(day) do
case File.ls(tests_dir(day)) do
{:ok, paths} ->
paths |> Enum.filter(&(not String.contains?(&1, "."))) |> Enum.sort
paths |> Enum.filter(&(not String.contains?(&1, "."))) |> Enum.sort()
{:error, e} ->
dbg(e)
@@ -119,22 +119,26 @@ defmodule Mix.Tasks.Aoc do
defp run(day, ["test", "watch"]) do
{:ok, pid} = Mix.Tasks.Aoc.Watcher.start_link({[dirs: ["lib/", "tests/day#{day}/"]], self()})
Process.monitor(pid)
loop = fn (loop) ->
loop = fn loop ->
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")
:code.purge(module(day))
:code.purge(Aoc2023.Common)
:code.load_file(module(day))
:code.load_file(Aoc2023.Common)
spawn fn ->
spawn(fn ->
run(day, ["test"])
end
end)
receive do
:update -> loop.(loop)
{:DOWN, _, :process, ^pid, _} -> nil
end
end
loop.(loop)
end
@@ -147,13 +151,21 @@ defmodule Mix.Tasks.Aoc do
|> Enum.map(fn {test, input, p1e, p2e} ->
parsed = mod.parse(input)
result = fn (part, got, expected) ->
result = fn part, got, expected ->
cond do
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 ->
IO.puts(IO.ANSI.format [:red, "Test #{test}.#{part} failed: expected #{expected}, got #{stringify(got)}"])
true -> IO.puts("Test #{test}.#{part}: #{stringify(got)}")
IO.puts(
IO.ANSI.format([
:red,
"Test #{test}.#{part} failed: expected #{expected}, got #{stringify(got)}"
])
)
true ->
IO.puts("Test #{test}.#{part}: #{stringify(got)}")
end
end
@@ -171,13 +183,24 @@ defmodule Mix.Tasks.Aoc do
run(day, ["fetch"])
mod = module(day)
parsed = mod.parse(read("#{base_dir()}/inputs/day#{day}"))
val = case part do
val =
case part do
"1" -> mod.part1(parsed)
"2" -> mod.part2(parsed)
end
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
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))
end
defmodule Watcher do
use GenServer
@@ -218,7 +240,10 @@ defmodule Mix.Tasks.Aoc do
{:ok, %{watcher_pid: watcher_pid, callback: callback}}
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)
{:noreply, state}
end