# Advent of Code 2022 - Day 18: Boiling Boulders Solution

## Part 1

Reject sides that are next to other cubes

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}const fs = require("fs")2
3const readData = () => {4  const data = fs5    .readFileSync("./input", "utf-8")6    .split(/\r?\n/)7    .map(line => [...line.split(",").map(Number)])8
9  const set = new Set(data.map(String))10
11  return { data, set }12}13
14const main = () => {15  const { data, set } = readData()16
17  const dirs = [18    [-1, 0, 0],19    [1, 0, 0],20    [0, -1, 0],21    [0, 1, 0],22    [0, 0, -1],23    [0, 0, 1],24  ]25
26  const SIDES_OF_A_CUBE = 627
28  const res = data.reduce(29    (acc, [x, y, z]) =>30      acc +31      (SIDES_OF_A_CUBE -32        dirs33          .map(([dx, dy, dz]) => String([x + dx, y + dy, z + dz]))34          .filter(pos => set.has(pos)).length),35    036  )37
38  console.log(res)39}40
41main()```

## Part 2

Traversal by flooding all cubes (in defined bound (

range
))

Whenever an adjacent cube is a lava cube, it means we face one side of the lava cube

For better visualization, ignore one dimension

```1. . . . .2. . L . .3. L x L .4. * L . .5. . . . .6
7at cube *, we face 2 sides of lava cube8x here is cube of air, which could never be reached```

## Implementation

```1const fs = require("fs")2
3const readData = () => {4  const data = fs5    .readFileSync("./input", "utf-8")6    .split(/\r?\n/)7    .map(line => [...line.split(",").map(Number)])8
9  const set = new Set(data.map(String))10
11  return { data, set }12}13
14const main = () => {15  const { data: lavaCubes, set: lavaCubesSet } = readData()16
17  const dirs = [18    [-1, 0, 0],19    [1, 0, 0],20    [0, -1, 0],21    [0, 1, 0],22    [0, 0, -1],23    [0, 0, 1],24  ]25
26  const isInRange = (value, range) => range <= value && value <= range27
28  const [xRange, yRange, zRange] = Array.from({ length: 3 }, (_, i) =>29    lavaCubes.map(coord => coord[i])30  ).map(values => [Math.min(...values) - 1, Math.max(...values) + 1])31
32  let res = 033  let queue = [[xRange, yRange, zRange]]34  let visited = new Set()35
36  while (queue.length) {37    const currentCube = queue.shift()38
39    if (visited.has(String(currentCube))) continue40
43    const [x, y, z] = currentCube44
45    const adjacentCubes = dirs46      .map(([dx, dy, dz]) => [x + dx, y + dy, z + dz])47      .filter(48        ([x, y, z]) =>49          isInRange(x, xRange) && isInRange(y, yRange) && isInRange(z, zRange)50      )51
61  console.log(res)62}63
64main()```

## References

Original problem

Flood fill (Wikipedia)

matrix

hash table

bfs

## Next Post

Advent of Code 2022 - Day 20: Grove Positioning System

Dec 20, 2022

Handling indexes with care

Search Posts