๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ, ์ •๊ทœ์‹ test() ํ•จ์ˆ˜ (feat.๋น„๋‚œ๋‹˜์˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ)

728x90

์˜ค๋Š˜๋„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ๋จผ์ € ๋‚˜์—ดํ•ด๋ดค๋‹ค.

 

function solution(numbers, hand) {
    const click = [];
    const keypad = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
        ['*', 0, '#']
    ]
    const findKey = (n) => {
        let result = [];
        keypad.forEach((line, i) => {
            if (line.findIndex(v => v === n) !== -1) {
                result = [i, line.findIndex(v => v === n)];
                return;
            }
        })
        return result;
    }

    let [leftNow, rightNow] = [[3, 0], [3, 2]];
    numbers.forEach(n => {
        const left = [1, 4, 7];
        const right = [3, 6, 9];
        const leftFn = () => {
            click.push("L");
            leftNow = nPosition;
        }
        const rightFn = () => {
            click.push("R");
            rightNow = nPosition;
        }
        let nPosition = findKey(n);

        if (left.includes(n)) {
            leftFn();
        }
        else if (right.includes(n)) {
            rightFn();
        }
        else {
            let distance = (nowPosition) => Math.abs(nPosition[0] - nowPosition[0]) + Math.abs(nPosition[1] - nowPosition[1])
            const leftD = distance(leftNow);
            const rightD = distance(rightNow);
            if (leftD === rightD) {
                hand === "left" ? leftFn() : rightFn();
            } else {
                leftD < rightD ? leftFn() : rightFn();
            }
        }
    });
    return click.join("")
}

 

left, right ๋ฐฐ์—ด์—์„œ findIndex ํ•˜๋Š” ๋ถ€๋ถ„์€ ์ •๊ทœ์‹์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

 

       if (/[147]/.test(x)) {
            leftFn();
        }
      if (/[369]/.test(x)) {
            rightFn();
        }

 

์ฒ˜์Œ์— test ํ•จ์ˆ˜๊ฐ€ ์ž‘์„ฑํ•œ ์‚ฌ๋žŒ์ด ๋งŒ๋“  ์ž„์˜์˜ ํ•จ์ˆ˜์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ, 

์•Œ๊ณ ๋ณด๋‹ˆ ์ •๊ทœ์‹ ๊ณ ์ฐจํ•จ์ˆ˜์˜€๋‹ค...!!

์™€!

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

 

RegExp.prototype.test() - JavaScript | MDN

test() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํŒ๋ณ„ํ•˜๊ณ , ๊ทธ ์—ฌ๋ถ€๋ฅผ true ๋˜๋Š” false๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 

test() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํŒ๋ณ„ํ•˜๊ณ , ๊ทธ ์—ฌ๋ถ€๋ฅผ true ๋˜๋Š” false๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์กฑํ•œ๋‹ค๋Š” ๊ฑด -> ์ •๊ทœ ํ‘œํ˜„์‹์ด ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๊ฐ€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ข‹์€ ํ•จ์ˆ˜๋ฅผ ์ด์ œ ์•Œ์•˜๋‹ค๋‹ˆ

์—ญ์‹œ mdn ์€ ๊ผผ๊ผผํžˆ ๋ณผ ์ˆ˜๋ก ์ด๋“์ด๋‹น

 

 

 

 

๋‘๊ตฌ๋‘๊ตฌ ๊ทธ๋ฆฌ๊ณ  ์˜ค๋žœ๋งŒ์— ๋น„๋‚œ๋‹˜์ด ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ํ•ด์ฃผ์…จ๋‹ค.

์™œ์ธ์ง€ ๊ฐˆ ์ˆ˜๋ก ๊ผผ๊ผผํ•˜๊ฒŒ ํ•ด์ฃผ์‹œ๋Š”๊ฑฐ ๊ฐ™์•„์„œ ๋„ˆ๋ฌด ๊ฐ์‚ฌํ•˜๋ˆผ....

