Skip to content

Commit 51c3b30

Browse files
fix(2018 day-02): account for variable padding needed around list of plants between generations
1 parent 99241d4 commit 51c3b30

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

2018/day-12/plants.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Plants {
22
constructor (initial, rules) {
33
this.generations = []
4+
this.boundaryBuffers = [0, 0]
45
this.rules = {}
56
this._setInitialGeneration(initial)
67
if (rules && rules.length > 0) {
78
rules.forEach((rule) => {
89
this.addRule(rule)
910
})
1011
}
12+
this.boundaryBuffers = this.getBoundaryBuffers()
1113
}
1214

1315
_setInitialGeneration (initial) {
@@ -63,11 +65,14 @@ class Plants {
6365
state: state
6466
}
6567
})
66-
// Add 2 pots at the beginning and end to support future generation
67-
for (let x = 1; x <= 2; x++) {
68+
69+
// Padd the list to support future generation
70+
for (let x = 1; x <= this.boundaryBuffers[0]; x++) {
6871
const first = nextGen[0].position
69-
const last = nextGen[nextGen.length - 1].position
7072
nextGen.splice(0, 0, { position: first - 1, state: '.' })
73+
}
74+
for (let x = 1; x <= this.boundaryBuffers[1]; x++) {
75+
const last = nextGen[nextGen.length - 1].position
7176
nextGen.push({ position: last + 1, state: '.' })
7277
}
7378

@@ -100,6 +105,40 @@ class Plants {
100105
}, [0, 0])
101106
}
102107

108+
/**
109+
* Rules need empty pots lef/right of row for verification. Figures out the number of pots we need
110+
* to add to the left/right of a row
111+
* @returns {Array} [left, right] necessary buffer size of first and last pots
112+
*/
113+
getBoundaryBuffers () {
114+
let buffers = [0, 0]
115+
Object.keys(this.rules).filter((rule) => this.rules[rule] === '#').forEach((rule) => {
116+
// For left edge
117+
for (let x = 0; x < rule.length; x++) {
118+
if (rule.substr(x, 1) === '.') {
119+
let y = x + 1
120+
buffers[0] = Math.max(buffers[0], y)
121+
} else {
122+
// break the loop when we encounter a #
123+
x = rule.length
124+
break
125+
}
126+
}
127+
128+
for (let x = rule.length - 1; x >= 0; x--) {
129+
if (rule.substr(x, 1) === '.') {
130+
let y = rule.length - x
131+
buffers[1] = Math.max(buffers[1], y)
132+
} else {
133+
// break the loop when we encounter a #
134+
x = -1
135+
break
136+
}
137+
}
138+
})
139+
return buffers
140+
}
141+
103142
/**
104143
* Generates a visual display of the pots in all generations. Accepts optional boundaries.
105144
* @param {Number} start First pot to include

2018/day-12/plants.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('--- Day 12: Subterranean Sustainability ---', () => {
6262
describe('advance()', () => {
6363
it('advances the next generation', () => {
6464
const expected = [
65+
{ position: -3, state: '.' },
6566
{ position: -2, state: '.' },
6667
{ position: -1, state: '.' },
6768
{ position: 0, state: '#' },
@@ -90,7 +91,8 @@ describe('--- Day 12: Subterranean Sustainability ---', () => {
9091
{ position: 23, state: '.' },
9192
{ position: 24, state: '#' },
9293
{ position: 25, state: '.' },
93-
{ position: 26, state: '.' }
94+
{ position: 26, state: '.' },
95+
{ position: 27, state: '.' }
9496
]
9597
let plantTracker = new Plants(initialState, rules)
9698
plantTracker.advance()
@@ -175,5 +177,13 @@ describe('--- Day 12: Subterranean Sustainability ---', () => {
175177
expect(actual).to.equal(expected)
176178
})
177179
})
180+
describe('findBoundaryBuffers()', () => {
181+
it('figures out how many buffer pots are needed at the ends each row based on the rules', () => {
182+
const expected = [3, 3]
183+
const plantTracker = new Plants(initialState, rules)
184+
const actual = plantTracker.getBoundaryBuffers()
185+
expect(actual).to.deep.equal(expected)
186+
})
187+
})
178188
})
179189
})

0 commit comments

Comments
 (0)