Skip to content

Commit 9a06551

Browse files
refactor(2018 day-14): run calculateXAfterY() more efficiently
1 parent 96b6a31 commit 9a06551

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

2018/day-14/recipes.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,19 @@ const calculateXAfterY = (x, y, recipes) => {
114114
loopRecipesForElves(recipes, 1)
115115
counter = recipes.length
116116
}
117-
console.log(`${y} matches ${recipes.length}`)
118117

119-
if (recipes.length === y) {
120-
iterator = recipes.head
121-
} else if (recipes.length > y) {
122118
// In case multidigit recipe results created more than Y
123-
iterator = recipes.head.prev
124-
}
119+
iterator = (recipes.length > y) ? recipes.head.prev : recipes.head
125120

121+
// Add enough recipes to cover X
126122
while (recipes.length < x + y) {
127-
loopRecipesForElves(recipes, 1)
123+
loopRecipesForElves(recipes, x + y - recipes.length)
128124
}
129125

130126
let result = ''
131127
while (result.length < x) {
132-
result += iterator.value.toString()
133128
iterator = iterator.next
129+
result += iterator.value.toString()
134130
}
135131
return result
136132
}

0 commit comments

Comments
 (0)