Skip to content

Instantly share code, notes, and snippets.

@tfwright

tfwright/1.exs Secret

Last active December 3, 2022 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfwright/f00195bd2c7e6690b19997b9433cee84 to your computer and use it in GitHub Desktop.
Save tfwright/f00195bd2c7e6690b19997b9433cee84 to your computer and use it in GitHub Desktop.
priorities = String.graphemes("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
System.argv()
|> List.first()
|> File.stream!()
|> Enum.reduce(0, fn inventory, total ->
items = inventory |> String.trim() |> String.graphemes()
{compartment_1, compartment_2} = Enum.split(items, items |> Enum.count() |> Integer.floor_div(2))
duplicate_item = Enum.find(compartment_1, & Enum.member?(compartment_2, &1))
total + Enum.find_index(priorities, & &1 == duplicate_item) + 1
end)
|> IO.inspect()
priorities = String.graphemes("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
System.argv()
|> List.first()
|> File.stream!()
|> Enum.map(& &1 |> String.trim() |> String.graphemes() |> MapSet.new())
|> Enum.chunk_every(3)
|> Enum.reduce(0, fn group, total ->
badge =
group
|> Enum.reduce(&MapSet.intersection/2)
|> MapSet.to_list()
|> List.first()
total + Enum.find_index(priorities, & &1 == badge) + 1
end)
|> IO.inspect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment