-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLv.2.광물캐기.js
More file actions
73 lines (58 loc) · 1.6 KB
/
Lv.2.광물캐기.js
File metadata and controls
73 lines (58 loc) · 1.6 KB
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
const exhausionMap = {
diamond: {
diamond: 1,
iron: 1,
stone: 1
},
iron: {
diamond: 5,
iron: 1,
stone: 1
},
stone: {
diamond: 25,
iron: 5,
stone: 1
}
};
function solution(picks, minerals) {
let queue = [[picks, 0]];
let mineralIndex = 0;
const getAddedFatigue = (type) => {
let addedFatigue = 0;
for (let i = mineralIndex; i < mineralIndex + 5; i += 1) {
const targetMineral = minerals[i];
if (targetMineral) {
addedFatigue += exhausionMap[type][targetMineral];
}
}
return addedFatigue;
};
const results = [];
while (queue.length && mineralIndex < minerals.length) {
const newQueue = [];
for (const oneQueue of queue) {
const [picks, fatigue] = oneQueue;
const [diamondCount, ironCount, stoneCount] = picks;
if (!diamondCount && !ironCount && !stoneCount) {
results.push(fatigue);
} else {
if (diamondCount) {
const addedFatigue = getAddedFatigue("diamond");
newQueue.push([[diamondCount - 1, ironCount, stoneCount], fatigue + addedFatigue]);
}
if (ironCount) {
const addedFatigue = getAddedFatigue("iron");
newQueue.push([[diamondCount, ironCount - 1, stoneCount], fatigue + addedFatigue]);
}
if (stoneCount) {
const addedFatigue = getAddedFatigue("stone");
newQueue.push([[diamondCount, ironCount, stoneCount - 1], fatigue + addedFatigue]);
}
}
}
queue = newQueue;
mineralIndex += 5;
}
return Math.min(...queue.map((el) => el[1]), ...results);
}