Skip to content

Commit 1dc7b24

Browse files
feat(2018 day-11): Finds the square with the most power
1 parent ea468f5 commit 1dc7b24

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

2018/day-11/fuel-cells.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { dynamicSort } = require('../day-04/helpers')
2+
13
class Rack {
24
/**
35
* Initializes a new power rack
@@ -30,6 +32,10 @@ class Rack {
3032
}
3133
}
3234

35+
getCellsByPower () {
36+
return this.cells.sort(dynamicSort('-squareTotal'))
37+
}
38+
3339
/**
3440
* Calculates the Rack ID at the specified coordinate
3541
* @param {Array} coords [x,y]
@@ -57,14 +63,18 @@ class Rack {
5763

5864
_tallySquare (idx, size) {
5965
let power = 0
66+
let valid = true
6067
for (let x = 0; x < size[0]; x++) {
6168
for (let y = 0; y < size[0]; y++) {
6269
let pointer = idx + x + (y * this.size[0])
6370
let dest = this.cells[pointer]
71+
valid = (dest) ? valid : false // flag invalid results from cells off the grid
6472
power += (dest) ? dest.power : 0
6573
}
6674
}
67-
return power
75+
76+
// Discard value if square exceeds the grid
77+
return (valid) ? power : null
6878
}
6979

7080
tallySquares (size) {

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,26 @@ describe('--- Day 11: Chronal Charge ---', () => {
7676
expect(actual).to.equal(expected)
7777
})
7878
})
79+
describe('getCellsByPower()', () => {
80+
it('sorts the cells by their available square power, skipping ones that go off the edge of the rack', () => {
81+
const expected = [33, 45]
82+
const serial = 18
83+
const gridSize = [300, 300]
84+
const squareSize = [3, 3]
85+
const grid = new Rack(serial, gridSize)
86+
grid.tallySquares(squareSize)
87+
const actual = grid.getCellsByPower(squareSize)[0].coords
88+
expect(actual).to.deep.equal(expected)
89+
})
90+
it('sorts the cells by their available square power, skipping ones that go off the edge of the rack', () => {
91+
const expected = [21, 61]
92+
const serial = 42
93+
const gridSize = [300, 300]
94+
const squareSize = [3, 3]
95+
const grid = new Rack(serial, gridSize)
96+
grid.tallySquares(squareSize)
97+
const actual = grid.getCellsByPower(squareSize)[0].coords
98+
expect(actual).to.deep.equal(expected)
99+
})
100+
})
79101
})

2018/day-11/solution.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const { Rack } = require('./fuel-cells')
22
const serial = 1133 // From puzzle input
3+
const size = [300, 300]
4+
const squareSize = [3, 3]
35

4-
let powerBank = new Rack(serial)
5-
6-
console.log(powerBank)
6+
let powerBank = new Rack(serial, size)
7+
powerBank.tallySquares(squareSize)
8+
const answer = powerBank.getCellsByPower(squareSize)[0].coords
9+
const answer2 = ''
10+
console.log(`-- Part 1 --`)
11+
console.log(`Answer: ${answer}`)
12+
console.log(`-- Part 2 --`)
13+
console.log(`Answer: ${answer2}`)

0 commit comments

Comments
 (0)