-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
halloween-sweets.js
93 lines (82 loc) · 2.55 KB
/
halloween-sweets.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function pick(bags, scale) {
if (bags.length === 3) {
const result = scale.weigh([bags[0]], [bags[1]]);
if (result === 0) {
return bags[2];
} else if (result === -1) {
return bags[0];
} else {
return bags[1];
}
}
const groupOne = bags.slice(0, 3);
const groupTwo = bags.slice(3, 6);
const groupThree = bags.slice(6);
const result = scale.weigh(groupOne, groupTwo);
if (result === 0) {
return pick(groupThree, scale);
} else if (result === -1) {
return pick(groupOne, scale);
} else {
return pick(groupTwo, scale);
}
}
function pick(bags, scale) {
if (bags.length === 3) {
const result = scale.weigh([bags[0]], [bags[1]]);
switch(result) {
case 0:
return bags[2];
case -1:
return bags[0];
case 1:
return bags[1]
}
}
const groupOne = bags.slice(0, 3);
const groupTwo = bags.slice(3, 6);
const groupThree = bags.slice(6);
const result = scale.weigh(groupOne, groupTwo);
switch(result) {
case 0:
return pick(groupThree, scale);
case -1:
return pick(groupOne, scale);
case 1:
return pick(groupTwo, scale);
}
}
function pick(bags, scale) {
if (bags.length === 3) {
return {
'0': bags[2],
'-1': bags[0],
'1': bags[1]
}[scale.weigh([bags[0]], [bags[1]])];
}
const groupOne = bags.slice(0, 3);
const groupTwo = bags.slice(3, 6);
const groupThree = bags.slice(6);
return {
'0': () => pick(groupThree, scale),
'-1': () => pick(groupOne, scale),
'1': () => pick(groupTwo, scale)
}[scale.weigh(groupOne, groupTwo)]();
}
function pick(bags, scale) {
return bags.length === 3
? {
'0': bags[2],
'-1': bags[0],
'1': bags[1]
}[scale.weigh([bags[0]], [bags[1]])]
: {
'0': () => pick(bags.slice(6), scale),
'-1': () => pick(bags.slice(0, 3), scale),
'1': () => pick(bags.slice(3, 6), scale)
}[scale.weigh(bags.slice(0, 3), bags.slice(3, 6))]();
}
function pick(bags, scale) {
return bags.length === 3 ? { '0': bags[2], '-1': bags[0], '1': bags[1] }[scale.weigh([bags[0]], [bags[1]])] : { '0': () => pick(bags.slice(6), scale), '-1': () => pick(bags.slice(0, 3), scale), '1': () => pick(bags.slice(3, 6), scale) }[scale.weigh(bags.slice(0, 3), bags.slice(3, 6))]();
}
function pick(bags,scale){return bags.length===3?{'0':bags[2],'-1':bags[0],'1':bags[1]}[scale.weigh([bags[0]],[bags[1]])]:{'0':()=>pick(bags.slice(6),scale),'-1':()=>pick(bags.slice(0,3),scale),'1':()=>pick(bags.slice(3,6),scale)}[scale.weigh(bags.slice(0,3),bags.slice(3,6))]()}