Public
Edited
1 star
Insert cell
Insert cell
Insert cell
Insert cell
neighbours = [
[1, 0],
[0, 1],
[-1, 0],
[0, -1]
]
Insert cell
Insert cell
function part1(input) {
const gSize = input.length;
const costs = AOC.gInit(gSize, gSize, Number.MAX_SAFE_INTEGER);
costs[0][0] = 0;
const pq = new PriorityQueue(([pos1, a], [pos2, b]) => a < b);
pq.add([[0, 0], 0]);

while (pq.size > 0) {
const [[r, c], cost] = pq.poll();
if (r == gSize - 1 && c == gSize - 1) {
return cost;
}
neighbours.forEach(([dy, dx]) => {
const [row, col] = [r + dy, c + dx];
if (row >= 0 && col >= 0 && row < gSize && col < gSize) {
const newCost = cost + input[row][col];
if (newCost < costs[row][col]) {
costs[row][col] = newCost;
pq.add([[row, col], newCost]);
}
}
});
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function caveMap(input) {
const gSize = input.length;
const cave = AOC.gInit(gSize * 5, gSize * 5);

for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
for (let row = 0; row < gSize; row++) {
for (let col = 0; col < gSize; col++) {
cave[i * gSize + row][j * gSize + col] =
((input[row][col] - 1 + i + j) % 9) + 1;
}
}
}
}
return cave;
}
Insert cell
Insert cell
function part2(input) {
return part1(caveMap(input));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more