Skip to content

Commit cb1a6d8

Browse files
feat(2018 day-14): find the position of a specified pattern
1 parent 59f02c9 commit cb1a6d8

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

2018/day-14/recipes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Recipes {
66
this.head = null
77
this.tail = null
88
this.length = 0
9+
this.tracker = ''
910
this.addFirst(recipe)
1011
}
1112

@@ -15,6 +16,7 @@ class Recipes {
1516
newRecipe.prev = newRecipe
1617
this.head = newRecipe
1718
this.tail = newRecipe
19+
this.tracker += recipe.toString()
1820
this.length++
1921
return this
2022
}
@@ -30,6 +32,7 @@ class Recipes {
3032
newRecipe.prev = this.head // link new recipe to old head
3133
this.head.next = newRecipe
3234
this.head = newRecipe // make new recipe the new head
35+
this.tracker += recipe.toString() // Sore the sequence
3336
this.length++
3437
return this.head
3538
}
@@ -103,8 +106,25 @@ const calculateXAfterY = (x, y, recipes, elves) => {
103106
return result
104107
}
105108

109+
/**
110+
* Counts how many recipes are to the left of the specified pattern
111+
* @param {String} pattern to search for
112+
* @param {LinkedList} recipes recipe list
113+
* @param {Array} elves doing the work
114+
*/
115+
const findPattern = (pattern, recipes, elves) => {
116+
let position = -1
117+
// Generate the sequence until the sequence exists
118+
while (position < 0) {
119+
loopRecipesForElves(elves, recipes, 1)
120+
position = recipes.tracker.indexOf(pattern)
121+
}
122+
return position
123+
}
124+
106125
module.exports = {
107126
calculateXAfterY,
127+
findPattern,
108128
loopRecipesForElves,
109129
Recipes,
110130
totalDigitsInArray

2018/day-14/recipes.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const expect = require('chai').expect
33
const {
44
calculateXAfterY,
5+
findPattern,
56
loopRecipesForElves,
67
Recipes,
78
totalDigitsInArray
@@ -136,5 +137,77 @@ describe('--- Day 14: Chocolate Charts ---', () => {
136137
expect(actual).to.equal('5941429882')
137138
})
138139
})
140+
describe('findPattern()', () => {
141+
it('counts the number of recipes to the left of the specified pattern', () => {
142+
const elves = [3, 7]
143+
const recipes = new Recipes(elves[0])
144+
let actual = ''
145+
146+
elves.forEach((elf, idx) => {
147+
if (idx === 0) {
148+
elves[0] = recipes.head
149+
} else {
150+
elves[idx] = recipes.addRecipe(elf)
151+
}
152+
})
153+
154+
actual = findPattern('51589', recipes, elves)
155+
expect(actual).to.equal(9)
156+
})
157+
})
158+
describe('findPattern()', () => {
159+
it('counts the number of recipes to the left of the specified pattern', () => {
160+
const elves = [3, 7]
161+
const recipes = new Recipes(elves[0])
162+
let actual = ''
163+
164+
elves.forEach((elf, idx) => {
165+
if (idx === 0) {
166+
elves[0] = recipes.head
167+
} else {
168+
elves[idx] = recipes.addRecipe(elf)
169+
}
170+
})
171+
172+
actual = findPattern('01245', recipes, elves)
173+
expect(actual).to.equal(5)
174+
})
175+
})
176+
describe('findPattern()', () => {
177+
it('counts the number of recipes to the left of the specified pattern', () => {
178+
const elves = [3, 7]
179+
const recipes = new Recipes(elves[0])
180+
let actual = ''
181+
182+
elves.forEach((elf, idx) => {
183+
if (idx === 0) {
184+
elves[0] = recipes.head
185+
} else {
186+
elves[idx] = recipes.addRecipe(elf)
187+
}
188+
})
189+
190+
actual = findPattern('92510', recipes, elves)
191+
expect(actual).to.equal(18)
192+
})
193+
})
194+
describe('findPattern()', () => {
195+
it('counts the number of recipes to the left of the specified pattern', () => {
196+
const elves = [3, 7]
197+
const recipes = new Recipes(elves[0])
198+
let actual = ''
199+
200+
elves.forEach((elf, idx) => {
201+
if (idx === 0) {
202+
elves[0] = recipes.head
203+
} else {
204+
elves[idx] = recipes.addRecipe(elf)
205+
}
206+
})
207+
208+
actual = findPattern('59414', recipes, elves)
209+
expect(actual).to.equal(2018)
210+
})
211+
})
139212
})
140213
})

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