Advent of Code 2022 - Day 18: Boiling Boulders Solution
Flooding with breadth-first searchPart 1
Reject sides that are next to other cubes
Implementation
1const fs = require("fs")23const readData = () => {4 const data = fs5 .readFileSync("./input", "utf-8")6 .split(/\r?\n/)7 .map(line => [...line.split(",").map(Number)])89 const set = new Set(data.map(String))1011 return { data, set }12}1314const main = () => {15 const { data, set } = readData()1617 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 ]2526 const SIDES_OF_A_CUBE = 62728 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 )3738 console.log(res)39}4041main()
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. . . . .67at cube *, we face 2 sides of lava cube8x here is cube of air, which could never be reached
Implementation
1const fs = require("fs")23const readData = () => {4 const data = fs5 .readFileSync("./input", "utf-8")6 .split(/\r?\n/)7 .map(line => [...line.split(",").map(Number)])89 const set = new Set(data.map(String))1011 return { data, set }12}1314const main = () => {15 const { data: lavaCubes, set: lavaCubesSet } = readData()1617 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 ]2526 const isInRange = (value, range) => range[0] <= value && value <= range[1]2728 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])3132 let res = 033 let queue = [[xRange[0], yRange[0], zRange[0]]]34 let visited = new Set()3536 while (queue.length) {37 const currentCube = queue.shift()3839 if (visited.has(String(currentCube))) continue4041 visited.add(String(currentCube))4243 const [x, y, z] = currentCube4445 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 )5152 for (const adjacentCube of adjacentCubes) {53 if (lavaCubesSet.has(String(adjacentCube))) {54 res++55 } else {56 queue.push(adjacentCube)57 }58 }59 }6061 console.log(res)62}6364main()
References
Comments
Loading comments...
Tags
adventofcode
matrix
hash table
bfs
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Advent of Code 2022 - Day 20: Grove Positioning System
Handling indexes with care
Previous Post
Advent of Code 2022 - Day 15: Beacon Exclusion Zone
Union of ranges