day 15, what a slog

This commit is contained in:
Joshua Seigler 2021-12-15 02:58:55 -05:00
parent 8c135affd7
commit 901f88482f
6 changed files with 149 additions and 40 deletions

View file

@ -209,16 +209,20 @@
},
{
"part1": {
"solved": false,
"result": null,
"attempts": [],
"time": null
"solved": true,
"result": "702",
"attempts": [
"NaN"
],
"time": 55.8
},
"part2": {
"solved": false,
"result": null,
"attempts": [],
"time": null
"solved": true,
"result": "2955",
"attempts": [
"2967"
],
"time": 761.32
}
},
{

View file

@ -25,7 +25,7 @@
[![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%85/green)](src/day12)
[![Day](https://badgen.net/badge/13/%E2%98%85%E2%98%85/green)](src/day13)
[![Day](https://badgen.net/badge/14/%E2%98%85%E2%98%85/green)](src/day14)
![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/15/%E2%98%85%E2%98%85/green)](src/day15)
![Day](https://badgen.net/badge/16/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/17/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/18/%E2%98%86%E2%98%86/gray)
@ -160,16 +160,16 @@ Both parts: 21.44ms
```
Day 14
Time part 1: 11.29ms
Time part 2: 5.6ms
Both parts: 16.89ms
Time part 1: 1.6ms
Time part 2: 4.68ms
Both parts: 6.279999999999999ms
```
```
Day 15
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 8.12ms
Time part 2: 769.36ms
Both parts: 777.48ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 28/50
Total time: 1095.0899999999997ms
Total stars: 30/50
Total time: 1861.9599999999996ms
```
<!--/RESULTS-->

37
package-lock.json generated
View file

@ -5,8 +5,12 @@
"requires": true,
"packages": {
"": {
"name": "aoc2021",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"heap": "^0.2.7"
},
"devDependencies": {
"@types/node": "^16.11.6",
"aocrunner": "^1.4.5",
@ -168,7 +172,6 @@
"dependencies": {
"anymatch": "~3.1.2",
"braces": "~3.0.2",
"fsevents": "~2.3.2",
"glob-parent": "~5.1.2",
"is-binary-path": "~2.1.0",
"is-glob": "~4.0.1",
@ -315,25 +318,6 @@
"integrity": "sha512-raCxt02HBKv8RJxE8vkTSCXGIyKHdEdGfUmiYb8wnabnaEmHzyW7DCHb5tEN0xU8ryqg5xw54mcwnYkC4x3AIw==",
"dev": true,
"hasInstallScript": true,
"dependencies": {
"esbuild-android-arm64": "0.13.15",
"esbuild-darwin-64": "0.13.15",
"esbuild-darwin-arm64": "0.13.15",
"esbuild-freebsd-64": "0.13.15",
"esbuild-freebsd-arm64": "0.13.15",
"esbuild-linux-32": "0.13.15",
"esbuild-linux-64": "0.13.15",
"esbuild-linux-arm": "0.13.15",
"esbuild-linux-arm64": "0.13.15",
"esbuild-linux-mips64le": "0.13.15",
"esbuild-linux-ppc64le": "0.13.15",
"esbuild-netbsd-64": "0.13.15",
"esbuild-openbsd-64": "0.13.15",
"esbuild-sunos-64": "0.13.15",
"esbuild-windows-32": "0.13.15",
"esbuild-windows-64": "0.13.15",
"esbuild-windows-arm64": "0.13.15"
},
"bin": {
"esbuild": "bin/esbuild"
},
@ -587,8 +571,7 @@
"esprima": "^4.0.1",
"estraverse": "^5.2.0",
"esutils": "^2.0.2",
"optionator": "^0.8.1",
"source-map": "~0.6.1"
"optionator": "^0.8.1"
},
"bin": {
"escodegen": "bin/escodegen.js",
@ -733,6 +716,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",
@ -1770,6 +1758,11 @@
"is-glob": "^4.0.1"
}
},
"heap": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/heap/-/heap-0.2.7.tgz",
"integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg=="
},
"html-encoding-sniffer": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-3.0.0.tgz",

View file

@ -17,7 +17,9 @@
"aocrunner": "^1.4.5",
"prettier": "^2.4.1"
},
"dependencies": {},
"dependencies": {
"heap": "^0.2.7"
},
"engines": {
"node": ">=16.0.0"
}

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

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

101
src/day15/index.js Normal file
View file

@ -0,0 +1,101 @@
import run from "aocrunner"
import Heap from "heap"
const parseInput = (rawInput) =>
rawInput.trim().split`\n`.map((l) => [...l].map((x) => +x))
const part1 = (rawInput) => {
return part2(rawInput, 1)
/* ONLY WORKS FOR SOME INPUTS*/
// const input = parseInput(rawInput)
// const h = input.length,
// w = input[0].length
// const costs = new Map()
// function solve(r, c) {
// const key = `${r},${c}`
// if (r === h - 1 && c === w - 1) {
// return input[r][c]
// }
// if (c < 0 || c >= w || r < 0 || r >= h) {
// return Infinity
// }
// if (costs.has(key)) {
// return costs.get(key)
// }
// const ans = input[r][c] + Math.min(solve(r + 1, c), solve(r, c + 1))
// costs.set(key, ans)
// return ans
// }
// return solve(0, 0) - input[0][0]
}
const part2 = (rawInput, mult = 5) => {
const input = parseInput(rawInput)
const h = input.length,
w = input[0].length,
Q = new Heap(([a], [b]) => a - b)
Q.push([0, 0, 0])
const costs = []
for (let i = 0; i < mult * h; i++) {
costs[i] = new Array(mult * w)
costs[i].fill(Infinity)
}
const dr = [-1, 0, 1, 0]
const dc = [0, -1, 0, 1]
while (Q.size()) {
const [totalRisk, r, c] = Q.pop() // take off the lowest distance point
if (c < 0 || c >= mult * w || r < 0 || r >= mult * h) continue // out of bounds
const tileDistance = Math.floor(r / h) + Math.floor(c / w)
const riskHere = 1 + ((input[r % h][c % w] - 1 + tileDistance) % 9)
if (costs[r][c] <= totalRisk + riskHere) {
continue // wasting time
} else {
costs[r][c] = totalRisk + riskHere // we found a cheaper path here
}
if (r === mult * h - 1 && c === mult * w - 1) break // yay we finished
for (let i = 0; i < 4; i++) {
Q.push([costs[r][c], r + dr[i], c + dc[i]]) // queue all the neighbors
}
}
return costs[mult * h - 1][mult * w - 1] - costs[0][0]
}
run({
part1: {
tests: [
{
input: `1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581`,
expected: 40,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581`,
expected: 315,
},
],
solution: part2,
},
trimTestInputs: true,
})