-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-15.js
185 lines (159 loc) · 4.38 KB
/
day-15.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const fsp = require("fs/promises")
const path = require("path")
const {performance} = require("perf_hooks")
main()
async function main(){
const start = performance.now()
const input = await parseInput()
const partOneStart = performance.now()
console.log("Part one:", partOne(input))
const partOneEnd = performance.now()
console.log(`Part one runtime: ${(partOneEnd - partOneStart).toFixed(2)}ms`)
const partTwoStart = performance.now()
console.log("Part two:", partTwo(input))
const partTwoEnd = performance.now()
console.log(`Part two runtime: ${(partTwoEnd - partTwoStart).toFixed(2)}ms`)
const end = performance.now()
console.log(`Total runtime: ${(end - start).toFixed(2)}ms`)
}
async function parseInput(){
const rawInput = await fsp.readFile(path.resolve(__dirname, "day-15-input.txt"), "utf-8")
const input = rawInput.split("\n").filter(Boolean).map(line => line.split("").map(Number))
return input
}
function partOne(input){
return dijkstra(input)
}
function partTwo(input){
const newInput = []
buildDown(input, newInput)
buildAcross(input, newInput)
return dijkstra(newInput)
}
function buildAcross(input, newInput){
for(let i = 0; i < 4; i++){
newInput.forEach((row, rowI) => {
const originalRow = row.slice(0, input.length)
const newRow = originalRow.map(num => {
const newNum = num + i + 1
return newNum > 9 ? newNum % 9 : newNum
})
newInput[rowI] ? newInput[rowI].push(...newRow) : newInput.push(newRow)
})
}
}
function buildDown(input, newInput){
for(let i = 0; i < 5; i++){
input.forEach(row => {
const newRow = row.map(num => {
const newNum = num + i
return newNum > 9 ? newNum % 9 : newNum
})
newInput.push(newRow)
})
}
}
function dijkstra(input){
input = mapToNodes(input)
let unvisited = input.flatMap(node => node)
let current = initNode(input)
while(!currentIsLastNode(current, input)){
visitNeighbors()
logProgress(input, unvisited)
}
process.stdout.write("\n")
return current.minDanger.val
function visitNeighbors(){
const neighbors = getNeighbors(input, current)
for(const neighbor of neighbors){
neighbor.setMinDanger(current)
}
current.visited = true
unvisited = unvisited.filter(node => node !== current)
current = getSmallestUnvisited(unvisited)
}
}
function mapToNodes(input){
return input.map((line, rowI) => {
return line.map((node, colI) => {
return new Node(node, new Point(rowI, colI))
})
})
}
class Node {
/**
*
* @param {number} val
* @param {Point} point
*/
constructor(val, point){
this.val = val
this.minDanger = {
val: Infinity,
via: null
}
this.point = point
this.visited = false
}
/**
*
* @param {Node} potentialParent
*/
setMinDanger(potentialParent){
const newVal = potentialParent.minDanger.val + this.val
if(newVal < this.minDanger.val){
this.minDanger = {
val: newVal,
via: potentialParent
}
}
}
}
class Point {
/**
*
* @param {number} row
* @param {number} col
*/
constructor(row, col){
this.row = row
this.col = col
}
}
function initNode(input){
const start = input[0][0]
if(start.minDanger.val === Infinity){
start.minDanger.val = 0
}
return start
}
function getNeighbors(input, current){
return [
input[current.point.row - 1]?.[current.point.col],
input[current.point.row + 1]?.[current.point.col],
input[current.point.row]?.[current.point.col + 1],
input[current.point.row]?.[current.point.col - 1],
].filter(node => !!node && !node.visited)
}
function getSmallestUnvisited(unvisited){
let smallestUnvisited = null
for(const node of unvisited){
if(!node.visited){
if(!smallestUnvisited || node.minDanger.val < smallestUnvisited.minDanger.val){
smallestUnvisited = node
}
}
}
return smallestUnvisited
}
function currentIsLastNode(current, input){
return current.point.row === input.length - 1 && current.point.col === input[0].length - 1
}
function logProgress(input, unvisited){
process.stdout.cursorTo(0)
process.stdout.write((() => {
const inputLen = input.reduce((total, row) => total + row.length, 0)
return `${((inputLen - unvisited.length) / inputLen * 100).toFixed(2)}% visited`
})())
process.stdout.clearLine(1)
}