-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
maxHexagonBeam.js
185 lines (162 loc) · 3.83 KB
/
maxHexagonBeam.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
function createHexagon(n, seq) {
const hexagon = [];
const height = 2 * n - 1;
let current = 0;
let currentWidth = n;
for (let row = 0; row < height; row++) {
const beam = [];
for (let col = 0; col < currentWidth; col++) {
beam.push(seq[current % seq.length]);
current++;
}
hexagon.push(beam);
if (row >= n - 1) {
currentWidth--;
} else {
currentWidth++;
}
}
return hexagon;
}
function sumBeam(beam) {
return beam.reduce((sum, number) => sum + number);
}
// n == 4
// 0 0
// 1 0
// 2 0
// 3 0
// 0 1
// 1 1
// 2 1
// 3 1
// 4 0
// 0 2
// 1 2
// 2 2
// 3 2
// 4 1
// 5 0
// 0 3
// 1 3
// 2 3
// 3 3
// 4 2
// 5 1
// 6 0
// 1 4
// 2 4
// 3 4
// 4 3
// 5 2
// 6 1
// 2 5
// 3 5
// 4 4
// 5 3
// 6 2
// 3 6
// 4 5
// 5 4
// 6 3
// n === 2
// 0 0
// 1 0
// 0 1
// 1 1
// 2 0
// 1 2
// 2 1
// function getForwardDiagonalBeams(n, hexagon) {
// const height = 2 * n - 1;
// const diagonals = [];
// let currentCol = 0;
// let currentLength = n;
// let start = 0;
// for (let diagonal = 0; diagonal < height; diagonal++) {
// const forwardDiagonal = [];
// let innerCol = currentCol;
// // TODO: i <= currentlength sometimes....
// for (let i = start; i < currentLength; i++) {
// if (i >= n) {
// innerCol -= 1;
// forwardDiagonal.push(hexagon[i][innerCol]);
// } else {
// forwardDiagonal.push(hexagon[i][currentCol]);
// }
// }
// if (diagonal >= Math.floor(height / 2)) {
// currentLength--;
// start++;
// } else {
// currentLength++;
// }
// currentCol++;
// diagonals.push(forwardDiagonal);
// }
// return diagonals;
// }
function padForwardDiagonalHexagon(n, hexagon) {
const height = 2 * n - 1;
const paddedHexagon = hexagon.map((row, i) => {
const numZeroes = height - row.length;
if (i + 1 > Math.floor(height / 2)) {
return Array(numZeroes).fill(0).concat(row);
}
return row.concat(Array(numZeroes).fill(0));
});
return paddedHexagon;
}
function getForwardDiagonalBeamSums(n, hexagon) {
const height = 2 * n - 1;
const paddedHexagon = padForwardDiagonalHexagon(n, hexagon);
const diagonalBeamSums = [];
for (let colNum = 0; colNum < height; colNum++) {
let diagonalSum = 0;
for (let rowNum = 0; rowNum < height; rowNum++) {
diagonalSum += paddedHexagon[rowNum][colNum];
}
diagonalBeamSums.push(diagonalSum);
}
return diagonalBeamSums;
}
function padBackwardDiagonalHexagon(n, hexagon) {
const height = 2 * n - 1;
const paddedHexagon = hexagon.map((row, i) => {
const numZeroes = height - row.length;
if (i + 1 > Math.floor(height / 2)) {
return row.concat(Array(numZeroes).fill(0));
}
return Array(numZeroes).fill(0).concat(row);
});
return paddedHexagon;
}
function getBackwardDiagonalBeamSums(n, hexagon) {
const height = 2 * n - 1;
const paddedHexagon = padBackwardDiagonalHexagon(n, hexagon);
const diagonalBeamSums = [];
for (let colNum = 0; colNum < height; colNum++) {
let diagonalSum = 0;
for (let rowNum = 0; rowNum < height; rowNum++) {
diagonalSum += paddedHexagon[rowNum][colNum];
}
diagonalBeamSums.push(diagonalSum);
}
return diagonalBeamSums;
}
// THANK YOU: to the person that suggested to implement with padding... https://i.imgur.com/PGGhhyY.png
function maxHexagonBeam(n, seq) {
const hexagon = createHexagon(n, seq);
const horizontalSums = hexagon.map(sumBeam);
const forwardDiagonalSums = getForwardDiagonalBeamSums(n, hexagon);
const backwardsDiagonalSums = getBackwardDiagonalBeamSums(n, hexagon);
return Math.max(...horizontalSums.concat(forwardDiagonalSums).concat(backwardsDiagonalSums));
}
module.exports = {
maxHexagonBeam,
sumBeam,
createHexagon,
getForwardDiagonalBeamSums,
padForwardDiagonalHexagon,
getBackwardDiagonalBeamSums,
};