-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP8.js
More file actions
77 lines (65 loc) · 1.94 KB
/
Copy pathP8.js
File metadata and controls
77 lines (65 loc) · 1.94 KB
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
/* ====== Problem 8 =================================== *
*
* The four adjacent digits in the 1000-digit number that
* have the greatest product are 9 × 9 × 8 × 9 = 5832.
*
* Find the thirteen adjacent digits in the 1000-digit
* number that have the greatest product. What is the value
* of this product?
*
* ========================================**/
const assert = require('assert');
const fs = require('fs');
var series = '';
function splitBy(series, adjacentMax) {
var array = [];
for (var i = 0; i < series.length - adjacentMax; i++) {
var product = 1;
for (var j = i; j < (i + adjacentMax); j++) {
if (series[j])
product *= +series[j];
}
array.push({position: i, product: product});
}
return array;
}
function findLargest(array) {
var max = 0, position = 0;
for (var i = 0; i < array.length; i++) {
if (array[i].product > max) {
max = array[i].product;
position = array[i].position;
}
}
return {largest: max, position: position};
}
function getAdjacent(series, position, adjacentMax) {
return series.substr(position, adjacentMax);
}
function findAdjacentProduct(series, adjacentMax) {
var array = splitBy(series, adjacentMax);
var product = findLargest(array);
return product.largest;
}
function solution(product) {
fs.readFile('./fixtures/P8.series.txt',
{encoding: 'utf8'},
(err, data) => {
if (err) throw err;
data = data.split('\n').join('');
var answer = findAdjacentProduct(data, 13);
console.log(`The solution to problem 8 is ${answer}`);
});
}
solution();
/* ====== Meta ========================================= *
*
* Estimated time of completion: 40 minutes
* Search engine used: Yes. For file read.
* Concepts learned: Basic usage of fs from node. Got to
* reiterate Math.max.apply(Math, array); Split a string by
* endlines and rejoin.
*
* Additional notes:
*
* ========================================**/