Day 5
Part 1
input =
IO.getn("input: ", 1_000_000)
|> String.split("\n", trim: true)
defmodule AdventOfCode.DayFive do
def solve_part_one(input) do
input
|> parse()
|> Enum.filter(fn [first, second] ->
elem(first, 0) == elem(second, 0) ||
elem(first, 1) == elem(second, 1)
end)
|> solve()
end
def solve_part_two(input) do
input
|> parse()
|> solve()
end
defp parse(input) do
input
|> Enum.map(fn pair ->
[first, second] = String.split(pair, " -> ")
[x1, y1] =
String.split(first, ",")
|> Enum.map(fn el ->
{number, _rest} = Integer.parse(el)
number
end)
[x2, y2] =
String.split(second, ",")
|> Enum.map(fn el ->
{number, _rest} = Integer.parse(el)
number
end)
[{x1, y1}, {x2, y2}]
end)
end
defp solve(input) do
input
|> Enum.map(fn [{x1, y1}, {x2, y2}] ->
cond do
x1 == x2 ->
y1..y2 |> Enum.map(fn el -> {x1, el} end)
y1 == y2 ->
x1..x2 |> Enum.map(fn el -> {el, y1} end)
true ->
Enum.zip(x1..x2, y1..y2)
end
end)
|> List.flatten()
|> Enum.frequencies()
|> Enum.filter(fn {_pair, freq} -> freq >= 2 end)
|> Enum.count()
end
end
AdventOfCode.DayFive.solve_part_one(input)
Part 2
AdventOfCode.DayFive.solve_part_two(input)