Day 5: Hydrothermal Venture
Mix.install([:kino])
input = Kino.Input.textarea("Please paste your input:")
Setup
inputs =
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(fn input ->
input
|> String.split(" -> ", trim: true)
|> Enum.map(fn point ->
point |> String.split(",") |> Enum.map(&String.to_integer/1)
end)
end)
Part 1
https://adventofcode.com/2021/day/5
Solve: Part 1
inputs
|> Enum.reduce(%{}, fn input, point_count_map ->
new_points =
case input do
[[x, y1], [x, y2]] -> y1..y2 |> Enum.map(&[x, &1])
[[x1, y], [x2, y]] -> x1..x2 |> Enum.map(&[&1, y])
_ -> []
end
new_points
|> Enum.reduce(point_count_map, fn point, point_count_map ->
point_count_map |> Map.update(point, 1, &(&1 + 1))
end)
end)
|> Enum.count(fn {_key, value} -> value >= 2 end)
Part 2
https://adventofcode.com/2021/day/5#part2
Solve: Part 2
inputs
|> Enum.reduce(%{}, fn input, point_count_map ->
new_points =
case input do
[[x, y1], [x, y2]] -> y1..y2 |> Enum.map(&[x, &1])
[[x1, y], [x2, y]] -> x1..x2 |> Enum.map(&[&1, y])
[[x1, y1], [x2, y2]] -> Enum.zip_with(x1..x2, y1..y2, &[&1, &2])
end
new_points
|> Enum.reduce(point_count_map, fn point, point_count_map ->
point_count_map |> Map.update(point, 1, &(&1 + 1))
end)
end)
|> Enum.count(fn {_key, value} -> value >= 2 end)