Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Add more solutions

update_hugging_face_dataset.livemd

Add more solutions

Mix.install([
  :req,
  :jason
])

Load all advent of code from hugging face

dataset_url = "https://huggingface.co/datasets/isavita/advent-of-code/resolve/main/train.json"
dataset = Req.get!(dataset_url).body

dataset =
  dataset
  |> Enum.sort_by(&{&1["year"], String.length(&1["name"]), &1["name"]})

Enum.count(dataset)
new_solutions_path = ~s|#{System.get_env("HOME")}/code/advent_generated/|
lang_ext = ".zig"
lang = "zig"

data =
  Enum.reduce(dataset, [], fn solution, acc ->
    acc = [solution | acc]

    if solution["solution_lang"] == "go" do
      path = (new_solutions_path <> solution["name"] <> lang_ext) |> IO.inspect()

      case File.read(path) do
        {:ok, new_solution} ->
          new_solution = String.trim(new_solution)

          if Enum.all?(dataset, fn s -> s["solution"] != new_solution end) do
            [%{solution | "solution_lang" => lang, "solution" => new_solution} | acc]
          else
            acc
          end

        _ ->
          acc
      end
    else
      acc
    end
  end)
  |> Enum.reverse()
{Enum.count(dataset), Enum.count(data)}
output_path = "#{File.cwd!()}/git/advent-of-code/train.json"
File.write(output_path, Jason.encode!(data))