Skip to content

Instantly share code, notes, and snippets.

@inodaf
Last active January 12, 2021 15:34
Show Gist options
  • Save inodaf/e23c79013358d86374c24bcc79b5229b to your computer and use it in GitHub Desktop.
Save inodaf/e23c79013358d86374c24bcc79b5229b to your computer and use it in GitHub Desktop.
Data Structures: Array
const twoDimensionsArray = [
[-9, -9, -9, 1, 1, 1],
[ 0, -9, 0, 4, 3, 2],
[-9, -9, -9, 1, 2, 3],
[ 0, 0, 8, 6, 6, 0],
[ 0, 0, 0, -3, 0, 0],
[ 0, 0, 1, 2, 4, 0],
]
const twoDimensionsArray2 = [
[0, -4, -6, 0, -7, -6],
[-1, -2, -6, -8, -3, -1],
[-8, -4, -2, -8, -8, -6],
[-3, -1, -2, -5, -7, -4],
[-3, -5, -3, -6, -6, -6],
[-3, -6, 0, -8, -6, -7],
]
function extractHourglasses(array2D) {
const hourglasses = []
for (let i = 0; i < array2D.length - 1; i++) {
for (let j = 0; j < array2D[i].length; j++) {
const [topA, topB, topC] = [
array2D[i][j],
array2D[i][j + 1],
array2D[i][j + 2]
]
const center = array2D[i + 1][j + 1]
const [bottomA, bottomB, bottomC] = [
array2D[i + 2] !== undefined && array2D[i + 2][j],
array2D[i + 2] !== undefined && array2D[i + 2][j + 1],
array2D[i + 2] !== undefined && array2D[i + 2][j + 2],
]
if (
topA !== undefined && topB !== undefined && topC !== undefined &&
center !== undefined &&
bottomA !== undefined && bottomB !== undefined && (bottomC !== undefined && bottomC !== false)
) {
hourglasses.push([topA, topB, topC, center, bottomA, bottomB, bottomC])
}
}
}
return hourglasses
}
Math.max(...extractHourglasses(twoDimensionsArray)
.map(hourglass => hourglass.reduce((acc, item) => acc + item, 0)))
function reverse(target: array) {
let reversed = [];
for (var i = target.length - 1; i >= 0; i--) {
reversed.push(target[i]);
}
return reversed;
}
const tostr = JSON.stringify
// Test Assertions
tostr(reverse([1, 4, 3, 2])) === '[2,3,4,1]';
tostr(reverse([-1, 2, 4, -3])) === '[-3,4,2,-1]';
tostr(reverse([4, 3, 2, 1])) === '[1,2,3,4]';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment