don't forget the snacks

This commit is contained in:
eleith 2023-02-15 19:08:18 -08:00
parent acd09b0a48
commit edf0e31f3c
4 changed files with 550 additions and 0 deletions

254
24/README.md Normal file
View File

@ -0,0 +1,254 @@
--- Day 24: Blizzard Basin ---
With everything replanted for next year (and with elephants and monkeys to tend the grove), you and the Elves leave for the extraction point.
Partway up the mountain that shields the grove is a flat, open area that serves as the extraction point. It's a bit of a climb, but nothing the expedition can't handle.
At least, that would normally be true; now that the mountain is covered in snow, things have become more difficult than the Elves are used to.
As the expedition reaches a valley that must be traversed to reach the extraction site, you find that strong, turbulent winds are pushing small blizzards of snow and sharp ice around the valley. It's a good thing everyone packed warm clothes! To make it across safely, you'll need to find a way to avoid them.
Fortunately, it's easy to see all of this from the entrance to the valley, so you make a map of the valley and the blizzards (your puzzle input). For example:
#.#####
#.....#
#>....#
#.....#
#...v.#
#.....#
#####.#
The walls of the valley are drawn as #; everything else is ground. Clear ground - where there is currently no blizzard - is drawn as .. Otherwise, blizzards are drawn with an arrow indicating their direction of motion: up (^), down (v), left (<), or right (>).
The above map includes two blizzards, one moving right (>) and one moving down (v). In one minute, each blizzard moves one position in the direction it is pointing:
#.#####
#.....#
#.>...#
#.....#
#.....#
#...v.#
#####.#
Due to conservation of blizzard energy, as a blizzard reaches the wall of the valley, a new blizzard forms on the opposite side of the valley moving in the same direction. After another minute, the bottom downward-moving blizzard has been replaced with a new downward-moving blizzard at the top of the valley instead:
#.#####
#...v.#
#..>..#
#.....#
#.....#
#.....#
#####.#
Because blizzards are made of tiny snowflakes, they pass right through each other. After another minute, both blizzards temporarily occupy the same position, marked 2:
#.#####
#.....#
#...2.#
#.....#
#.....#
#.....#
#####.#
After another minute, the situation resolves itself, giving each blizzard back its personal space:
#.#####
#.....#
#....>#
#...v.#
#.....#
#.....#
#####.#
Finally, after yet another minute, the rightward-facing blizzard on the right is replaced with a new one on the left facing the same direction:
#.#####
#.....#
#>....#
#.....#
#...v.#
#.....#
#####.#
This process repeats at least as long as you are observing it, but probably forever.
Here is a more complex example:
#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#
Your expedition begins in the only non-wall position in the top row and needs to reach the only non-wall position in the bottom row. On each minute, you can move up, down, left, or right, or you can wait in place. You and the blizzards act simultaneously, and you cannot share a position with a blizzard.
In the above example, the fastest way to reach your goal requires 18 steps. Drawing the position of the expedition as E, one way to achieve this is:
Initial state:
#E######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#
Minute 1, move down:
#.######
#E>3.<.#
#<..<<.#
#>2.22.#
#>v..^<#
######.#
Minute 2, move down:
#.######
#.2>2..#
#E^22^<#
#.>2.^>#
#.>..<.#
######.#
Minute 3, wait:
#.######
#<^<22.#
#E2<.2.#
#><2>..#
#..><..#
######.#
Minute 4, move up:
#.######
#E<..22#
#<<.<..#
#<2.>>.#
#.^22^.#
######.#
Minute 5, move right:
#.######
#2Ev.<>#
#<.<..<#
#.^>^22#
#.2..2.#
######.#
Minute 6, move right:
#.######
#>2E<.<#
#.2v^2<#
#>..>2>#
#<....>#
######.#
Minute 7, move down:
#.######
#.22^2.#
#<vE<2.#
#>>v<>.#
#>....<#
######.#
Minute 8, move left:
#.######
#.<>2^.#
#.E<<.<#
#.22..>#
#.2v^2.#
######.#
Minute 9, move up:
#.###### #<E2>>.#
#.<<.<.#
#>2>2^.#
#.v><^.#
######.#
Minute 10, move right:
#.######
#.2E.>2#
#<2v2^.#
#<>.>2.#
#..<>..#
######.#
Minute 11, wait:
#.######
#2^E^2>#
#<v<.^<#
#..2.>2#
#.<..>.#
######.#
Minute 12, move down:
#.######
#>>.<^<#
#.<E.<<#
#>v.><>#
#<^v^^>#
######.#
Minute 13, move down:
#.######
#.>3.<.#
#<..<<.#
#>2E22.#
#>v..^<#
######.#
Minute 14, move right:
#.######
#.2>2..#
#.^22^<#
#.>2E^>#
#.>..<.#
######.#
Minute 15, move right:
#.######
#<^<22.#
#.2<.2.#
#><2>E.#
#..><..#
######.#
Minute 16, move right:
#.######
#.<..22#
#<<.<..#
#<2.>>E#
#.^22^.#
######.#
Minute 17, move down:
#.######
#2.v.<>#
#<.<..<#
#.^>^22#
#.2..2E#
######.#
Minute 18, move down:
#.######
#>2.<.<#
#.2v^2<#
#>..>2>#
#<....>#
######E#
What is the fewest number of minutes required to avoid the blizzards and reach the goal?
Your puzzle answer was 253.
--- 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?
Your puzzle answer was 794.

