Skip to content

Commit

Permalink
Add solution to 2022/12/24 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexg-axis committed Dec 24, 2022
1 parent 867d85d commit 912351e
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions 2022/24/solutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ function bfs(
const width = states[0][0].length;
const height = states[0].length;

const toVisit: (Vector & { steps: number })[] = [];
toVisit.push({ ...startPosition, steps: 0 });
const toVisit: (Vector & { steps: number; waits: number })[] = [];
toVisit.push({ ...startPosition, steps: 0, waits: 0 });

const visited: Record<string, boolean> = {};

while (toVisit.length > 0) {
const current = toVisit.shift()!;
Expand All @@ -111,10 +113,11 @@ function bfs(
}

const checks = [
[-1, 0], // Left
[1, 0], // Right
[0, -1], // Up
[0, 1], // Down
[-1, 0], // Left
[0, -1], // Up
[0, 0], // Wait
];

let moves = 0;
Expand All @@ -125,21 +128,18 @@ function bfs(

const map = states[current.steps + 1];
if (map[next.y][next.x] === ".") {
toVisit.push({
...next,
steps: current.steps + 1,
});
const key = `${next.x}:${next.y}:${current.steps + 1}`;
if (!visited[key]) {
toVisit.push({
...next,
steps: current.steps + 1,
waits: 0,
});
visited[key] = true;
}
moves++;
}
}

// Wait only if there are no moves
if (moves === 0) {
toVisit.push({
...current,
steps: current.steps + 1,
});
}
}

return -1;
Expand Down

0 comments on commit 912351e

Please sign in to comment.