๊ณ ์Šค๋ž€ํžˆ ํก์ˆ˜ํ•˜๋ฉด ์–ผ๋งˆ๋‚˜ ์ข‹์„๊นŒ!

 

 

์•ˆ๋…•ํ•˜์„ธ์š” ๋˜์ž๋‹˜ ํ•ด๋‹น ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์ „์— ์ฝ”๋“œ ์Šคํƒ€์ผ๋ถ€ํ„ฐ ์ ๊ฒ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋งˆ์นจํ‘œ ์ข…๊ฒฐํ‘œ ์„ธ๋ฏธํด๋ก ์„ ์ƒ๋žต ์—ฌ๋ถ€๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ œ์ถœํ•œ ์ฝ”๋“œ๋Š”  distance ํ•จ์ˆ˜๋งŒ ์„ธ๋ฏธํด๋ก ์„ ์ƒ๋žตํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋Š” ์„ธ๋ฏธํด๋ก ์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ค‘๊ฐ„์— ์ƒ๋žต์„ ํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์—์„œ ์ž๋™์œผ๋กœ ์„ธ๋ฏธํด๋ก ์„ ๋ถ™์—ฌ์ฃผ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๊ด€๋œ ๋ฐฉ์‹์œผ๋กœ ํ†ต์ผ์„ ํ•ด์•ผ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•—... ์„ธ๋ฏธ์ฝœ๋ก  ์ œ๋Œ€๋กœ ๋ถ™์ด๋Š”์ง€๋„ ๋ณด์‹œ๋‹ค๋‹ˆ ๋น„๋‚œ๋‹˜์€ ์—ญ์‹œ GPT ์•„๋‹๊นŒ...?

 

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

์ด๊ฑด ๊ฑ ์›ƒ๊ฒจ์„œ ๊ฐ€์ ธ์˜ด

 

๋„˜๋‚˜ ์นœ์ ˆ.....

 

 

 

๊ทธ๋ฆฌ๊ณ  ์ด์ œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ ๋ถ€๋ถ„์„ ์ ๊ฒ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ๊ธฐ์กด์˜ findKey ํ•จ์ˆ˜๋Š” forEach๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚คํŒจ๋“œ์˜ ๊ฐ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉด์„œ, 
์ˆซ์ž n์ด ์žˆ๋Š” ํ–‰์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ findIndex ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ–‰ ๋‚ด์—์„œ n์˜ ์œ„์น˜๋ฅผ ์ฐพ์•˜๊ณ , n์ด ์žˆ์œผ๋ฉด n์˜ ์œ„์น˜๋ฅผ ๊ฒฐ๊ณผ์— ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ findIndex๋ฅผ ๋‘๋ฒˆ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด findIndex๋ฅผ ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

const findPosition = n => keypad.reduce((acc, row, i) => row.includes(n) ? [i, row.indexOf(n)] : acc, []);

๋ฆฌํŒฉํ† ๋ง๋œ findPosition ํ•จ์ˆ˜์—์„œ๋Š” reduce ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 
reduce ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ, ๋ˆ„์ ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 
์ด ๋•Œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ˜„์žฌ ์š”์†Œ๊ฐ€ n์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , n์ด ์žˆ์œผ๋ฉด n์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜,
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ˆ„์ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ findIndex๋ฅผ ๋‘๋ฒˆ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ณ  includes์™€ indexOf๋ฅผ ๊ฐ๊ฐ ํ•œ๋ฒˆ ์”ฉ ์‚ฌ์šฉํ•˜์—ฌ
ํ˜ธ์œจ์„ฑ์„ ๋†’์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ์กด์˜ ํ•จ์ˆ˜์˜ ์ด๋ฆ„ key๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ  ๋ถˆ๋ถ„๋ช…ํ•ฉ๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๊ฐ€ ํ•˜๋Š”์ผ์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊ฟ”์•ผํ•ฉ๋‹ˆ๋‹ค. 
์œ„์น˜๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์—  ๋ฆฌํŒฉํ† ๋ง ๋œ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค.

 

ํ  ์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ผ๊นŒ

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

 