27
24/input.txt Normal file
View File

@ -0,0 +1,27 @@
#.########################################################################################################################
#.<^>v^v>v>^.><v.>><.>><v^v^v<<>.v^<v^.<>v<^v..>.><v<^v>v<>v.vv^v>^vvvv>>><><.v<<v>><<<.^^><v^vvv^v^>.^<^>v<>vv>v.>^^^<^<#
#>^^vv.>^v^>.v^^<^v<>.v><>v><v..v>><>>v^vv<....^>^v^v<<<<.><><v^v<v^<.vv.<v^.<v.^><<vv^v.v.^v<>^>.<><.v^v><.v<<.>>v<v>^^<#
#><>^<^^^^<^^>>^^^^v<v<>>^^.v<>v<v.><v><v^<^>><^>v^^.<<v>v<^>>.^vv<^.^vv>v>>vvv<>v<>.>>><<^<>..v<v<>><><>^^<<.vv>>><><>.<#
#>v^v^<>v^^>^>><<^<<.v><v.v<v><^<^<v^>^<^>^<<<.^^v<>vv>v<.vvv<>^..^.^<>^v><.v^.>v>v<vv^><.<>vvv.^<v<>^>>^<<vv<<^<.^<><>^<#
#>^^>v>>v.>><>vvvv><vv><><>v^^vvv.<^v>^<^>^^>><<v.v><.><<^^.v><>^v^^vv^^vv^<^<^<>v<vvvv<^<.>v<.vv.^>v^<vv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>^.v^><<<>.^.v^v.<<>vv>.vv>><^...<v<v>>v>>v<.>#
#<.>>.<><^<>^>.^>>>v<><v<<.^^v.>^.^>>vvv<<<^><<.v^.v>^<<>vvv^v^vvv^v.^^^..<^v<v><<vv.<.<<^.^^^^>>>v^v<.>^^<>>^><>><>>v^v.#
#><<>>vvvv><.<.>>^>^.^<^<.v>v>^^<<<><^^>>><^><^^^>>vv<>v<>^v^v^>>.^>v^<<<v<^<^^^^<>>.v><<>v^vvv^>v^.>^.^v<<>^v<^>>><>vv.<#
########################################################################################################################.#

6
24/sample-input.txt Normal file
View File

@ -0,0 +1,6 @@
#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#

263
24/src/index.ts Normal file
View File

