Skip to content

Commit d6e652c

Browse files
feat(2018 day-11): adds a way to find the max powered square without first generating cached tallies for every point
1 parent ade791b commit d6e652c

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

2018/day-11/fuel-cells.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Rack {
1212
if (typeof size === 'object' && size.length === 2) {
1313
this._allocateGrid(size)
1414
}
15+
this.maxSquare = null
1516
}
1617

1718
/**
@@ -89,10 +90,17 @@ class Rack {
8990

9091
tallySquares (size) {
9192
this.cells = this.cells.map((cell, idx) => {
92-
cell.squareTotal = this._tallySquare(idx, size)
93+
cell.squareTotal = this._tallySquare(idx, size) || null
9394
return cell
9495
})
9596
}
97+
98+
findMaxSquare (size) {
99+
return this.cells.reduce((acc, cell, idx) => {
100+
let squarePower = this._tallySquare(idx, size)
101+
return (squarePower !== null && squarePower > acc.power) ? { power: squarePower, idx: idx } : acc
102+
}, { power: -99999, idx: -1 })
103+
}
96104
}
97105

98106
module.exports = {

2018/day-11/fuel-cells.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,15 @@ describe('--- Day 11: Chronal Charge ---', () => {
9898
expect(actual).to.deep.equal(expected)
9999
})
100100
})
101+
describe('findMaxSquare(size)', () => {
102+
it('locates the top left corner of the square (of specified size) that has the maximum power in the grid', () => {
103+
const expected = [21, 61]
104+
const serial = 42
105+
const gridSize = [300, 300]
106+
const squareSize = [3, 3]
107+
const grid = new Rack(serial, gridSize)
108+
const actual = grid.cells[grid.findMaxSquare(squareSize).idx].coords
109+
expect(actual).to.deep.equal(expected)
110+
})
111+
})
101112
})

2018/day-11/solution.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@ const size = [300, 300]
55
const squareSize = [3, 3]
66

77
let powerBank = new Rack(serial, size)
8-
powerBank.tallySquares(squareSize)
9-
const answer = powerBank.getCellsByPower(squareSize)[0].coords
8+
// powerBank.tallySquares(squareSize)
9+
// const answer = powerBank.getCellsByPower(squareSize)[0].coords
10+
let max = powerBank.findMaxSquare(squareSize).idx
11+
const answer = powerBank.cells[max].coords
12+
13+
console.log(`-- Part 1 --`)
14+
console.log(`Answer: ${answer}`)
1015

1116
const anySizeSquares = []
1217
for (let dial = 1; dial <= 300; dial++) {
1318
console.log(`Measuring power with dial at ${dial}`)
14-
powerBank.tallySquares([dial, dial])
15-
let bestOfSizeX = powerBank.getCellsByPower(squareSize)[0]
19+
// powerBank.tallySquares([dial, dial])
20+
// let bestOfSizeX = powerBank.getCellsByPower(squareSize)[0]
21+
// anySizeSquares.push({
22+
// coords: bestOfSizeX.coords,
23+
// power: bestOfSizeX.squareTotal,
24+
// size: dial
25+
// })
26+
const max = powerBank.findMaxSquare([dial, dial])
1627
anySizeSquares.push({
17-
coords: bestOfSizeX.coords,
18-
power: bestOfSizeX.squareTotal,
28+
coords: powerBank.cells[max.idx].coords,
29+
power: max.power,
1930
size: dial
2031
})
2132
}
@@ -24,7 +35,5 @@ const bestOfAnySize = anySizeSquares.sort(dynamicSort('-power'))[0]
2435
let answer2 = bestOfAnySize.coords
2536
answer2.push(bestOfAnySize.size)
2637

27-
console.log(`-- Part 1 --`)
28-
console.log(`Answer: ${answer}`)
2938
console.log(`-- Part 2 --`)
3039
console.log(`Answer: ${answer2}`)

0 commit comments

Comments
 (0)