Skip to content

Commit dfa4234

Browse files
fix(2018 day-14): catch patterns that occur at search buffer breaks
1 parent 1511bc8 commit dfa4234

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

2018/day-14/recipes.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,31 @@ const calculateXAfterY = (x, y, recipes) => {
135135
* Counts how many recipes are to the left of the specified pattern
136136
* @param {String} pattern to search for
137137
* @param {LinkedList} recipes recipe list
138-
* @param {Number} bufferSize bucket size to search. Tuning bucket size can improve speed but may risk missing match if the match crosses buckets.
138+
* @param {Number} bufferSize bucket size to search. Tuning bucket size can improve speed by adjusting memory usage.
139139
*/
140140
const findPattern = (pattern, recipes, bufferSize) => {
141141
bufferSize = bufferSize || 101
142142
let matched = false
143143
let position = recipes.length
144+
let overlapBuffer = ''
145+
144146
while (matched !== true) {
147+
console.log(`Checking for ${pattern} in segement starting at ${position}`)
145148
let haystack = loopRecipesForElves(recipes, bufferSize)
149+
146150
let offset = haystack.indexOf(pattern)
151+
152+
position = (offset >= 0) ? position + offset : recipes.length
147153
if (offset > -1) {
148-
position += offset
149-
console.log(`Found ${pattern} at ${haystack.substr(0, offset + pattern.length)}`)
154+
// console.log(`Found ${pattern} at ${haystack.substr(0, offset + pattern.length)}`)
155+
matched = true
156+
}
157+
// Use another small buffer to check the string that overlaps the split between buffer segements
158+
overlapBuffer = overlapBuffer.substr(overlapBuffer.length - 1 - pattern.length, pattern.length)
159+
overlapBuffer += haystack.substr(0, pattern.length)
160+
if (overlapBuffer.indexOf(pattern) > -1) {
161+
position = position - pattern.length + overlapBuffer.indexOf(pattern)
150162
matched = true
151-
} else {
152-
position += bufferSize
153-
console.log(`Did not find ${pattern} before ${position}`)
154163
}
155164
}
156165

2018/day-14/recipes.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,26 @@ describe('--- Day 14: Chocolate Charts ---', () => {
104104
const actual = findPattern('51589', recipes)
105105
expect(actual).to.equal(9)
106106
})
107-
})
108-
describe('findPattern()', () => {
109107
it('counts the number of recipes to the left of the specified pattern', () => {
110108
const actual = findPattern('01245', recipes)
111109
expect(actual).to.equal(5)
112110
})
113-
})
114-
describe('findPattern()', () => {
115111
it('counts the number of recipes to the left of the specified pattern', () => {
116112
const actual = findPattern('92510', recipes)
117113
expect(actual).to.equal(18)
118114
})
119-
})
120-
describe('findPattern()', () => {
121115
it('counts the number of recipes to the left of the specified pattern', () => {
122116
const actual = findPattern('59414', recipes)
123117
expect(actual).to.equal(2018)
124118
})
119+
it('accepts small search buffer sizes', () => {
120+
const actual = findPattern('59414', recipes, 20)
121+
expect(actual).to.equal(2018)
122+
})
123+
it('accepts large search buffer sizes', () => {
124+
const actual = findPattern('59414', recipes, 50000)
125+
expect(actual).to.equal(2018)
126+
})
125127
})
126128
})
127129
})

2018/day-14/solution.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {
22
calculateXAfterY,
33
findPattern,
4+
loopRecipesForElves,
45
Recipes
56
} = require('./recipes')
67

@@ -13,16 +14,13 @@ const answer = calculateXAfterY(10, input, recipes)
1314
console.log(`-- Part 1 --`)
1415
console.log(`Answer: ${answer}`)
1516

16-
elves = [3, 7]
17-
recipes = new Recipes(elves[0])
18-
elves.forEach((elf, idx) => {
19-
if (idx === 0) {
20-
elves[0] = recipes.head
21-
} else {
22-
elves[idx] = recipes.addRecipe(elf)
23-
}
24-
})
25-
const bufferSize = 10001
26-
const answer2 = findPattern(input.toString(), recipes, elves, bufferSize)
17+
recipes = new Recipes([3, 7])
18+
while (recipes.length < 3000) {
19+
loopRecipesForElves(recipes, 1)
20+
}
21+
22+
recipes = new Recipes([3, 7])
23+
const bufferSize = 10000
24+
const answer2 = findPattern(input.toString(), recipes, bufferSize)
2725
console.log(`-- Part 2 --`)
2826
console.log(`Answer: ${answer2}`)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy