{list1, list2} =
  File.read!("../input.txt")
  |> String.split("\n")
  |> Enum.reduce({[], []}, fn line, {list1, list2} ->
    Regex.named_captures(~r/(?<left>\d+)\s+(?<right>\d+)/, line)
    |> case do
      %{"left" => left, "right" => right} ->
        {[String.to_integer(left) | list1], [String.to_integer(right) | list2]}

      _ ->
        {list1, list2}
    end
  end)

# list1, pair with similarity score
pairs =
  Enum.map(list1, fn value ->
    number_of_times_in_list2 =
      Enum.filter(list2, &(&1 == value))
      |> Enum.count()

    {value, value * number_of_times_in_list2}
  end)

total_similarity =
  Enum.reduce(pairs, 0, fn {_value, score}, total_similarity ->
    total_similarity + score
  end)

IO.puts("total similarity = #{total_similarity}")
