RepeaterCreeper
RepeaterCreeper

Reputation: 342

Project Euler #8 -- Javascript

/***
 * Problem 8 -- Largest Product in a Series
 *
 * @author RepeaterCreeper
 */

var grid = `73167176531330624919225119674426574742355349194934
    96983520312774506326239578318016984801869478851843
    85861560789112949495459501737958331952853208805511
    12540698747158523863050715693290963295227443043557
    66896648950445244523161731856403098711121722383113
    62229893423380308135336276614282806444486645238749
    30358907296290491560440772390713810515859307960866
    70172427121883998797908792274921901699720888093776
    65727333001053367881220235421809751254540594752243
    52584907711670556013604839586446706324415722155397
    53697817977846174064955149290862569321978468622482
    83972241375657056057490261407972968652414535100474
    82166370484403199890008895243450658541227588666881
    16427171479924442928230863465674813919123162824586
    17866458359124566529476545682848912883142607690042
    24219022671055626321111109370544217506941658960408
    07198403850962455444362981230987879927244284909188
    84580156166097919133875499200524063689912560717606
    05886116467109405077541002256983155200055935729725
    71636269561882670428252483600823257530420752963450`;

/**
 * Largest Adjacent Numbers
 * 
 * @returns int
 */
function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\n") {
  let gridData = grid.split("\n"),
    largestProduct = 0;

  for (var row in gridData) {
    currentRow = gridData[row].split('').map(x => parseInt(x));

    for (var i = 0; i < currentRow.length - consecutiveLength; i++) {
      combination = currentRow.slice(i, i + consecutiveLength);
      if (!combination.includes(0)) {
        product = combination.reduce(function(a, b) {
          return a * b
        });
        if (largestProduct < product) largestProduct = product;
      }
    }
  }

  return largestProduct;
}

console.log(largestAdjacentNumbers(grid, 13));

Issue:

So the issue the fact that no matter what I do, my code is stuck at getting:
5377010688

The correct answer is:
23514624000

As you can see that's a MASSIVE difference. If I simply do a 4-adjacent number in the grid, it works as it matches the solution given for the 4-adjacent number example. Which is:

5832


I've definitely looked into some other SO questions that relate to Problem 8 of Project Euler, but none of them seem to match my issue. If I did somehow miss one, a link would be nice. Though I do doubt that as I literally went through a good bit of SO questions before posting this.


Problem States:

https://projecteuler.net/problem=8 -- Link to Problem

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

Upvotes: 0

Views: 1717

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370929

The question says:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

(some number over multiple lines, 1000 digits long)

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

It's not asking you to iterate over each row - it's asking you to interpret the whole thing as one string (or number) and compare adjacent digits. Turn the grid into a single string initially, and your code works as expected:

var grid = `73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450`
    .split('\n').join('');

/**
 * Largest Adjacent Numbers
 * 
 * @returns int
 */
function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\n") {
  let gridData = grid.split("\n"),
    largestProduct = 0;

  for (var row in gridData) {
    currentRow = gridData[row].split('').map(x => parseInt(x));

    for (var i = 0; i < currentRow.length - consecutiveLength; i++) {
      combination = currentRow.slice(i, i + consecutiveLength);
      if (!combination.includes(0)) {
        product = combination.reduce(function(a, b) {
          return a * b
        });
        if (largestProduct < product) largestProduct = product;
      }
    }
  }

  return largestProduct;
}

console.log(largestAdjacentNumbers(grid, 13));

But it would be good to remove the part that iterates over each row now that there are no rows or newlines in the input, and to fix the off-by-one error, as noted in comment:

var grid = `73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450`;
var gridData = grid.split('\n').join('').split('').map(Number);
/**
 * Largest Adjacent Numbers
 * 
 * @returns int
 */
function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\n") {
  let largestProduct = 0;

  for (var i = 0; i <= gridData.length - consecutiveLength; i++) {
    combination = gridData.slice(i, i + consecutiveLength);
    if (!combination.includes(0)) {
      const product = combination.reduce(function(a, b) {
        return a * b
      });
      if (largestProduct < product) largestProduct = product;
    }
  }

  return largestProduct;
}

console.log(largestAdjacentNumbers(grid, 13));

Upvotes: 2

ASDFGerte
ASDFGerte

Reputation: 5193

The problem apparently doesn't intend that the end of line has any meaning. Here is code that produces the desired result.

let str = `73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450`;
str = str.replace(/\n/g, "");

let max = 0;
for (let i = 0; i < str.length - 12; i++) {
	let part = str.substr(i, 13);
	let currentProduct = 1;
	for (let n of part) {
		currentProduct *= +n;
	}
	max = Math.max(max, currentProduct);
}

console.log(max);

Upvotes: 0

Related Questions