์ดํ•ดํ•˜๊ณ  ์ž˜๋ชป ์ดํ•ดํ•œ ์ค„ ๋ฐํ—ท

 

์ด ํ•จ์ˆ˜๋Š” keypad์˜ ๊ฐ ์š”์†Œ (์ฆ‰, ๊ฐ ํ–‰)์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. 
์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ˆ„์ ๊ฐ’ (acc)์ด๋ฉฐ, 
์ดˆ๊ธฐ๊ฐ’์€ ๋นˆ ๋ฐฐ์—ด []์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ˜„์žฌ ์š”์†Œ (์ฆ‰, ํ˜„์žฌ ํ–‰)์ž…๋‹ˆ๋‹ค.

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ˜„์žฌ ํ–‰์ด n์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 
๋งŒ์•ฝ n์„ ํฌํ•จํ•˜๋ฉด, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” n์˜ ์œ„์น˜ ([i, row.indexOf(n)])๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 
์ด ์œ„์น˜๋Š” ๋‹ค์Œ ๋ฐ˜๋ณต์—์„œ์˜ ๋ˆ„์ ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํ˜„์žฌ ํ–‰์ด n์„ ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฉด, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋ˆ„์ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 
์ฆ‰, ๋ˆ„์ ๊ฐ’์€ ์ด์ „ ํ–‰์—์„œ ์ฐพ์€ n์˜ ์œ„์น˜๋ฅผ ์œ ์ง€ํ•˜๊ฑฐ๋‚˜, 
์•„์ง n์„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ๋นˆ ๋ฐฐ์—ด []์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ reduce ํ•จ์ˆ˜๋Š” keypad์˜ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜๋ฉด์„œ n์˜ ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. 
๋งŒ์•ฝ n์„ ์ฐพ์œผ๋ฉด ๊ทธ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , n์„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด ๋นˆ ๋ฐฐ์—ด []์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

๋งˆ์ด ๋ธŒ๋ ˆ์ธ ๋งž์ถคํ˜• ์„ค๋ช…..... ๊ฐ์‚ฌํ•˜๋‹ค....

 

 

 

์ค‘๋ณต์ฝ”๋“œ์ œ๊ฑฐํ•˜๊ธฐ

leftFn ๊ณผ rightFn
๊ธฐ์กด์˜ ์ฝ”๋“œ๋Š” leftFn๊ณผ rightFn์„ ๋”ฐ๋กœ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์€ ์ž…๋ ฅ๊ฐ’๋งŒ ๋‹ค๋ฅผ๋ฟ ํ•˜๋Š”์ผ์ด ๊ฐ™์€ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ค‘๋ณต์„ ์ค„์—ฌ์•ผํ•ฉ๋‹ˆ๋‹ค.

distance ํ•จ์ˆ˜
๊ธฐ์กด์˜ ์ฝ”๋“œ๋Š” forEach ์•ˆ์—์„œ ์กฐ๊ฑด๋ฌธ๋ฅผ ํ†ตํ•ด ๊ฑฐ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด์˜ ์ฝ”๋“œ if ์™€ else if๋Š” "L" ๋˜๋Š” "R"๋ฅผ ์ด์šฉํ•˜๊ณ  ์œ„์น˜๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ distance ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋œ else ๋ฌธ์„ ๋ณด๋ฉด if ์™€ else...if์™€ ๋‹ค๋ฅด๊ฒŒ 
์ถ”๊ฐ€์ ์ธ ๊ฑฐ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉฐ ์‹คํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์กฐ๊ฑด๋ฌธ์—์„œ else๋ฌธ๋งŒ ํ•จ์ˆ˜๋ฅผ ์ •์˜๋ฅผ ํ•˜๊ณ  ์žˆ๊ธฐ๋•Œ๋ฌธ์—

์ด์ „์— ์˜ฌ๋ฆฐ ๋ฆฌํŒฉํ† ๋ง๋œ ํ•จ์ˆ˜ (findPostion)์™€ ๊ฐ™์€ ์Šค์ฝ”ํ”„์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค.

