day 16 not working yet

This commit is contained in:
Joshua Seigler 2024-12-16 20:52:06 -05:00 committed by Joshua Seigler
parent 4ba525c65f
commit 223327735b
4 changed files with 159 additions and 5 deletions

18
package-lock.json generated
View file

@ -8,10 +8,14 @@
"name": "aoc2024",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"heap": "^0.2.7"
},
"devDependencies": {
"@types/heap": "^0.2.34",
"@types/node": "^16.11.6",
"aocrunner": "^1.10.0",
"prettier": "^2.8.0"
"prettier": "^2.8.8"
},
"engines": {
"node": ">=16.13.0"
@ -61,6 +65,12 @@
"node": ">= 10"
}
},
"node_modules/@types/heap": {
"version": "0.2.34",
"resolved": "https://registry.npmjs.org/@types/heap/-/heap-0.2.34.tgz",
"integrity": "sha512-x9gp0NZnQeFrgrb6O3Tq5KtvsuEhWbc2vlHfs4cDOKcHsanzI39lIGcPdND/MRyaKUbxNgKXfKGCQGnjjOWweQ==",
"dev": true
},
"node_modules/@types/node": {
"version": "16.18.121",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.121.tgz",
@ -900,6 +910,11 @@
"node": ">= 6"
}
},
"node_modules/heap": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/heap/-/heap-0.2.7.tgz",
"integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg=="
},
"node_modules/html-encoding-sniffer": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-3.0.0.tgz",
@ -1181,7 +1196,6 @@
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz",
"integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==",
"dev": true,
"license": "MIT",
"bin": {
"prettier": "bin-prettier.js"
},

View file

@ -15,12 +15,15 @@
"author": "Joshua Seigler",
"license": "ISC",
"devDependencies": {
"@types/heap": "^0.2.34",
"@types/node": "^16.11.6",
"aocrunner": "^1.10.0",
"prettier": "^2.8.0"
"prettier": "^2.8.8"
},
"dependencies": {},
"engines": {
"node": ">=16.13.0"
},
"dependencies": {
"heap": "^0.2.7"
}
}
}

9
src/day16/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2024 - day 16 🎄
## Info
Task description: [link](https://adventofcode.com/2024/day/16)
## Notes
...

128
src/day16/index.ts Normal file
View file

@ -0,0 +1,128 @@
import run from "aocrunner"
import Heap from 'heap'
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split(''))
type Tuple<TItem, TLength extends number> = [TItem, ...TItem[]] & { length: TLength }
const deltas: [Coord, Coord, Coord, Coord] = [
[ 0, 1], // right
[ 1, 0], // down
[ 0, -1], // left
[-1, 0], // up
]
type Coord = [number, number]
const bold = (text: string) => {
return `\x1b[44;37m${text}\x1b[0m`
}
type Step = {
r: number
c: number
dir: number
cost: number
prev: Step | null
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
const get = (r: number, c: number) => (input[r] ?? [])[c]
const startIndex = rawInput.indexOf('S')
const c0 = startIndex % (input[0].length + 1)
const r0 = (startIndex - c0) / (input[0].length + 1)
const pq = new Heap<Step>((a, b) => a.cost - b.cost)
pq.push({
r: r0,
c: c0,
dir: 0,
cost: 0,
prev: null
})
while (pq.size() > 0) {
const prev = pq.pop()
const { r, c, dir, cost } = prev
const here = get(r, c)
if (here === 'E') {
// draw it
let cur = prev.prev
const visited = new Map<string, number>()
while (cur.prev !== null) {
visited.set(`${cur.r},${cur.c}`, cur.dir)
cur = cur.prev
}
console.log(
input
.map((row, r2) => {
return row
.map((cell, c2) => {
const key = `${r2},${c2}`
if (visited.has(key)) return bold(">v<^"[visited.get(key)])
if (cell === "#") return "\x1b[100m \x1b[0m"
if (cell === ".") return " "
return `\x1b[41m${cell}\x1b[0m`
})
.join("")
})
.join("\n"),
)
return cost
}
for (let i = 0; i < (here === "S" ? 4 : 3); i += 1) {
const newDir = (dir + 3 + i) % 4 // left, ahead, right, u-turn
const [dr, dc] = deltas[newDir]
const addedCost = [1001, 1, 1001, 2001][i]
if (get(r + dr, c + dc) !== "#") pq.push({
r: r + dr,
c: c + dc,
dir: newDir,
cost: cost + addedCost,
prev,
})
}
}
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
return
}
run({
part1: {
tests: [
{
input: `###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############`,
expected: 7036,
},
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})