-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.js
77 lines (67 loc) · 1.67 KB
/
11.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
const fs = require('fs')
const max = require('./utils/max')
const range = require('./utils/range')
// (void) => Promise<number[][]>
function getData() {
return new Promise(function(resolve, reject) {
fs.readFile('./inputs/11.txt', (err, data) => {
resolve(
data
.toString()
.split("\n")
.map(line => line.split(' ').map(Number))
)
})
})
}
// each path is [deltaY, deltaX]
const PATHS = {
'N': [-1, 0],
'E': [0, 1],
'S': [1, 0],
'W': [0, -1],
'NE': [-1, 1],
'SE': [1, 1],
'SW': [1, -1],
'NW': [-1, -1]
}
const DISTANCE = 3
const getProduct = (y, x, arr, direction, distance) => {
const values = [arr[y][x]]
for (let i = 1; i <= distance; i++) {
let yStep = PATHS[direction][0] * i
let xStep = PATHS[direction][1] * i
let nextX
let nextY
if (arr[y+yStep] === undefined) {
values.push(null)
} else if (arr[y+yStep][x+xStep] === undefined) {
values.push(null)
} else {
values.push(arr[y+yStep][x+xStep])
}
}
return values.reduce((acc, curr) => acc * (curr || 0), 1)
}
const getMaxProduct = (y, x, arr, distance) => {
return Object.keys(PATHS)
.map(direction => getProduct(y, x, arr, direction, distance))
.max()
}
const solveMatrix = (arr, distance) => {
let productMatrix = Array.from(new Array(arr.length), (x, i) => new Array(arr[0].length))
for (let y = 0; y < arr.length; y++) {
for (let x = 0; x < arr[0].length; x++) {
productMatrix[y][x] = getMaxProduct(x, y, arr, distance)
}
}
return productMatrix
.map(rows => rows.max())
.max()
}
getData()
.then(arr => {
console.log(
solveMatrix(arr, DISTANCE)
)
})