1- class FuelCellGrid {
1+ class SummedAreaTable {
22 constructor ( serialNo , length = 300 , subsectionSize = 3 ) {
33 this . serial = serialNo ;
44 this . length = length ;
55 this . subsectionSize = subsectionSize ;
6- this . grid = Array
6+ this . sum = Array
77 . from ( { length } )
88 . map ( ( ) => Array
99 . 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]);
1714 }
1815 }
1916 }
@@ -23,32 +20,30 @@ class FuelCellGrid {
2320 let coord ;
2421 for ( let x = 0 ; x < this . length - size ; x ++ ) {
2522 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 ) ;
2724 if ( curr > currMax ) {
2825 currMax = curr ;
29- coord = `${ x + 1 } ,${ y + 1 } ` ;
26+ coord = `${ x + 2 } ,${ y + 2 } ` ; //no idea why this is +2 rather than +1!
3027 }
3128 }
3229 }
3330 return { coord :coord , power :currMax } ;
3431 }
3532
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 ;
4240 }
43- return sum ;
41+ return this . sum [ y ] [ x ] ;
4442 }
4543
4644 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 ) ;
5046 }
51-
5247}
5348
5449function calculateFuelCellValue ( serial , x , y ) {
@@ -63,23 +58,38 @@ function getHundredsValue(input) {
6358}
6459
6560function calculateMaxFuelGrid3 ( serial ) {
66- const grid = new FuelCellGrid ( serial ) ;
61+ const grid = new SummedAreaTable ( serial ) ;
6762 return grid . getMaxSubSectionCoordinate ( ) . coord ;
6863}
6964
7065function calculateMaxFuelGrid ( serial ) {
71- const grid = new FuelCellGrid ( serial ) ;
66+ const grid = new SummedAreaTable ( serial ) ;
7267
7368 const maxPowers = [ ] ;
7469 for ( let i = 1 ; i <= 300 ; i ++ ) {
70+ //console.log(i);
7571 let max_i = grid . getMaxSubSectionCoordinate ( i ) ;
7672 maxPowers . push ( { size :i , coord :max_i . coord , power :max_i . power } ) ;
7773 }
7874 let max = maxPowers . sort ( ( a , b ) => b . power - a . power ) [ 0 ] ;
75+ //console.log(maxPowers);
76+ return max . coord + ',' + max . size ;
77+ }
7978
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);
8089 return max . coord + ',' + max . size ;
8190}
8291
8392module . exports . calculateFuelCellValue = calculateFuelCellValue ;
8493module . exports . calculateMaxFuelGrid3 = calculateMaxFuelGrid3 ;
85- module . exports . calculateMaxFuelGrid = calculateMaxFuelGrid ;
94+ module . exports . calculateMaxFuelGrid = calculateMaxFuelGrid ;
95+ module . exports . calculateMaxFuelSum = calculateMaxFuelSum ;
0 commit comments