@ -0,0 +1,263 @@
import { readFileSync } from 'fs'
const BlizzardMarker = {
BLIZZARD_UP: '^',
BLIZZARD_DOWN: 'v',
BLIZZARD_LEFT: '<',
BLIZZARD_RIGHT: '>',
} as const
const ValleySpace = {
...BlizzardMarker,
WALL: '#',
EMPTY: '.',
ME: 'E',
BLIZARD_MANY: '*',
} as const
type Point = { x: number; y: number }
type ValleySpaces = typeof ValleySpace[keyof typeof ValleySpace]
type BlizzardMarkers = typeof ValleySpace[keyof typeof BlizzardMarker]
type Blizzard = {
marker: BlizzardMarkers
start: Point
}
type Valley = {
width: number
height: number
start: Point
exit: Point
blizzards: Blizzard[]
spaceSnapshots: { [key: string]: Record<string, ValleySpaces> }
}
function pointToKey(point: Point): string {
return `${point.x},${point.y}`
}
function findFastestStepsBetween(valley: Valley, start: Point, exit: Point, timeStart = 0): number {
const queue = [{ point: start, time: timeStart }] as {
point: Point
time: number
}[]
const visited = {} as { [time: string]: { [key: string]: boolean } }
let fastest = Infinity
while (queue.length) {
const { point, time } = queue.shift() as typeof queue[number]
const key = pointToKey(point)
const cycle = (valley.width - 2) * (valley.height - 2)
const cycleTime = (time + 1) % cycle
if (time >= fastest) {
continue
}
if (visited[key]?.[cycleTime]) {
continue
}
if (point.x === exit.x && point.y === exit.y) {
fastest = time
continue
}
const nextMoves = [
{ x: point.x - 1, y: point.y },
{ x: point.x + 1, y: point.y },
{ x: point.x, y: point.y - 1 },
{ x: point.x, y: point.y + 1 },
{ x: point.x, y: point.y },
]
if (!visited[key]) {
visited[key] = {}
}
visited[key][cycleTime] = true
nextMoves
.filter((neighbor) => getSpaceAtPoint(neighbor, valley, time + 1) === ValleySpace.EMPTY)
.forEach((neighbor) => {
queue.push({ point: neighbor, time: time + 1 })
})
}
return fastest
}
function getSpaceAtPoint(point: Point, valley: Valley, time: number): ValleySpaces {
const snapshot = getSpacePositions(valley, time)
const space = snapshot[`${point.x},${point.y}`]
if (point.x === valley.start.x && point.y === valley.start.y) {
return ValleySpace.EMPTY
} else if (point.x === valley.exit.x && point.y === valley.exit.y) {
return ValleySpace.EMPTY
} else if (point.x <= 0 || point.x >= valley.width - 1) {
if (space) {
throw new Error(`blizzard is in a wall at ${point.x},${point.y}`)
}
return ValleySpace.WALL
} else if (point.y <= 0 || point.y >= valley.height - 1) {
if (space) {
throw new Error(`blizzard is in a wall at ${point.x},${point.y}`)
}
return ValleySpace.WALL
} else if (space) {
return space
} else {
return ValleySpace.EMPTY
}
}
function blizzardPositionAtTime(blizzard: Blizzard, valley: Valley, time: number): Point {
const start = blizzard.start
const maxWidth = valley.width - 2
const maxHeight = valley.height - 2
let x = start.x
let y = start.y
switch (blizzard.marker) {
case ValleySpace.BLIZZARD_UP:
y = start.y - time
break
case ValleySpace.BLIZZARD_DOWN:
y = start.y + time
break
case ValleySpace.BLIZZARD_LEFT:
x = start.x - time
break
case ValleySpace.BLIZZARD_RIGHT:
x = start.x + time
break
}
if (y < 1) {
y = maxHeight - Math.abs(y % maxHeight)
} else if (y > maxHeight) {
y = 1 + ((y - 1) % maxHeight)
}
if (x < 1) {
x = maxWidth - Math.abs(x % maxWidth)
} else if (x > maxWidth) {
x = 1 + ((x - 1) % maxWidth)
}
return { x, y }
}
function parseInput(lines: string[]): Valley {
const valley: Valley = {
width: lines[0].length,
height: lines.length,
start: { x: 0, y: 0 },
exit: { x: 0, y: 0 },
blizzards: [],
spaceSnapshots: {},
}
lines.forEach((line, y) => {
line.split('').forEach((space, x) => {
if (space === ValleySpace.EMPTY) {
if (y === 0) {
valley.start = { x, y }
} else if (y === lines.length - 1) {
valley.exit = { x, y }
}
} else if (Object.values(BlizzardMarker).includes(space as BlizzardMarkers)) {
valley.blizzards.push({ marker: space, start: { x, y } } as Blizzard)
}
})
})
return valley
}
function getSpacePositions(valley: Valley, time: number): Record<string, ValleySpaces> {
const timeCycle = time % ((valley.width - 2) * (valley.height - 2))
const snapshot = valley.spaceSnapshots[timeCycle]
if (snapshot) {
return snapshot
} else {
const snapshots = valley.spaceSnapshots
snapshots[timeCycle] = valley.blizzards.reduce((pointMap, blizzard) => {
const point = blizzardPositionAtTime(blizzard, valley, time)
const key = `${point.x},${point.y}`
pointMap[key] = pointMap[key] ? ValleySpace.BLIZARD_MANY : blizzard.marker
return pointMap
}, {} as Record<string, ValleySpaces>)
return snapshots[timeCycle]
}
}
function printValley(valley: Valley, me: Point, time: number) {
const line: string[] = []
for (let y = 0; y < valley.height; y++) {
for (let x = 0; x < valley.width; x++) {
const point = { x, y }
if (point.x === me.x && point.y === me.y) {
line.push(ValleySpace.ME)
} else {
const space = getSpaceAtPoint(point, valley, time)
line.push(space)
}
}
line.push('\n')
}
console.log(line.join(''))
}
function solvePart1(lines: string[], debug = false): number {
const valley = parseInput(lines)
if (debug) {
printValley(valley, valley.start, 0)
}
return findFastestStepsBetween(valley, valley.start, valley.exit, 0)
}
function solvePart2(lines: string[], debug = false): number {
const valley = parseInput(lines)
if (debug) {
printValley(valley, valley.start, 0)
}
const timeToExit = findFastestStepsBetween(valley, valley.start, valley.exit, 0)
const timeToSnack = findFastestStepsBetween(valley, valley.exit, valley.start, timeToExit)
const timeToExitAgain = findFastestStepsBetween(valley, valley.start, valley.exit, timeToSnack)
return timeToExitAgain
}
async function run() {
const input = process.argv.slice(2)[0]
const debug = process.argv.slice(2)[1] === '--debug'
const file = readFileSync(input, 'utf8')
const lines = file.split('\n').slice(0, -1)
const solution1 = solvePart1(lines, debug)
console.log(`part1: ${solution1}`)
const solution2 = solvePart2(lines, debug)
console.log(`part2: ${solution2}`)
}
run()