-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solved day 17. p2 takes 133 secs....
- Loading branch information
Showing
3 changed files
with
100 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
"use strict"; | ||
|
||
/* eslint-disable one-var */ | ||
|
||
const fs = require("node:fs"); | ||
const path = require("node:path"); | ||
const { performance } = require("node:perf_hooks"); | ||
|
||
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n"); | ||
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n").map(l => l.split("").map(Number)); | ||
|
||
const pStart = performance.now(); | ||
|
||
// | ||
// YOUR CODE HERE | ||
// | ||
const result = "..."; | ||
let res = 0; | ||
const q = [{ c: 0, row: 0, col: 0, dR: 0, dC: 0, m: 0 }], seen = new Set(); | ||
while (q.length){ // @ts-ignore | ||
const { c, row, col, dR, dC, m } = q.sort((p, n) => n.c - p.c).pop(); | ||
(row === (INPUT.length - 1) && col === (INPUT[0].length - 1) && m >= 1) && (res = c); | ||
if (res !== 0) break; | ||
|
||
if (seen.has(JSON.stringify([row, col, dR, dC, m]))) continue; | ||
seen.add(JSON.stringify([row, col, dR, dC, m])); | ||
|
||
((m < 3 && !(dR === 0 && dC === 0)) | ||
&& (row + dR >= 0 && row + dR < INPUT.length && col + dC >= 0 && col + dC < INPUT[0].length) | ||
&& q.push( { c: c + INPUT[row + dR][col + dC], row: row + dR, col: col + dC, dR, dC, m: m + 1 }) || 1) | ||
&& ((m >= 1 || (dR === 0 && dC === 0)) && [[0, 1], [1, 0], [0, -1], [-1, 0]].forEach(([nDR, nDC]) => ( | ||
(JSON.stringify([nDR, nDC]) !== JSON.stringify([dR, dC])) && (JSON.stringify([nDR, nDC]) !== JSON.stringify([-dR, -dC]))) | ||
&& (row + nDR >= 0 && row + nDR < INPUT.length && col + nDC >= 0 && col + nDC < INPUT[0].length) | ||
&& q.push({ c: c + INPUT[row + nDR][col + nDC], row: row + nDR, col: col + nDC, dR: nDR, dC: nDC, m: 1 }), | ||
)); | ||
} | ||
|
||
const pEnd = performance.now(); | ||
|
||
console.log("<DESCRIPTION>: " + result); | ||
console.log("LEAST HEAT LOSS: " + res); | ||
console.log(pEnd - pStart); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
"use strict"; | ||
|
||
/* eslint-disable one-var */ | ||
|
||
const fs = require("node:fs"); | ||
const path = require("node:path"); | ||
const { performance } = require("node:perf_hooks"); | ||
|
||
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n"); | ||
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n").map(l => l.split("").map(Number)); | ||
|
||
const pStart = performance.now(); | ||
|
||
// | ||
// YOUR CODE HERE | ||
// | ||
const result = "..."; | ||
let res = 0; | ||
const q = [{ c: 0, row: 0, col: 0, dR: 0, dC: 0, m: 0 }], seen = new Set(); | ||
while (q.length){ // @ts-ignore | ||
const { c, row, col, dR, dC, m } = q.sort((p, n) => n.c - p.c).pop(); | ||
(row === (INPUT.length - 1) && col === (INPUT[0].length - 1) && m >= 4) && (res = c); | ||
if (res !== 0) break; | ||
|
||
if (seen.has(JSON.stringify([row, col, dR, dC, m]))) continue; | ||
seen.add(JSON.stringify([row, col, dR, dC, m])); | ||
|
||
((m < 10 && !(dR === 0 && dC === 0)) | ||
&& (row + dR >= 0 && row + dR < INPUT.length && col + dC >= 0 && col + dC < INPUT[0].length) | ||
&& q.push( { c: c + INPUT[row + dR][col + dC], row: row + dR, col: col + dC, dR, dC, m: m + 1 }) || 1) | ||
&& ((m >= 4 || (dR === 0 && dC === 0)) && [[0, 1], [1, 0], [0, -1], [-1, 0]].forEach(([nDR, nDC]) => ( | ||
(JSON.stringify([nDR, nDC]) !== JSON.stringify([dR, dC])) && (JSON.stringify([nDR, nDC]) !== JSON.stringify([-dR, -dC]))) | ||
&& (row + nDR >= 0 && row + nDR < INPUT.length && col + nDC >= 0 && col + nDC < INPUT[0].length) | ||
&& q.push({ c: c + INPUT[row + nDR][col + nDC], row: row + nDR, col: col + nDC, dR: nDR, dC: nDC, m: 1 }), | ||
)); | ||
} | ||
|
||
const pEnd = performance.now(); | ||
|
||
console.log("<DESCRIPTION>: " + result); | ||
console.log("NEW LEAST HEAT LOSS: " + res); | ||
console.log(pEnd - pStart); |