Home
🏭

Day 9: Smoke Basin

https://adventofcode.com/2021/day/9
Challenge
These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).
Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:
2199943210
3987894921
9856789892
8767896789
9899965678
Each number corresponds to the height of a particular location, where 9is the highest and 0 is the lowest a location can be.
Your first goal is to find the low points - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)
In the above example, there are four low points, all highlighted: two are in the first row (a 1 and a 0), one is in the third row (a 5), and one is in the bottom row (also a 5). All other locations on the heightmap have some lower adjacent location, and so are not low points.
The risk level of a low point is 1 plus its height. In the above example, the risk levels of the low points are 216, and 6. The sum of the risk levels of all low points in the heightmap is therefore 15.
Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?

🔗 Part Two

Next, you need to find the largest basins so you know what areas are most important to avoid.
basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.
The size of a basin is the number of locations within the basin, including the low point. The example above has four basins.
The top-left basin, size 3:
The top-right basin, size 9:
The middle basin, size 14:
The bottom-right basin, size 9:
Find the three largest basins and multiply their sizes together. In the above example, this is 9 * 14 * 9 = 1134.
What do you get if you multiply together the sizes of the three largest basins?

🔗 Part A

import { strings } from "./util";

const heightMap = strings().map((line) =>
  line.split("").map((n) => parseInt(n))
);

function value(
  heightMap: number[][],
  row: number,
  col: number
) {
  if (heightMap[row] === undefined) {
    return Infinity;
  }

  if (heightMap[row][col] === undefined) {
    return Infinity;
  }

  return heightMap[row][col];
}

let totalRiskLevel = 0;
for (
  let row = 0;
  row < heightMap.length;
  row++
) {
  for (
    let col = 0;
    col < heightMap[row].length;
    col++
  ) {
    if (
      [
        value(heightMap, row + 1, col),
        value(heightMap, row - 1, col),
        value(heightMap, row, col + 1),
        value(heightMap, row, col - 1),
      ].every((v) => heightMap[row][col] < v)
    ) {
      totalRiskLevel +=
        heightMap[row][col] + 1;
    }
  }
}

console.log(totalRiskLevel);

🔗 Part B

import { strings } from "./util";

const heightMap = strings().map((line) =>
  line.split("").map((n) => parseInt(n))
);

const basinMap = heightMap.map((row) =>
  row.map((_) => 0)
);

function value(
  heightMap: number[][],
  row: number,
  col: number
) {
  if (heightMap[row] === undefined) {
    return Infinity;
  }

  if (heightMap[row][col] === undefined) {
    return Infinity;
  }

  return heightMap[row][col];
}

function flood(
  row: number,
  col: number,
  id: number
) {
  [
    [row + 1, col],
    [row - 1, col],
    [row, col + 1],
    [row, col - 1],
  ].forEach(([neighborRow, neighborCol]) => {
    if (
      value(
        basinMap,
        neighborRow,
        neighborCol
      ) !== 0
    ) {
      return;
    }

    const neighborValue = value(
      heightMap,
      neighborRow,
      neighborCol
    );
    if (
      neighborValue < 9 &&
      neighborValue !== heightMap[row][col]
    ) {
      basinMap[neighborRow][neighborCol] =
        id;
      flood(neighborRow, neighborCol, id);
    }
  });
}

let currentBasinId = 1;
for (
  let row = 0;
  row < heightMap.length;
  row++
) {
  for (
    let col = 0;
    col < heightMap[row].length;
    col++
  ) {
    const height = heightMap[row][col];
    if (
      height !== 9 &&
      !basinMap[row][col]
    ) {
      basinMap[row][col] = currentBasinId;
      flood(row, col, currentBasinId);
      currentBasinId++;
    }
  }
}

const basinSizes: { [key: number]: number } =
  {};
basinMap.forEach((row) =>
  row.forEach((basinId) => {
    if (basinId === 0) return;
    basinSizes[basinId] ||= 0;
    basinSizes[basinId]++;
  })
);

console.log(
  Object.values(basinSizes)
    .sort((a, b) => b - a)
    .slice(0, 3)
    .reduce((a, b) => a * b, 1)
);