This commit is contained in:
Shadowfacts 2020-12-06 10:25:55 -05:00
parent ac51a553e6
commit 75745b42b7
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 2298 additions and 0 deletions

61
lib/day6/day6.ex Normal file
View File

@ -0,0 +1,61 @@
defmodule Day6 do
@example """
abc
a
b
c
ab
ac
a
a
a
a
b
"""
def part1(example \\ false) do
parse_input(example)
|> Enum.map(&questions_with_any_yes/1)
|> Enum.map(&MapSet.size/1)
|> Enum.sum()
end
def part2(example \\ false) do
parse_input(example)
|> Enum.map(&questions_with_all_yes/1)
|> Enum.map(&MapSet.size/1)
|> Enum.sum()
end
def parse_input(example) do
if(example, do: @example, else: File.read!("lib/day6/input.txt"))
|> String.trim()
|> String.split("\n\n")
end
def questions_with_any_yes(str) do
str
|> String.replace(~r/\s/, "")
|> String.to_charlist()
|> MapSet.new()
end
def questions_with_all_yes(str) do
people =
str
|> String.split("\n")
|> Enum.map(fn person ->
person
|> String.to_charlist()
|> MapSet.new()
end)
people
|> Enum.drop(1)
|> Enum.reduce(List.first(people), &MapSet.intersection/2)
end
end

2237
lib/day6/input.txt Normal file

File diff suppressed because it is too large Load Diff