52 lines
843 B
Elixir
52 lines
843 B
Elixir
defmodule Day6 do
|
|
@example """
|
|
abc
|
|
|
|
a
|
|
b
|
|
c
|
|
|
|
ab
|
|
ac
|
|
|
|
a
|
|
a
|
|
a
|
|
a
|
|
|
|
b
|
|
"""
|
|
|
|
def part1(example \\ false) do
|
|
get_answer(example, &MapSet.union/2)
|
|
end
|
|
|
|
def part2(example \\ false) do
|
|
get_answer(example, &MapSet.intersection/2)
|
|
end
|
|
|
|
def get_answer(example, combiner) do
|
|
if(example, do: @example, else: File.read!("lib/day6/input.txt"))
|
|
|> String.trim()
|
|
|> String.split("\n\n")
|
|
|> Enum.map(fn group -> answers_to_count(group, combiner) end)
|
|
|> Enum.map(&MapSet.size/1)
|
|
|> Enum.sum()
|
|
end
|
|
|
|
def answers_to_count(group, combiner) do
|
|
people =
|
|
group
|
|
|> String.split("\n")
|
|
|> Enum.map(fn person ->
|
|
person
|
|
|> String.to_charlist()
|
|
|> MapSet.new()
|
|
end)
|
|
|
|
people
|
|
|> Enum.drop(1)
|
|
|> Enum.reduce(List.first(people), combiner)
|
|
end
|
|
end
|