Skip to content

Commit a822069

Browse files
test(2018 day-14): tuning buffers exposes a failing test. recipe list is not being constructed correctly.
1 parent d55eb11 commit a822069

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

2018/day-14/recipes.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,25 @@ const calculateXAfterY = (x, y, recipes, elves) => {
111111
* @param {String} pattern to search for
112112
* @param {LinkedList} recipes recipe list
113113
* @param {Array} elves doing the work
114-
* @param {Number} bufferSize bucket size to search. Higher sizes use more memory but go faster
114+
* @param {Number} bufferSize bucket size to search. Tuning bucket size can improve speed but may risk missing match if the match crosses buckets.
115115
*/
116116
const findPattern = (pattern, recipes, elves, bufferSize) => {
117-
bufferSize = bufferSize || 10000
117+
bufferSize = bufferSize || 101
118118
let matched = false
119119
let position = recipes.length
120120
while (matched !== true) {
121121
let haystack = loopRecipesForElves(elves, recipes, bufferSize)
122-
if (haystack.indexOf(pattern) > -1) {
123-
position += haystack.indexOf(pattern)
122+
let offset = haystack.indexOf(pattern)
123+
if (offset > -1) {
124+
position += offset
125+
console.log(`Found ${pattern} at ${haystack.substr(0, offset + pattern.length)}`)
124126
matched = true
125127
} else {
126-
position += 1000
128+
position += bufferSize
129+
console.log(`Did not find ${pattern} before ${position}`)
127130
}
128131
}
132+
129133
return position
130134
}
131135

2018/day-14/solution.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const {
22
calculateXAfterY,
3+
findPattern,
34
Recipes
45
} = require('./recipes')
56

67
const input = 540561
78

8-
const elves = [3, 7]
9-
const recipes = new Recipes(elves[0])
9+
let elves = [3, 7]
10+
let recipes = new Recipes(elves[0])
1011

1112
elves.forEach((elf, idx) => {
1213
if (idx === 0) {
@@ -17,9 +18,20 @@ elves.forEach((elf, idx) => {
1718
})
1819

1920
const answer = calculateXAfterY(10, input, recipes, elves)
20-
const answer2 = ''
2121

2222
console.log(`-- Part 1 --`)
2323
console.log(`Answer: ${answer}`)
24+
25+
elves = [3, 7]
26+
recipes = new Recipes(elves[0])
27+
elves.forEach((elf, idx) => {
28+
if (idx === 0) {
29+
elves[0] = recipes.head
30+
} else {
31+
elves[idx] = recipes.addRecipe(elf)
32+
}
33+
})
34+
const bufferSize = 10001
35+
const answer2 = findPattern(input.toString(), recipes, elves, bufferSize)
2436
console.log(`-- Part 2 --`)
2537
console.log(`Answer: ${answer2}`)

0 commit comments

Comments
 (0)