Compare commits
1 Commits
75745b42b7
...
296c2e2e15
Author | SHA1 | Date |
---|---|---|
Shadowfacts | 296c2e2e15 |
|
@ -0,0 +1,65 @@
|
|||
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()
|
||||
|> Enum.into(MapSet.new())
|
||||
end
|
||||
|
||||
def questions_with_all_yes(str) do
|
||||
str
|
||||
|> String.split("\n")
|
||||
|> Enum.map(fn person ->
|
||||
person
|
||||
|> String.to_charlist()
|
||||
|> MapSet.new()
|
||||
end)
|
||||
|> Enum.reduce(nil, fn person, acc ->
|
||||
case acc do
|
||||
nil ->
|
||||
person
|
||||
|
||||
_ ->
|
||||
MapSet.intersection(acc, person)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue