AoC 2021 - Day 5: Hydrothermal Venture
Setup
Mix.install([
{:kino, "~> 0.4.0"},
{:vega_lite, "~> 0.1.1"}
])
input = Kino.Input.textarea("Please paste your input:")
Part 1
You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.
They tend to form in lines; the submarine helpfully produces a list of nearby lines of vents (your puzzle input) for you to review. For example:
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
Each line of vents is given as a line segment in the format x1,y1 -> x2,y2
where x1
,y1
are the coordinates of one end the line segment and x2
,y2
are the coordinates of the other end. These line segments include the points at both ends. In other words:
-
An entry like
1,1 -> 1,3
covers points1,1
,1,2
, and1,3
. -
An entry like
9,7 -> 7,7
covers points9,7
,8,7
, and7,7
.
For now, only consider horizontal and vertical lines: lines where either x1 = x2
or y1 = y2
.
So, the horizontal and vertical lines from the above list would produce the following diagram:
.......1..
..1....1..
..1....1..
.......1..
.112111211
..........
..........
..........
..........
222111....
In this diagram, the top left corner is 0,0
and the bottom right corner is 9,9
. Each position is shown as the number of lines which cover that point or .
if no line covers that point. The top-left pair of 1
s, for example, comes from 2,2 -> 2,1
; the very bottom row is formed by the overlapping lines 0,9 -> 5,9
and 0,9 -> 2,9
.
To avoid the most dangerous areas, you need to determine the number of points where at least two lines overlap. In the above example, this is anywhere in the diagram with a 2
or larger - a total of 5
points.
Consider only horizontal and vertical lines. At how many points do at least two lines overlap?
segments =
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line
|> String.split([",", " -> "])
|> Enum.map(&String.to_integer/1)
end)
We are going to plot the points on a grid. We’re only interested in horizontal and vertical lines, so we can use pattern matching to find those segments:
[x, y1, x, y2] = [2, 1, 2, 3]
The above is a horizontal line because the x
is the same. Vertical lines are given by matching y
.
defmodule Counter1 do
def overlaps(segments) do
segments
|> Enum.reduce(%{}, fn
[x, y1, x, y2], grid ->
sum_grid(Stream.cycle([x]), y1..y2, grid)
[x1, y, x2, y], grid ->
sum_grid(x1..x2, Stream.cycle([y]), grid)
_segment, grid ->
grid
end)
|> Enum.count(fn {_, v} -> v > 1 end)
end
defp sum_grid(xs, ys, grid) do
Enum.reduce(Enum.zip(xs, ys), grid, fn point, grid ->
Map.update(grid, point, 1, &(&1 + 1))
end)
end
end
Counter1.overlaps(segments)
Part 2
Unfortunately, considering only horizontal and vertical lines doesn’t give you the full picture; you need to also consider diagonal lines.
Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:
-
An entry like
1,1 -> 3,3
covers points1,1
,2,2
, and3,3
. -
An entry like
9,7 -> 7,9
covers points9,7
,8,8
, and7,9
.
Considering all lines from the above example would now produce the following diagram:
1.1....11.
.111...2..
..2.1.111.
...1.2.2..
.112313211
...1.2....
..1...1...
.1.....1..
1.......1.
222111....
You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2
or larger - now a total of 12
points.
Consider all of the lines. At how many points do at least two lines overlap?
segments
defmodule Counter2 do
def overlaps(segments) do
segments
|> Enum.reduce(%{}, fn
[x, y1, x, y2], grid -> sum_grid(Stream.cycle([x]), y1..y2, grid)
[x1, y, x2, y], grid -> sum_grid(x1..x2, Stream.cycle([y]), grid)
[x1, y1, x2, y2], grid -> sum_grid(x1..x2, y1..y2, grid)
end)
|> Enum.count(fn {_, v} -> v > 1 end)
end
defp sum_grid(xs, ys, grid) do
Enum.reduce(Enum.zip(xs, ys), grid, fn point, grid ->
Map.update(grid, point, 1, &(&1 + 1))
end)
end
end
Counter2.overlaps(segments)
Visualization with Vega Lite
viz_input = """
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
"""
alias VegaLite, as: Vl
lines =
viz_input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line
|> String.split([",", " -> "])
|> Enum.map(&String.to_integer/1)
end)
lines_data =
Enum.map(lines, fn [x1, y1, x2, y2] ->
%{x1: x1, y1: y1, x2: x2, y2: y2}
end)
Vl.new(width: 400, height: 400)
|> Vl.data_from_values(lines_data)
|> Vl.mark(:rule)
|> Vl.encode_field(:x, "x1", axis: [title: "x"])
|> Vl.encode_field(:y, "y1", axis: [title: "y"])
|> Vl.encode_field(:x2, "x2")
|> Vl.encode_field(:y2, "y2")
hist_data =
segments
|> Enum.flat_map(fn
[x, y1, x, y2] -> Enum.zip(Stream.cycle([x]), y1..y2)
[x1, y, x2, y] -> Enum.zip(x1..x2, Stream.cycle([y]))
[x1, y1, x2, y2] -> Enum.zip(x1..x2, y1..y2)
end)
|> Enum.map(fn {x, y} -> %{x: x, y: y} end)
Vl.new(width: 400, height: 400, background: "white")
|> Vl.data_from_values(hist_data)
|> Vl.mark(:circle)
|> Vl.encode_field(:x, "x", axis: [title: "x"], bin: [maxbins: 50])
|> Vl.encode_field(:y, "y", axis: [title: "y"], bin: [maxbins: 50])
|> Vl.encode(:size, aggregate: :count)
|> Vl.encode(:color, aggregate: :count, scale: [range: ["green", "red"]])