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\nMvQBJMBQhjQFNFnjnj\ndtlZmRtLmjSTSLLtTtNVwWzDRzDVwwWFwnNn\ndmmLCqTdcLqtLGqjBhpfHqBGpv\nPBPRhjTPPlLRBvlvfwffqJGfpG\nrHtMtrszFtSgbFrrggrFgMnwWGzmQqWvGWzGQpJGfNqqNz\nFggcbSMntVgMdRCwZcjChLCT\nlCqqBlCwlnDqPZTZZBLNdjJLwttNWjjdzJzc\nfVfMbvbvmbVsmSsmMVWNtzzcjgLWgjztMMtg\nVVmFhFRSfbQsvVQmvSfhSsmzHlCZqrrBrDBrHZPRTZnnzB\nCRrDWmzRRQMmDqrrBgBQmtHljhHwtwlwplcBjHGwwB\nPWfPSWnvsNZS" <> ...
Part 1
defmodule Day03 do
def priority(char) do
ascii = String.to_charlist(char) |> List.first()
if String.upcase(char) == char do
ascii - ?A + 27
else
ascii - ?a + 1
end
end
end
{:module, Day03, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:priority, 1}}
correct = 157
res =
input_test
# split lines
|> String.split("\n", trim: true)
# split rucksack
|> Enum.map(fn s -> String.split_at(s, Kernel.trunc(String.length(s) / 2)) end)
# lists of lists
|> Enum.map(fn {a, b} -> {String.graphemes(a), String.graphemes(b)} end)
# find similar
|> Enum.map(fn {a, b} -> Enum.map(a, fn c -> {c, c in b} end) end)
# filter away all not similar
|> Enum.map(fn r -> Enum.filter(r, fn {_, val} -> val end) end)
# grab only char
|> Enum.map(fn r -> Enum.map(r, fn {c, _} -> c end) |> List.first() end)
# convert to int
|> Enum.map(fn c -> Day03.priority(c) end)
|> Enum.sum()
IO.puts("Correct: #{correct}")
correct === res
Correct: 157
true
correct = 8123
res =
input
|> String.split("\n", trim: true)
|> Enum.map(fn s -> String.split_at(s, Kernel.trunc(String.length(s) / 2)) end)
|> Enum.map(fn {a, b} -> {String.graphemes(a), String.graphemes(b)} end)
|> Enum.map(fn {a, b} -> Enum.map(a, fn c -> {c, c in b} end) end)
|> Enum.map(fn r -> Enum.filter(r, fn {_, val} -> val end) end)
|> Enum.map(fn r -> Enum.map(r, fn {c, _} -> c end) |> List.first() end)
|> Enum.map(fn c -> Day03.priority(c) end)
|> Enum.sum()
IO.puts("Correct: #{correct}")
correct === res
Correct: 8123
true
Intro - Part 2
As you finish identifying the misplaced items, the Elves come to you with another issue.
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a group’s badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves will be carrying any other item type.
The problem is that someone forgot to put this year’s updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached.
Additionally, nobody wrote down which item type corresponds to each group’s badges. The only way to tell which item type is the right one is by finding the one item type that is common between all three Elves in each group.
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group’s rucksacks are the first three lines:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
And the second group’s rucksacks are the next three lines:
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
In the first group, the only item type that appears in all three rucksacks is lowercase r; this must be their badges. In the second group, their badge item type must be Z.
Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (r) for the first group and 52 (Z) for the second group. The sum of these is 70.
Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types?
Solution - Part 2
correct = 70
res =
input_test
|> String.split("\n", trim: true)
# group elves
|> Enum.chunk_every(3)
|> Enum.map(fn [a, b, c] -> [String.graphemes(a), String.graphemes(b), String.graphemes(c)] end)
# similar
|> Enum.map(fn [a, b, c] -> Enum.map(a, fn char -> {char, char in b and char in c} end) end)
|> Enum.map(fn r -> Enum.filter(r, fn {_, val} -> val end) end)
|> Enum.map(fn r -> Enum.map(r, fn {c, _} -> c end) |> List.first() end)
|> Enum.map(fn c -> Day03.priority(c) end)
|> Enum.sum()
IO.puts("Correct: #{correct}")
correct === res
Correct: 70
true
correct = 2620
res =
input
|> String.split("\n", trim: true)
|> Enum.chunk_every(3)
|> Enum.map(fn [a, b, c] -> [String.graphemes(a), String.graphemes(b), String.graphemes(c)] end)
|> Enum.map(fn [a, b, c] -> Enum.map(a, fn char -> {char, char in b and char in c} end) end)
|> Enum.map(fn r -> Enum.filter(r, fn {_, val} -> val end) end)
|> Enum.map(fn r -> Enum.map(r, fn {c, _} -> c end) |> List.first() end)
|> Enum.map(fn c -> Day03.priority(c) end)
|> Enum.sum()
IO.puts("Correct: #{correct}")
correct === res
Correct: 2620
true