Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code - Day 5: Hydrothermal Venture

lib/advent_of_code/2021/day-05.livemd

Advent of Code - Day 5: Hydrothermal Venture

Mix.install([:kino])

Day 5: Hydrothermal Venture

Part One: Prompt

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 points 1,1, 1,2, and 1,3.
  • An entry like 9,7 -> 7,7 covers points 9,7, 8,7, and 7,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 1s, 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?

Part One: Solution

input = Kino.Input.textarea("Please paste your input file:")
defmodule Lines do
  def traverse([x, y], map) do
    move(
      List.to_tuple(x),
      List.to_tuple(y),
      map
    )
  end

  def traverse_h_v([[x, y1], [x, y2]], map) do
    move({x, y1}, {x, y2}, map)
  end

  def traverse_h_v([[x1, y], [x2, y]], map) do
    move({x1, y}, {x2, y}, map)
  end

  def traverse_h_v(_, map), do: map

  # {8,0} -> {0,0}
  defp move({x1, y}, {x2, y}, map) when x1 > x2 do
    move({x2, y}, {x1, y}, map)
  end

  # {0,8} -> {0,0}
  defp move({x, y1}, {x, y2}, map) when y1 > y2 do
    move({x, y2}, {x, y1}, map)
  end

  # {0,0} -> {8,0}
  defp move({x1, y}, {x2, y}, map) do
    for x <- x1..x2, reduce: map do
      acc -> increment_coord(acc, {x, y})
    end
  end

  # {0,0} -> {0,8}
  defp move({x, y1}, {x, y2}, map) do
    for y <- y1..y2, reduce: map do
      acc -> increment_coord(acc, {x, y})
    end
  end

  # {8,8} -> {3,3}
  defp move({x1, y1}, {x2, y2}, map) when x1 > x2 do
    move({x2, y2}, {x1, y1}, map)
  end

  # {3,3} -> {8,8}
  defp move({x1, y1}, {x2, y2}, map) when y1 < y2 do
    for x <- x1..x2, reduce: map do
      acc -> increment_coord(acc, {x, y1 + x - x1})
    end
  end

  # {3,8} -> {8,3}
  defp move({x1, y1}, {x2, y2}, map) when y1 > y2 do
    for x <- x1..x2, reduce: map do
      acc -> increment_coord(acc, {x, y1 - x + x1})
    end
  end

  defp increment_coord(map, {x, y}) do
    Map.update(map, {x, y}, 1, &amp;(&amp;1 + 1))
  end
end
{:module, Lines, <<70, 79, 82, 49, 0, 0, 15, ...>>, {:increment_coord, 2}}
coord =
  input
  |> Kino.Input.read()
  |> String.split("\n", trim: true)
  |> Enum.map(fn s ->
    s
    |> String.split(~r{\s*\-\>\s*})
    |> Enum.map(fn s ->
      s |> String.split(",") |> Enum.map(&amp;String.to_integer/1)
    end)
  end)
