-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.js
67 lines (51 loc) · 1.18 KB
/
recipe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Node {
constructor (value, next) {
this.value = value;
this.next = next;
}
}
class LinkedList {
constructor () {
this.head = null;
this.tail = null;
this.length = 0;
this.addToTail(3);
this.addToTail(7);
}
addToTail (value) {
if (this.tail === null) {
const node = new Node(value, this.head);
this.head = this.tail = node.next = node;
} else {
const node = new Node(value, this.head);
this.tail.next = node;
this.tail = node;
}
this.length++;
}
}
const recipe = (input) => {
const list = new LinkedList();
const elves = [list.head, list.tail];
while (list.length < input + 10) {
`${elves[0].value + elves[1].value}`
.split('')
.forEach((value) => list.addToTail(+value));
for (let e = 0; e < elves.length; e++) {
const steps = elves[e].value + 1;
for (let step = 0; step < steps; step++) {
elves[e] = elves[e].next;
}
}
}
const scores = [];
let node = list.tail;
for (let i = 0; i < input + 10; i++) {
node = node.next;
if (i >= input) {
scores.push(node.value);
}
}
return scores.join('');
};
module.exports = recipe;