Skip to content

Commit 2744874

Browse files
author
Alberto Bastos
committed
day 14. just some minor refactor for loop end condition readibility
1 parent 11dd749 commit 2744874

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/d14.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function run(n) {
3131

3232
// keep making recipes until we have 10 recipes after the input
3333
let expectedRecipes = n + 10;
34-
while (data.recipes.length < expectedRecipes || data.part2 < 0) {
34+
while (!data.score || data.part2 < 0) {
3535
let toAdd = (data.recipes[data.elf1] + data.recipes[data.elf2])
3636
.toString()
3737
.split("")
@@ -40,6 +40,11 @@ function run(n) {
4040
data.elf1 = (data.elf1 + 1 + data.recipes[data.elf1]) % data.recipes.length;
4141
data.elf2 = (data.elf2 + 1 + data.recipes[data.elf2]) % data.recipes.length;
4242

43+
// check if we already reach the desired number of recipes to get the score
44+
if (!data.score && data.recipes.length >= expectedRecipes) {
45+
data.score = data.recipes.slice(n, n + 10).join("");
46+
}
47+
4348
// check if we found our input in the recipes list for the first time
4449
// optimization: we just need to look it up at the end of the recipes list, no need to indexOf of the full list each time!
4550
if (data.part2 < 0) {
@@ -54,7 +59,6 @@ function run(n) {
5459
//printStatus(data);
5560
}
5661

57-
data.score = data.recipes.slice(n, n + 10).join("");
5862
return data;
5963
}
6064

0 commit comments

Comments
 (0)