2021-10-02 23:45:28 +05:30
|
|
|
// Problem: https://projecteuler.net/problem=8
|
|
|
|
|
|
|
|
|
|
const largestAdjacentNumber = (grid, consecutive) => {
|
2021-10-02 23:55:41 +05:30
|
|
|
grid = grid.split('\n').join('')
|
2023-02-07 08:50:28 -08:00
|
|
|
const splitGrid = grid.split('\n')
|
2021-10-02 23:55:41 +05:30
|
|
|
let largestProd = 0
|
2021-10-02 23:45:28 +05:30
|
|
|
|
2023-02-07 08:50:28 -08:00
|
|
|
for (const row in splitGrid) {
|
2023-10-03 23:08:19 +02:00
|
|
|
const currentRow = splitGrid[row].split('').map((x) => Number(x))
|
2021-10-02 23:45:28 +05:30
|
|
|
|
|
|
|
|
for (let i = 0; i < currentRow.length - consecutive; i++) {
|
2021-10-02 23:55:41 +05:30
|
|
|
const combine = currentRow.slice(i, i + consecutive)
|
2021-10-02 23:45:28 +05:30
|
|
|
|
|
|
|
|
if (!combine.includes(0)) {
|
|
|
|
|
const product = combine.reduce(function (a, b) {
|
|
|
|
|
return a * b
|
2021-10-02 23:55:41 +05:30
|
|
|
})
|
2021-10-02 23:45:28 +05:30
|
|
|
|
2021-10-02 23:55:41 +05:30
|
|
|
if (largestProd < product) largestProd = product
|
2021-10-02 23:45:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-02 23:55:41 +05:30
|
|
|
return largestProd
|
2021-10-02 23:45:28 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-02 23:55:41 +05:30
|
|
|
export { largestAdjacentNumber }
|