Day 6 improved

This commit is contained in:
Shadowfacts 2020-12-06 11:55:10 -05:00
parent 75745b42b7
commit 7b67ec3c39
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 12 additions and 22 deletions

View File

@ -18,35 +18,25 @@ defmodule Day6 do
"""
def part1(example \\ false) do
parse_input(example)
|> Enum.map(&questions_with_any_yes/1)
|> Enum.map(&MapSet.size/1)
|> Enum.sum()
get_answer(example, &MapSet.union/2)
end
def part2(example \\ false) do
parse_input(example)
|> Enum.map(&questions_with_all_yes/1)
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 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
def answers_to_count(group, combiner) do
people =
str
group
|> String.split("\n")
|> Enum.map(fn person ->
person
@ -56,6 +46,6 @@ defmodule Day6 do
people
|> Enum.drop(1)
|> Enum.reduce(List.first(people), &MapSet.intersection/2)
|> Enum.reduce(List.first(people), combiner)
end
end