[
  [[88, 177], [566, 655]],
  [[346, 264], [872, 264]],
  [[409, 631], [506, 534]],
  [[300, 216], [300, 507]],
  [[80, 370], [193, 483]],
  [[85, 283], [85, 483]],
  [[589, 528], [968, 528]],
  [[936, 83], [936, 909]],
  [[21, 41], [907, 927]],
  [[868, 624], [868, 490]],
  [[954, 972], '3E'],
  [[95, 223], [851, 979]],
  [[681, 222], [681, 32]],
  [[596, 557], [384, 557]],
  [[830, 945], [830, 210]],
  [[146, 17], [582, 17]],
  [[923, 864], [923, 854]],
  [[698, 289], [893, 94]],
  [[521, 860], [521, 658]],
  [[602, 699], [602, 626]],
  [[115, 537], [12, 434]],
  [[872, 264], [239, 897]],
  [[820, 674], [820, 752]],
  [[885, 292], [519, 658]],
  [[88, 193], [88, 618]],
  [[371, 681], [556, 681]],
  [[222, 894], [741, 894]],
  [[81, 790], [277, 790]],
  [[973, 328], [973, 42]],
  [[517, 548], [491, 522]],
  [[75, 417], [260, 417]],
  [[920, 334], [920, 416]],
  [[923, 110], [44, 989]],
  [[736, 333], [714, 333]],
  [[697, 850], [345, 850]],
  [[404, 746], [770, 380]],
  [[156, 166], [156, 857]],
  [[579, 571], [796, 788]],
  [[94, 929], [277, 746]],
  [[929, 313], [929, 633]],
  [[337, 951], [337, 651]],
  [[751, 841], [119, 209]],
  [[648, 705], [775, 578]],
  [[496, 362], [84, 362]],
  [[22, 19], [981, 978]],
  [[463, 111], [877, 111]],
  [[857, 378], [299, ...]],
  [[973, ...], [...]],
  [[...], ...],
  [...],
  ...
]
final_map = Enum.reduce(coord, %{}, &amp;Lines.traverse_h_v/2)
%{
  {64, 757} => 1,
  {210, 767} => 1,
  {537, 441} => 2,
  {53, 593} => 1,
  {593, 345} => 1,
  {936, 538} => 2,
  {191, 874} => 1,
  {344, 853} => 1,
  {793, 881} => 1,
  {297, 142} => 1,
  {310, 481} => 1,
  {142, 180} => 1,
  {549, 515} => 1,
  {104, 571} => 1,
  {624, 302} => 1,
  {191, 963} => 1,
  {376, 593} => 1,
  {95, 874} => 1,
  {565, 475} => 1,
  {248, 394} => 1,
  {689, 586} => 1,
  {931, 206} => 1,
  {187, 247} => 1,
  {936, 154} => 1,
  {263, 76} => 1,
  {142, 355} => 1,
  {498, 471} => 1,
  {65, 63} => 1,
  {160, 246} => 1,
  {753, 389} => 1,
  {88, 230} => 1,
  {621, 927} => 1,
  {601, 181} => 1,
  {681, 302} => 1,
  {598, 510} => 1,
  {815, 487} => 1,
  {428, 846} => 1,
  {191, 676} => 1,
  {522, 542} => 1,
  {405, 593} => 2,
  {527, 892} => 1,
  {815, 909} => 1,
  {345, 529} => 1,
  {631, 334} => 1,
  {808, 452} => 1,
  {300, 367} => 1,
  {659, 892} => 1,
  {646, 355} => 2,
  {537, ...} => 2,
  {...} => 1,
  ...
}
final_map |> Enum.count(fn {_k, v} -> v > 1 end)
5835

Your puzzle answer was 5835.

The first half of this puzzle is complete! It provides one gold star: *

Part Two: Prompt

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 points 1,1, 2,2, and 3,3.
  • An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,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?

Part Two: Solution

I modififed the Lines module above so that traverse/2 will now work for diagonal lines, as well as horizontal and vertical.

The move/3 function was modified to accommodate the diagonal lines.

final_map = Enum.reduce(coord, %{}, &amp;Lines.traverse/2)
%{
  {64, 757} => 1,
  {619, 554} => 1,
  {727, 735} => 1,
  {210, 767} => 1,
  {397, 835} => 1,
  {235, 660} => 1,
  {537, 441} => 2,
  {53, 593} => 1,
  {593, 345} => 1,
  {517, 548} => 1,
  {936, 538} => 2,
  {945, 96} => 1,
  {291, 742} => 1,
  {191, 874} => 1,
  {440, 452} => 1,
  {344, 853} => 1,
  {793, 881} => 1,
  {297, 142} => 1,
  {310, 481} => 1,
  {358, 877} => 1,
  {276, 695} => 1,
  {142, 180} => 1,
  {549, 515} => 2,
  {104, 571} => 1,
  {624, 302} => 1,
  {191, 963} => 1,
  {376, 593} => 1,
  {707, 349} => 1,
  {95, 874} => 1,
  {576, 335} => 1,
  {893, 376} => 2,
  {565, 475} => 1,
  {248, 394} => 1,
  {689, 586} => 1,
  {705, 283} => 1,
  {315, 152} => 1,
  {931, 206} => 2,
  {187, 247} => 1,
  {936, 154} => 1,
  {263, 76} => 1,
  {142, 355} => 1,
  {498, 471} => 1,
  {391, 682} => 1,
  {65, 63} => 1,
  {160, 246} => 1,
  {857, 114} => 1,
  {753, 389} => 1,
  {88, 230} => 1,
  {621, ...} => 1,
  {...} => 1,
  ...
}
final_map |> Enum.count(fn {_k, v} -> v > 1 end)
17013

Your puzzle answer was 17013.

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.