diff --git a/day6/day6part1.exs b/day6/day6part1.exs index 1fa52b1..7122d42 100644 --- a/day6/day6part1.exs +++ b/day6/day6part1.exs @@ -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() diff --git a/day6/day6part2.exs b/day6/day6part2.exs index 7c376f8..34ad11f 100644 --- a/day6/day6part2.exs +++ b/day6/day6part2.exs @@ -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()