Skip to content

Commit e80dd3d

Browse files
committed
Day 11 - optimise partial sum
1 parent 273ea70 commit e80dd3d

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

day-11-chronal-change/fuelcells.js

+35-25
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
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

5449
function calculateFuelCellValue(serial, x, y) {
@@ -63,23 +58,38 @@ function getHundredsValue(input) {
6358
}
6459

6560
function calculateMaxFuelGrid3(serial) {
66-
const grid = new FuelCellGrid(serial);
61+
const grid = new SummedAreaTable(serial);
6762
return grid.getMaxSubSectionCoordinate().coord;
6863
}
6964

7065
function 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

8392
module.exports.calculateFuelCellValue = calculateFuelCellValue;
8493
module.exports.calculateMaxFuelGrid3 = calculateMaxFuelGrid3;
85-
module.exports.calculateMaxFuelGrid = calculateMaxFuelGrid;
94+
module.exports.calculateMaxFuelGrid = calculateMaxFuelGrid;
95+
module.exports.calculateMaxFuelSum = calculateMaxFuelSum;

day-11-chronal-change/test.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const expect = require('chai').expect;
22

33
const fuelcells = require('./fuelcells');
44

5-
describe('Day 11: Chronal Charge', () => {
5+
describe.only('Day 11: Chronal Charge', () => {
66

77
describe('Calculate fuel cell value', () => {
88
it('fuel cell at 3,5 in a grid with serial number 8', () => {
@@ -30,9 +30,11 @@ describe('Day 11: Chronal Charge', () => {
3030
describe('Part One', () => {
3131
// it('print grid 33,45, serial 18', () => {
3232
// let serial = 18;
33-
// for(let y = 45;y<45+3;y++) {
33+
// let xStart = 1;
34+
// let yStart = 1;
35+
// for(let y = yStart;y<yStart+3;y++) {
3436
// let row = [];
35-
// for(let x = 33;x<33+3;x++) {
37+
// for(let x = xStart;x<xStart+3;x++) {
3638
// row.push(fuelcells.calculateFuelCellValue(serial, x, y));
3739
// }
3840
// console.log(row.join(' '));
@@ -52,17 +54,17 @@ describe('Day 11: Chronal Charge', () => {
5254
});
5355
});
5456

55-
describe.only('Part Two', () => {
57+
describe('Part Two', () => {
5658
it('should get x,y of largest total power for serial number 18', () => {
57-
expect(fuelcells.calculateMaxFuelGrid(18)).to.equal('90,269,16');
59+
expect(fuelcells.calculateMaxFuelSum(18)).to.equal('90,269,16');
5860
}).timeout(500000);
5961

6062
it('should get x,y of largest total power for serial number 42', () => {
61-
expect(fuelcells.calculateMaxFuelGrid(42)).to.equal('232,251,12');
63+
expect(fuelcells.calculateMaxFuelSum(42)).to.equal('232,251,12');
6264
}).timeout(500000);
6365

6466
it('should get x,y of largest total power for serial number 2187', () => {
65-
expect(fuelcells.calculateMaxFuelGrid(2187)).to.equal('233,40,13');
67+
expect(fuelcells.calculateMaxFuelSum(2187)).to.equal('233,40,13');
6668
}).timeout(500000);
6769
});
6870
});

0 commit comments

Comments
 (0)