const findPosition = n => keypad.reduce((acc, row, i) => row.includes(n) ? [i, row.indexOf(n)] : acc, []);
const distance = (p1, p2) => Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);


์ฆ‰ ์กฐ๊ฑด๋ฌธ์•ˆ์— ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์ง€ ๋ง๊ณ  forEach ๋ฌธ ๋ฐ–์—์„œ ์ •์˜ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์˜คํ˜ธ... 

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ else if ๋ฌธ ์•ˆ์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ–ˆ๋Š”๋ฐ, ...

์ด ๋ถ€๋ถ„ ์ž‘์„ฑํ•  ๋•Œ else ๋ฌธ์—์„œ๋งŒ ์“ฐ๋‹ˆ๊นŒ else ๋ฌธ ์•ˆ์— ๋„ฃ์–ด์•ผ์ง€~ ํ•˜๊ณ  ์“ด๊ฑด๋ฐ

 


์กฐ๊ฑด๋ฌธ ์ „์ฒด์˜ ํ๋ฆ„..? ๋•Œ๋ฌธ์— ์กฐ๊ฑด๋ฌธ ๋ฐ–์—์„œ ์“ฐ๋Š” ๊ฒŒ ์ ์ ˆํ•˜๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋˜๋Š”๊ฑธ๊นŒ??

 

 

์ตœ์ข… ์ˆ˜์ •๋œ ๋ฆฌํŒฉํ† ๋ง ์ฝ”๋“œ
const solution = (numbers, hand) => {
    const keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ['*', 0, '#']];
    let [left, right] = [[3, 0], [3, 2]];
    const findPosition = n => keypad.reduce((acc, row, i) => row.includes(n) ? [i, row.indexOf(n)] : acc, []);
    const distance = (p1, p2) => Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);

    return numbers.map(n => {
        const pos = findPosition(n);
        const [leftKeys, rightKeys] = [[1, 4, 7], [3, 6, 9]];

        if (leftKeys.includes(n)) {
            left = pos;
            return 'L';
        } else if (rightKeys.includes(n)) {
            right = pos;
            return 'R';
        } else {
            const [leftDist, rightDist] = [distance(left, pos), distance(right, pos)];
            const result = leftDist < rightDist || (leftDist === rightDist && hand === 'left') ? 'L' : 'R';
            result === 'L' ? left = pos : right = pos;
            return result;
        }
    }).join('');
}

 

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ• ์‚ฌ์šฉํ•˜๊ธฐ
const left = [1, 4, 7];
const right = [3, 6, 9];

์œ„์˜ ์ฝ”๋“œ๋Š” ๊ธฐ์กด์˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. left right๋ณ€์ˆ˜๋Š” ์„œ๋กœ ์—ฐ๊ด€์ด ์žˆ๋Š” ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ€์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 const [leftKeys, rightKeys] = [[1, 4, 7], [3, 6, 9]];

 

3.
๊ธฐ์กด์˜ ์ฝ”๋“œ๋Š” ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด "L"๊ณผ "R"์„ click ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ   ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ฐฑ์‹ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 
๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ์ผ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์„  ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด leftFn, rightFn ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š”๊ฒƒ์€ 
์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์€ ์ ‘๊ทผ์ž…๋‹ˆ๋‹ค.

 const leftFn = () => {
            click.push("L");
            leftNow = nPosition;
}
const rightFn = () => {
            click.push("R");
            rightNow = nPosition;
 }


๋‹ค๋งŒ  ํ•ด๋‹น ํ•จ์ˆ˜๋“ค์€ ์„œ๋กœ ๊ฐ™์€ ์ผ์„ ํ•˜๊ณ  ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜์˜ ์ผ์ด ๋งค์šฐ ์งง์Šต๋‹ˆ๋‹ค. 
๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ค‘๋ณต์„ ๋ฌถ์„ ๋งŒํผ ์กฐ๊ฑด๋ฌธ์•ˆ์— ๋‘๊ฐ€์ง€ ์ผ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์กฐ๊ฑด๋ฌธ์„ ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์งค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
ํŠนํžˆ else ๋ฌธ์„ ๋ณด์‹œ๋ฉด ๋‘ ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ ์กฐ๊ฑด๋ฌธ ๋‚ด๋ถ€๋กœ ์ง์ ‘ ์ด๋™์‹œ์ผœ ์ค‘๋ณต์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

