1
- class FuelCellGrid {
1
+ class SummedAreaTable {
2
2
constructor ( serialNo , length = 300 , subsectionSize = 3 ) {
3
3
this . serial = serialNo ;
4
4
this . length = length ;
5
5
this . subsectionSize = subsectionSize ;
6
- this . grid = Array
6
+ this . sum = Array
7
7
. from ( { length } )
8
8
. map ( ( ) => Array
9
9
. from ( { length } ) ) ;
10
- for ( let x = 0 ; x < 300 ; x ++ ) {
11
- for ( let y = 0 ; y < 300 ; y ++ ) {
12
- this . grid [ x ] [ y ] = calculateFuelCellValue ( this . serial , x + 1 , y + 1 ) ;
13
- if ( x >= 32 && x <= 34 && y >= 44 && y <= 46 ) {
14
- // console.log(calculateFuelCellValue(this.serial, x+1, y+1),'=',this.getValue(x,y));
15
- }
16
- //getGridValue(grid,32,44);
10
+ for ( let y = 0 ; y < length ; y ++ ) {
11
+ for ( let x = 0 ; x < length ; x ++ ) {
12
+ this . sum [ y ] [ x ] = calculateFuelCellValue ( this . serial , x + 1 , y + 1 ) + this . getSumValue ( y - 1 , x ) + this . getSumValue ( y , x - 1 ) - this . getSumValue ( y - 1 , x - 1 ) ;
13
+ //console.log(x,y,':',calculateFuelCellValue(this.serial, x+1, y+1), '+', this.getSumValue(y - 1,x), '+', this.getSumValue(y,x - 1) ,'-', this.getSumValue(y - 1,x - 1),'=',this.sum[y][x]);
17
14
}
18
15
}
19
16
}
@@ -23,32 +20,30 @@ class FuelCellGrid {
23
20
let coord ;
24
21
for ( let x = 0 ; x < this . length - size ; x ++ ) {
25
22
for ( let y = 0 ; y < this . length - size ; y ++ ) {
26
- let curr = this . sumSubSection ( x , y , size ) ;
23
+ let curr = this . sumSubSection ( y , x , size ) ;
27
24
if ( curr > currMax ) {
28
25
currMax = curr ;
29
- coord = `${ x + 1 } ,${ y + 1 } ` ;
26
+ coord = `${ x + 2 } ,${ y + 2 } ` ; //no idea why this is +2 rather than +1!
30
27
}
31
28
}
32
29
}
33
30
return { coord :coord , power :currMax } ;
34
31
}
35
32
36
- sumSubSection ( xStart , yStart , size = this . subsectionSize ) {
37
- let sum = 0 ;
38
- for ( let x = 0 ; x < size ; x ++ ) {
39
- for ( let y = 0 ; y < size ; y ++ ) {
40
- sum += this . grid [ x + xStart ] [ y + yStart ] ;
41
- }
33
+ sumSubSection ( y , x , size = this . subsectionSize ) {
34
+ return this . sum [ y ] [ x ] + this . sum [ y + size ] [ x + size ] - this . sum [ y + size ] [ x ] - this . sum [ y ] [ x + size ] ;
35
+ }
36
+
37
+ getSumValue ( y , x ) {
38
+ if ( x < 0 || y < 0 ) {
39
+ return 0 ;
42
40
}
43
- return sum ;
41
+ return this . sum [ y ] [ x ] ;
44
42
}
45
43
46
44
getValue ( x , y ) {
47
- let value = this . grid [ x ] [ y ] ;
48
- console . log ( `${ x + 1 } ,${ y + 1 } =${ value } ` ) ;
49
- return value ;
45
+ return this . sumSubSection ( y - 1 , x - 1 , 1 ) ;
50
46
}
51
-
52
47
}
53
48
54
49
function calculateFuelCellValue ( serial , x , y ) {
@@ -63,23 +58,38 @@ function getHundredsValue(input) {
63
58
}
64
59
65
60
function calculateMaxFuelGrid3 ( serial ) {
66
- const grid = new FuelCellGrid ( serial ) ;
61
+ const grid = new SummedAreaTable ( serial ) ;
67
62
return grid . getMaxSubSectionCoordinate ( ) . coord ;
68
63
}
69
64
70
65
function calculateMaxFuelGrid ( serial ) {
71
- const grid = new FuelCellGrid ( serial ) ;
66
+ const grid = new SummedAreaTable ( serial ) ;
72
67
73
68
const maxPowers = [ ] ;
74
69
for ( let i = 1 ; i <= 300 ; i ++ ) {
70
+ //console.log(i);
75
71
let max_i = grid . getMaxSubSectionCoordinate ( i ) ;
76
72
maxPowers . push ( { size :i , coord :max_i . coord , power :max_i . power } ) ;
77
73
}
78
74
let max = maxPowers . sort ( ( a , b ) => b . power - a . power ) [ 0 ] ;
75
+ //console.log(maxPowers);
76
+ return max . coord + ',' + max . size ;
77
+ }
79
78
79
+ function calculateMaxFuelSum ( serial ) {
80
+ const sum = new SummedAreaTable ( serial ) ;
81
+ const maxPowers = [ ] ;
82
+ for ( let i = 1 ; i <= 300 ; i ++ ) {
83
+ //console.log(i);
84
+ let max_i = sum . getMaxSubSectionCoordinate ( i ) ;
85
+ maxPowers . push ( { size :i , coord :max_i . coord , power :max_i . power } ) ;
86
+ }
87
+ let max = maxPowers . sort ( ( a , b ) => b . power - a . power ) [ 0 ] ;
88
+ //console.log(maxPowers);
80
89
return max . coord + ',' + max . size ;
81
90
}
82
91
83
92
module . exports . calculateFuelCellValue = calculateFuelCellValue ;
84
93
module . exports . calculateMaxFuelGrid3 = calculateMaxFuelGrid3 ;
85
- module . exports . calculateMaxFuelGrid = calculateMaxFuelGrid ;
94
+ module . exports . calculateMaxFuelGrid = calculateMaxFuelGrid ;
95
+ module . exports . calculateMaxFuelSum = calculateMaxFuelSum ;
0 commit comments