AoC20/lib/day6/day6.ex

52 lines
843 B
Elixir
Raw Permalink Normal View History

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