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

闘魂 Elixir ── Rucksack Reorganization

rucksack_reorganization.livemd

闘魂 Elixir ── Rucksack Reorganization

# Mix.install([{:benchee, "~> 1.1"}])

About

Answers

input = """
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
"""
score = fn
  x when x in ?a..?z -> x - 96
  x when x in ?A..?Z -> x - 38
end

for row <- String.split(input, "\n", trim: true), reduce: 0 do
  sum ->
    {a, b} = String.split_at(row, div(String.length(row), 2))

    [intersection] =
      MapSet.intersection(MapSet.new(to_charlist(a)), MapSet.new(to_charlist(b)))
      |> MapSet.to_list()

    sum + score.(intersection)
end