   """
   @spec total(basket :: [book]) :: integer
   def total(basket) do
-    # We use an Agent to memoize the discounts
-    {:ok, _pid} = Agent.start_link(fn -> %{} end, name: :discount)
-
     basket
     |> frequencies
     |> Enum.sort()

defmodule BookStore do
  use Agent
  @type book :: integer

  @base_price 800

  def discounts([n]), do: [n * @base_price]

  def discounts(frequencies) do
    for num_books <- 2..5,
        {picked, left} <- pick_n_distinct_from(num_books, frequencies),
        remove_picked = picked |> Enum.map(&(&1 - 1)) |> Enum.reject(&(&1 == 0)),
        costs <- discounts(Enum.sort(remove_picked ++ left)),
        do: costs + num_books * @discount[num_books]
  end

  def pick_n_distinct_from(n, list) when length(list) < n, do: %{}
