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