Skip to content

Commit 1f6fd0b

Browse files
committed
크레인 인형뽑기 게임(중급)
1 parent c34eecd commit 1f6fd0b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function solution(board, moves) {
2+
let answer = 0;
3+
const stack = [];
4+
const width = board.length;
5+
6+
const pullCrane = (order) => {
7+
const w = order - 1;
8+
let h = 0;
9+
10+
while (width > h) {
11+
const doll = board[h][w];
12+
if (doll !== 0) {
13+
if (stack[stack.length - 1] === doll) {
14+
stack.pop();
15+
answer += 2;
16+
} else {
17+
stack.push(board[h][w]);
18+
}
19+
20+
board[h][w] = 0;
21+
break;
22+
}
23+
h++;
24+
}
25+
};
26+
27+
moves.forEach((e) => pullCrane(e));
28+
29+
return answer;
30+
}
31+
32+
console.log(
33+
solution(
34+
[
35+
[0, 0, 0, 0, 0],
36+
[0, 0, 1, 0, 3],
37+
[0, 2, 5, 0, 1],
38+
[4, 2, 4, 4, 2],
39+
[3, 5, 1, 3, 1],
40+
],
41+
[1, 5, 3, 5, 1, 2, 1, 4]
42+
)
43+
);

0 commit comments

Comments
 (0)