Rework with File.stream!/1

This commit is contained in:
Adam Millerchip 2020-12-06 15:45:22 +09:00
parent 07773441f7
commit 45292967af
2 changed files with 10 additions and 16 deletions

View file

@ -1,13 +1,13 @@
defmodule Day6Part1 do
def run do
File.read!("input")
|> String.trim()
|> String.split("\n\n")
File.stream!("input")
|> Stream.chunk_by(&(&1 == "\n"))
|> Stream.map(fn group ->
group
|> String.replace("\n", "")
|> Enum.join()
|> String.split("", trim: true)
|> Enum.frequencies()
|> Map.delete("\n")
|> Enum.count()
end)
|> Enum.sum()

View file

@ -1,19 +1,13 @@
defmodule Day6Part2 do
def run do
File.read!("input")
|> String.trim()
|> String.split("\n\n")
File.stream!("input")
|> Stream.chunk_by(&(&1 == "\n"))
|> Stream.map(fn group ->
count =
group
|> String.to_charlist()
|> Enum.count(&(&1 == ?\n))
|> Kernel.+(1)
frequencies = group |> Enum.join() |> String.split("", trim: true) |> Enum.frequencies()
group
|> String.replace("\n", "")
|> String.split("", trim: true)
|> Enum.frequencies()
count = frequencies["\n"]
Map.delete(frequencies, "\n")
|> Enum.count(fn {_, v} -> v == count end)
end)
|> Enum.sum()