if (leftKeys.includes(n)) {
  left = pos;
  return 'L';
} else if (rightKeys.includes(n)) {
  right = pos;
  return 'R';
} else {
  const [leftDist, rightDist] = [distance(left, pos), distance(right, pos)];
  const result = leftDist < rightDist || (leftDist === rightDist && hand === 'left') ? 'L' : 'R';
  result === 'L' ? left = pos : right = pos;

 

4.
๊ทธ๋ฆฌ๊ณ  else ๋ฌธ์— ์žˆ๋˜ ๋ณต์žกํ•œ ์กฐ๊ฑด๋ฌธ์„ ๊ฐ„๊ฒฐ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด์˜ ์ฝ”๋“œ
else {
  let distance = (nowPosition) => Math.abs(nPosition[0] - nowPosition[0]) + Math.abs(nPosition[1] - nowPosition[1])
  const leftD = distance(leftNow);
  const rightD = distance(rightNow);
  if (leftD === rightD) {
    hand === "left" ? leftFn() : rightFn();
  } else {
    leftD < rightD ? leftFn() : rightFn();
  }
}


๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ
else {
  const [leftDist, rightDist] = [distance(left, pos), distance(right, pos)];
  const result = leftDist < rightDist || (leftDist === rightDist && hand === 'left') ? 'L' : 'R';
  result === 'L' ? left = pos : right = pos;
  return result;
}

 

์บฌ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ž‘ ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž ๋ฅผ ์ด์šฉํ•ด์„œ ์ €๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ตฌ๋‚˜...! 

๋‚˜๋Š” ๊ฐ™์€ ๊ฒฝ์šฐ๋ž‘ ๋‹ค ๋”ฐ๋กœ ํ•ด๋Š”๋ฐ, 

|| ๋’ค์— ๊ฐ™์€ ๊ฒฝ์šฐ๋„ ํ•ด๋‹น๋˜๋‹ˆ๊นŒ ๊ทธ ์ดํ›„์— ๋งž์ถฐ์„œ ๋‹ค์‹œ L ์„ ์ถœ๋ ฅํ• ์ง€ R ์„ ์ถœ๋ ฅํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๊ตฐ..!

L, R ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ๊ฒฐ๋ก ์„ ๋‚ด๋ฆฌ๊ณ , ๊ทธ์— ๋”ฐ๋ผ pos (์ขŒํ‘œ) ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 

๊ทธ๋ฆฌ๊ณ  L ์ธ์ง€ R ์ธ์ง€ return ํ•ด๋ฒ„๋ฆผ. 

 

 

๋งˆ์ง€๋ง‰์œผ๋กœ forEach ๋ณด๋‹ค map ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•œ ํด๋ฆญ ๊ฒฐ๊ณผ๋ฅผ ์ฆ‰์‹œ ๋ณ€ํ™˜์„ ์‹œ์ผœ 
click ์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ์ง์ ‘ ์ •์˜ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค.

 ์˜ค... ์–ด์ฐจํ”ผ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ฑฐ๋‹ˆ๊นŒ map ์„ ์ด์šฉํ•ด์„œ ํ‰คํ‰ค ํ•œ ๋ฐฐ์—ด ์ž์ฒด๋ฅผ join("") ํ•œ ๊ฐ’์„ return ํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜...!

 

์ „์ฒด ์‹์—์„œ return ํ•ด์•ผํ•˜๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์ด๋ผ๋ฉด map ์ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฑธ ์ด์šฉํ•˜๋ฉด ๋ชฌ๊ฐ€ ๋ฉ‹์ง„ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ๊ตฐ!

 

 

 

 

 

๊ณต๋ถ€ ๋‹ค ํ–ˆ์œผ๋‹ˆ ๋‹ค์‹œ ์งœ๋ณด์ž..!!

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•