-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15question1.ts
More file actions
108 lines (78 loc) · 2.41 KB
/
day15question1.ts
File metadata and controls
108 lines (78 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { readFileSync } from "fs";
class Node {
public risk: number;
private _adjacentNodes: Node[] = [];
public visited = false;
public safestPath: number = Infinity;
public get adjacentNodes(): Node[] {
return this._adjacentNodes;
}
constructor(risk: number) {
this.risk = risk;
}
}
type Path = {
risk: number | null;
length: number | null;
};
class RiskMap {
private rows: Node[][] = [];
private endingPoint: Node = null;
public addRow(rowInput: string) {
this.rows.push(rowInput.split("").map((i) => new Node(Number(i))));
}
public buildMap(): this {
const numberOfColumns = this.rows[0].length;
const numberOfRows = this.rows.length;
for (let y = 0; y < numberOfRows; y++) {
for (let x = 0; x < numberOfColumns; x++) {
let point = this.getNodes(x, y);
if (y != 0) {
point.adjacentNodes.push(this.getNodes(x, y - 1));
}
if (y + 1 < numberOfRows) {
point.adjacentNodes.push(this.getNodes(x, y + 1));
}
if (x != 0) {
point.adjacentNodes.push(this.getNodes(x - 1, y));
}
if (x + 1 < numberOfColumns) {
point.adjacentNodes.push(this.getNodes(x + 1, y));
}
}
}
this.endingPoint = this.rows[numberOfColumns - 1][numberOfRows - 1];
return this;
}
public findSafestPath() {
const startingNode = this.rows[0][0];
startingNode.risk = 0;
startingNode.safestPath = 0;
const nodes = this.rows.flat();
// Dijkstra
while (nodes.length !== 0) {
let node = nodes.sort((a, b) => a.safestPath - b.safestPath)[0];
node.visited = true;
nodes.shift();
const nodesToConsider = node.adjacentNodes.filter((n) => !n.visited);
for (let adjacentNode of nodesToConsider) {
if (node.safestPath + adjacentNode.risk < adjacentNode.safestPath) {
adjacentNode.safestPath = node.safestPath + adjacentNode.risk;
}
}
}
return this.endingPoint.safestPath;
}
public getNodes(x, y) {
return this.rows[y][x];
}
}
let inputs: string[];
const rawData = readFileSync("./day15inputs.txt", "utf8");
inputs = rawData.split("\r\n");
const riskMap = new RiskMap();
for (let input of inputs) {
riskMap.addRow(input);
}
riskMap.buildMap();
console.log(riskMap.findSafestPath());