Home
😶‍🌫️

Day 5: Hydrothermal Venture

https://adventofcode.com/2021/day/5
Question
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:
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 Two

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:
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 A

We'll keep it simple and store our grid as a map of string keys (i.e. "5,0") to integer values.
const grid: { [key: string]: number } = {};
We read in the input and do a bunch of splitting and pattern matching to turn 1,2 -> 3,4 into x1 = 1, y1 = 2, x2 = 3, y2 = 4. We could do some regex but the pattern is pretty simple.
import { strings } from "./util";

const grid: { [key: string]: number } = {};

strings().forEach((line) => {
  const [src, dest] = line.split(" -> ");
  const [[x1, y1], [x2, y2]] = [
    src,
    dest,
  ].map((p) =>
    p.split(",").map((s) => parseInt(s))
  );

  // ...
})
If our "line" is horizontal (the y-coordinates are equal), we'll loop through the x's and place vents on our grid accordingly.
// Horizontal line
if (y1 === y2) {
  const [a, b] = [
    Math.min(x1, x2),
    Math.max(x1, x2),
  ];

  for (let i = a; i <= b; i++) {
		// Update `grid`
  }
}
Updating the grid involves setting a string key (the coordinates joined with ,) to an integer value. We want to increment the values on our grid, which requires us to first set the value 0 if nothing is there.
grid[`${i},${y1}`] ||= 0;
grid[`${i},${y1}`]++;
So in total our horizontal line code is as follows:
// Horizontal line
if (y1 === y2) {
  const [a, b] = [
    Math.min(x1, x2),
    Math.max(x1, x2),
  ];

  for (let i = a; i <= b; i++) {
		// Update `grid`
    grid[`${i},${y1}`] ||= 0;
    grid[`${i},${y1}`]++;
  }
}
The code for vertical lines is very similar.
// Vertical line
if (x1 === x2) {
  const [a, b] = [
    Math.min(y1, y2),
    Math.max(y1, y2),
  ];

  for (let i = a; i <= b; i++) {
    grid[`${x1},${i}`] ||= 0;
    grid[`${x1},${i}`]++;
  }
}
And that's our loop!
To determine how overlaps we count the values on our grid which are >= 2.
console.log(
  Object.values(grid).filter((p) => p >= 2)
    .length
);

🔗 Part B

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:
Attempting to write a third clause for // Diagonal line will quickly show you the logic for this is non-trivial. Diagonal lines can go from NW (northwest) to SE (southeast), NE to SW, SE to NW, and SW to NE.
Brute forcing this leads to four annoying if-statements. Attempt to be clever with some mins and maxs can shorten the if-statements at the cost of even more inscrutable code. Let's ignore all that and write some helper functions.
First, a function to iterate from a to b in either direction.
function* range(a: number, b: number) {
  if (a < b) {
    for (let i = a; i <= b; i++) {
      yield i;
    }
  } else {
    for (let i = a; i >= b; i--) {
      yield i;
    }
  }
}
Then we'll use this to iterate through points on a line segment. Horizontal and vertical lines are similar to code we've already written, while diagonal lines involve grabbing two ranges and interleaving them.
function* lineSegment(
  x1: number,
  y1: number,
  x2: number,
  y2: number
) {
  if (y1 === y2) {
    // Horizontal line
    for (let x of range(x1, x2)) {
      yield [x, y1];
    }
  } else if (x1 === x2) {
    // Vertical line
    for (let y of range(y1, y2)) {
      yield [x1, y];
    }
  } else {
    // Diagonal line
    let xs = Array.from(range(x1, x2));
    let ys = Array.from(range(y1, y2));

    yield* xs.map((_, idx) => [
      xs[idx],
      ys[idx],
    ]);
  }
}
Once our helper functions are set, part B becomes much easier to wrap our heads around.
Just as before, we read in our input and slice and dice it.
strings().forEach((line) => {
  const [src, dest] = line.split(" -> ");
  const [[x1, y1], [x2, y2]] = [
    src,
    dest,
  ].map((p) =>
    p.split(",").map((s) => parseInt(s))
  );

  // ...
})
Then we leverage our lineSegment generator, and end it off by counting values >= 2 just as we did in part A.
strings().forEach((line) => {
  const [src, dest] = line.split(" -> ");
  const [[x1, y1], [x2, y2]] = [
    src,
    dest,
  ].map((p) =>
    p.split(",").map((s) => parseInt(s))
  );

  for (let [x, y] of lineSegment(
    x1,
    y1,
    x2,
    y2
  )) {
    grid[`${x},${y}`] ||= 0;
    grid[`${x},${y}`]++;
  }
});

console.log(
  Object.values(grid).filter((p) => p >= 2)
    .length
);
Ahh, so simple. 🧖