-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_06.ex
36 lines (30 loc) · 883 Bytes
/
day_06.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule AdventOfCode.Y2020.Day06 do
@moduledoc """
--- Day 6: Custom Customs ---
Problem Link: https://adventofcode.com/2020/day/6
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2020, 6)
def run(input \\ input()) do
input = parse(input)
{reducer(input, &answers/1), reducer(input, &unanimous_answers/1)}
end
def reducer(input, mapper), do: Enum.reduce(input, 0, &(mapper.(&1) + &2))
def parse(input) do
input
|> String.split(~r{\n\n|\r\r|\r\n\r\n})
|> Enum.map(&Transformers.lines/1)
end
defp answers(group) do
group
|> Enum.flat_map(&String.graphemes/1)
|> Enum.uniq()
|> Enum.count()
end
defp unanimous_answers(group) do
group
|> Enum.map(&MapSet.new(String.graphemes(&1)))
|> Enum.reduce(&MapSet.intersection/2)
|> Enum.count()
end
end