Skip to content

Commit

Permalink
Add solution to 2022/12/24 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexg-axis committed Dec 24, 2022
1 parent 912351e commit 6fd5001
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
12 changes: 12 additions & 0 deletions 2022/24/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,15 @@ Minute 18, move down:

__What is the fewest number of minutes required to avoid the blizzards and reach the goal?__

## --- Part Two ---

As the expedition reaches the far side of the valley, one of the Elves looks especially dismayed:

He __forgot his snacks__ at the entrance to the valley!

Since you're so good at dodging blizzards, the Elves humbly request that you go back for his snacks. From the same initial conditions, how quickly can you make it from the start to the goal, then back to the start, then back to the goal?

In the above example, the first trip to the goal takes `18` minutes, the trip back to the start takes `23` minutes, and the trip back to the goal again takes `13` minutes, for a total time of `54` minutes.

__What is the fewest number of minutes required to reach the goal, go back to the start, then reach the goal again?__

3 changes: 2 additions & 1 deletion 2022/24/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import input from "../../utils/deno/input.ts";
import { solvePart1 } from "./solutions.ts";
import { solvePart1, solvePart2 } from "./solutions.ts";

console.log(solvePart1(input));
console.log(solvePart2(input));
31 changes: 29 additions & 2 deletions 2022/24/solutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ function mutate(currentMap: string[][]): string[][] {
function bfs(
states: string[][][],
startPosition: Vector,
goalPosition: Vector
goalPosition: Vector,
initialSteps = 0
): number {
const width = states[0][0].length;
const height = states[0].length;

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

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

Expand Down Expand Up @@ -166,3 +167,29 @@ export function solvePart1(input: Input): number {

return bfs(states, startPosition, goalPosition);
}

export function solvePart2(input: Input): number {
const currentMap: string[][] = input.lines.map((x) => x.split(""));

// Pre-compute blizzard states
const states: string[][][] = [currentMap];
for (let i = 1; i < 1000; i++) {
states.push(mutate(states[i - 1]));
}

const startPosition = {
x: currentMap[0].findIndex((x) => x === "."),
y: 0,
};

const goalPosition = {
x: currentMap[currentMap.length - 1].findIndex((x) => x === "."),
y: currentMap.length - 1,
};

const round1 = bfs(states, startPosition, goalPosition);
const round2 = bfs(states, goalPosition, startPosition, round1);
const round3 = bfs(states, startPosition, goalPosition, round2);

return round3;
}
12 changes: 11 additions & 1 deletion 2022/24/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { Input } from "../../utils/deno/input.ts";
import { solvePart1 } from "./solutions.ts";
import { solvePart1, solvePart2 } from "./solutions.ts";

Deno.test("part 1 - given test case", () => {
const input = `#.######
Expand All @@ -11,3 +11,13 @@ Deno.test("part 1 - given test case", () => {
######.#`;
assertEquals(solvePart1(new Input(input)), 18);
});

Deno.test("part 2 - given test case", () => {
const input = `#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#`;
assertEquals(solvePart2(new Input(input)), 54);
});

0 comments on commit 6fd5001

Please sign in to comment.